私は完全に機能するカウントダウンタイマーを設計していますが、これにとどまる場合に限りActivity
ます。カウントダウンが開始され、たとえば戻って other を開始するActivity
と、 countdonws はカウントダウンに従います。論理的には、カウントダウン タイマーがあるアクティビティをもう一度開始すると、何もカウント ダウンしていないように見えますが、バクグラウンドではカウント ダウンしていることがわかります。タイマーのオンティックで、続行通知をスローして、通知タブ。
コードは次のとおりです。sendnotification メソッドは、時間が終了するとアラートをスローし、sendnotificationTiempo
1 秒ごとにタブに通知を送信して上部に表示するメソッドです。
public static final int NOTIFICATION_ID = 33;
public static final int NOTIFICATION_ID_Tiempo = 34;
private int minCrono;
TextView text;
MyCounter timer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StartCrono = (Button) findViewById(R.id.butt_start);
CancelCrono = (Button) findViewById(R.id.butt_cancel);
text=(TextView)findViewById(R.id.text_countTime1);
CancelCrono.setEnabled(false);
minCrono = mins.getCurrentItem();
StartCrono.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
minCrono=(60000*minCrono);
timer = new MyCounter(minCrono,1000);
timer.start();
}
});
CancelCrono.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timer.cancel();
text.setText("El tiempo se ha cancelado");
minutosCrono = mins.getCurrentItem();
if (mNotificationManager != null)
mNotificationManager.cancel(NOTIFICATION_ID_Tiempo);
}
});
}
カウントダウンクラス:
public class MyCounter extends CountDownTimer{
public MyCounter(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
StartCrono.setEnabled(true);
CancelCrono.setEnabled(false);
mins.setEnabled(true);
minCrono = mins.getCurrentItem();
//
text.setText("Time Complete!");
sendnotification("Guau", "Time finished.");
}
@Override
public void onTick(long millisUntilFinished) {
long segRestantesTotal=(millisUntilFinished/1000);
long minRestantes= (segRestantesTotal/60);
long segRestantes= (segRestantesTotal%60);
if(segRestantes>=10){
text.setText(minRestantes+":"+segRestantes);
sendnotificationTiempo("Guau", minRestantes+":"+segRestantes);
}
else{
text.setText(minRestantes+":0"+segRestantes);
sendnotificationTiempo("Guau", minRestantes+":0"+segRestantes);
}
}
}
}
だから私がやるべきことは、いつ戻ってActivity
別のものを開始するかを知り、バックグラウンドでカウントダウンしている時間を表示して、たとえばテキストビューで表示することです。
または、通知タブからカウントダウンされている時間を取得できれば完璧です。ありがとう!