package com.example.timer;
import java.math.BigDecimal;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends Activity implements OnClickListener {
TextView mTextView;
Button mStartBtn, mStopBtn;
float mLaptime = 0.0f;
MyTimerTask timerTask = null;
Timer mTimer = null;
Handler mHandler = new Handler();
Button callintentbutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// IDの取得
mTextView = (TextView) findViewById(R.id.LapTime);
mStartBtn = (Button) findViewById(R.id.StartBtn);
mStopBtn = (Button) findViewById(R.id.StopBtn);
callintentbutton = (Button) findViewById(R.id.callintentbutton);
mStartBtn.setOnClickListener(this);
mStopBtn.setOnClickListener(this);
mStopBtn.setOnClickListener(this);
callintentbutton.setOnClickListener(this);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
@Override
public void onClick(View v) {
Button btn = (Button) v;
switch (btn.getId()) {
// スタートボタンが押されたとき
case R.id.StartBtn:
if (mTimer == null) {
// タイマーの初期化処理
timerTask = new MyTimerTask();
mLaptime = 0.0f;
mTimer = new Timer(true);
mTimer.schedule(timerTask, 100, 100);
}
break;
// ストップボタンが押されたとき
case R.id.StopBtn:
if (mTimer != null) {
mTimer.cancel();
mTimer = null;
}
break;
// ストップボタンが押されたとき
case R.id.callintentbutton:
Intent intent = new Intent();
intent.setClassName("com.example.timer",
"com.example.timer.SubActivity");
startActivity(intent);
break;
default:
break;
}
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Toast.makeText( getApplicationContext(), Integer.toString(444), Toast.LENGTH_SHORT ).show();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mTimer.cancel();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//outState.putInt("ageKey", Integer.parseInt(mTextView.getText().toString()));
outState.putInt("ageKey", 111);
Toast.makeText( getApplicationContext(), Integer.toString(222), Toast.LENGTH_SHORT ).show();
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
int name = savedInstanceState.getInt("ageKey");
//Log.d("abcdefg",Integer.toString(name));
//mTextView.setText(Integer.toString(name));
Toast.makeText( getApplicationContext(), Integer.toString(333), Toast.LENGTH_SHORT ).show();
}
class MyTimerTask extends TimerTask {
@Override
public void run() {
// mHandlerを通じてUI Threadへ処理をキューイング
mHandler.post(new Runnable() {
public void run() {
// 実行間隔分を加算処理
mLaptime += 0.1d;
// 計算にゆらぎがあるので小数点第1位で丸める
BigDecimal bi = new BigDecimal(mLaptime);
float outputValue = bi
.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
// 現在のLapTime
mTextView.setText(Float.toString(outputValue));
}
});
}
}
}
0 件のコメント:
コメントを投稿