Setting timer in android based on date in database

I am new to android. I have an application that should display data stored in a database (this is a time value, for example 40 hours) in text form.

I want to count the value from 40 (in my case) to zero, and the alarm should start when it reaches zero.

need to show countdown like image

+4
source share
2 answers

just convert the remaining time in the second (get the hour value from db and convert it to the second, in my case I get the time in seconds)

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); } } 
0
source

You can use CountDownTimer to count the value as follows:

  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."); } } 

For part of the alarm you can use AlarmManager . Below is a tutorial .

Hope this helps!

0
source

Source: https://habr.com/ru/post/1441725/


All Articles