http://kenkenppa.seesaa.net/article/270364692.html
上記のサイトを元にCountDownTimerを使った
アプリを作成しているのですが、
カウントダウンスタート後に、ストップボタンをタップせず
端末の戻るボタン、もしくは、ホームボタンをタップすると
バックグラウンド上で動いているようで、
カウントダウンが0になったタイミングで勝手にトーストが表示されます。
ストップボタンをタップすることなく、
戻るボタン、ホームボタンをタップしたタイミング等で止めたいのですが
どのように改善したら良いでしょうか?
import android.app.Activity; import android.os.Bundle; import android.os.CountDownTimer; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { TextView timer; Button start,stop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); timer = (TextView)findViewById(R.id.textView1); start =(Button)findViewById(R.id.button1); stop =(Button)findViewById(R.id.button2); //CountDownの初期値 final MyCountDownTimer cdt = new MyCountDownTimer(180000, 1000); start.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // カウントダウン開始 cdt.start(); } }); stop.setOnClickListener(new OnClickListener(){ public void onClick(View v) { // カウントダウン中止 cdt.cancel(); } }); } public class MyCountDownTimer extends CountDownTimer{ public MyCountDownTimer(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); } @Override public void onFinish() { // カウントダウン完了後に呼ばれる timer.setText("0"); Toast.makeText(getApplicationContext(), "終了", Toast.LENGTH_LONG).show(); } @Override public void onTick(long millisUntilFinished) { // インターバル(countDownInterval)毎に呼ばれる timer.setText(Long.toString(millisUntilFinished/1000/60) + ":" + Long.toString(millisUntilFinished/1000%60)); } } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/10/23 14:25