私はアンドロイドに不慣れです。データベースに保存されているデータ(時間値、たとえば40時間)をテキストビューで表示する必要があるアプリケーションがあります。
値を40(私の場合)からゼロまでカウントダウンしたいのですが、ゼロに達するとアラームが開始されます。
私はアンドロイドに不慣れです。データベースに保存されているデータ(時間値、たとえば40時間)をテキストビューで表示する必要があるアプリケーションがあります。
値を40(私の場合)からゼロまでカウントダウンしたいのですが、ゼロに達するとアラームが開始されます。
CountDownTimer
次のように、値をカウントダウンするためにaを使用できます。
final MyCounter timer = new MyCounter(600000, 1000); //add your time
...
public class MyCounter extends CountDownTimer
{
public MyCounter(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish()
{
Log.i("debug","Timer Completed");
}
@Override
public void onTick(long millisUntilFinished)
{
tv.setText("Timer : " + (millisUntilFinished/60000) + " " + "minutes remaining.");
}
}
アラーム部分には、AlarmManagerを使用できます。これは同じためのチュートリアルです。
それが役に立てば幸い!
残り時間を秒で変換するだけです(dbから時間の値を取得して秒に変換します。私の場合は秒で時間を取得しています)
public class CountDownActivity extends Activity {
int time,initStart,startPointTime,hh,mm,ss,millis;
TextView txtSecond;
TextView txtMinute;
TextView txtHour;
TextView txtDay;
Handler handler;
long seconds =40*60*60;
Runnable updater;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
txtSecond=(TextView)findViewById(R.id.cntSecond);
txtMinute=(TextView)findViewById(R.id.cntMinute);
txtHour=(TextView)findViewById(R.id.cntHour);
txtDay=(TextView)findViewById(R.id.cntDay);
handler= new Handler();
initStart = (int) SystemClock.elapsedRealtime();
updater = new Runnable() {
public void run() {
int sec,minute,hour,day;
long diff = seconds;
System.out.println(diff);
if (diff >= 1) {
sec = (int) (diff%60);
} else {
sec=00;
}
txtSecond.setText("" +sec);
diff = diff/60;
System.out.println(diff);
if (diff >= 1) {
minute = (int) (diff%60);
} else {
minute=00;
}
txtMinute.setText("" +minute);
diff = diff/60;
if (diff >= 1) {
hour = (int) (diff%24);
} else {hour = 00; }
txtHour.setText("" +hour);
diff = diff/24;
if (diff >= 1) {
day = (int) diff;
} else { day =00; }
txtDay.setText("" +day);
seconds=seconds-1;
handler.postDelayed(this, 1000);
}
};
handler.post(updater);
}
> //and don't forget to removeCallback on destroy
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
handler.removeCallbacks(updater);
}
}