mm:ss で良好なロッキング出力を備えたカウントダウン タイマーを作成しました。
したがって、前に activityView に切り替えて、タイマーを使用してアクティビティに戻ると、すべてがうまく見えますが;-):
- タイマーはまだバックグラウンドで実行されていますが、カウントされていないように見えます..
- この方法で、ボタンが既にアクティブになっているため、2 番目、3 番目、... タイマーを開始できます。
したがって、これらすべての設定を設定する必要があること、およびそれがごく普通のことであることを知っています。私は設定する必要があります
- 無効にする startButton - カウンターの実行中...
- ユーザーが戻ってきてもカウンターがまだアクティブな場合は、 activityView を更新します
ANDROIDでそれを行う方法を教えてもらえますか?ここに私の作業コード:
some imports....
public class BasicActivity extends Activity {
ImageButton start, stop;
TextView timer;
/** Called when the activity is first created. */
//Ist dei Verbindung zur XML
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_activity);
//TIMER
start = (ImageButton)findViewById(R.id.startButton);
stop = (ImageButton)findViewById(R.id.stopButton);
timer = (TextView)findViewById(R.id.basicCountDown);
timer.setText(getString(R.string.basic01)); // Platzhalter im Textfeld.
final MyCounter timer = new MyCounter(10000,1000);
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
timer.start();
}
});
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
timer.cancel();
}
});
}
public class MyCounter extends CountDownTimer{
public MyCounter(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
System.out.println("Timer Completed.");
timer.setText(getString(R.string.basic02));
//Spielt den TON ab, wenn die ZEit um ist. Siehe Methode playSound!
playSound();
//Notification
new AlertDialog.Builder(BasicActivity.this).setTitle(getString(R.string.app_name)).setMessage(getString(R.string.basicNotificationMessage)).setIcon(R.drawable.ic_launcher).setNeutralButton(getString(R.string.ButtonOK), null).show();
}
@Override
public void onTick(long millisUntilFinished) {
SimpleDateFormat dateFormat = new SimpleDateFormat("mm:ss");
// dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = new Date(millisUntilFinished);
timer.setText(dateFormat.format(date));
// System.out.println("Timer : " + (millisUntilFinished/1000));
System.out.println("Timer : " + dateFormat.format(date));
}
}
//Sound nach Ablauf der Zeit
public void playSound() {
final MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.ton_timer);
mp.start();
}
}