Android countdown timer in background

I have an Android application running CountdownTimer (class CountDownTimer) that is shown on the application screen to show how much time is left to arrive 00:00.

Now my problem is that when I press the home button or when starting another application, the application / timer does not start in the background. Therefore, I suspect that I should do a service, but how? Colleagues told me that I have to do something special when the device is locked so that the timer continues. I want to wake up when it comes to 0.

What is the best way to do this?

+4
source share
3 answers

You must create your class CountDownTimer and extend it from the Service class. Thus, your activity becomes a service, and it works in the background, even if you close the application or screen, it does not matter, it simply performs the task that you assign it to in the background.

+1
source

First, you need to use Handler to control the timer, but check out this great post to see if it helps:

How to set a timer in android

Android Developer Resources also covers this topic:

http://developer.android.com/resources/articles/timed-ui-updates.html

0
source

The easiest way is to use the service with an asynchronous timer. You can download the source code here ( Android countdown timer in the background )

Class of service:

 package com.countdowntimerservice; import android.app.Service; import android.content.Intent; import android.content.SharedPreferences; import android.os.Handler; import android.os.IBinder; import android.preference.PreferenceManager; import android.support.annotation.Nullable; import android.util.Log; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.TimeUnit; public class Timer_Service extends Service { public static String str_receiver = "com.countdowntimerservice.receiver"; private Handler mHandler = new Handler(); Calendar calendar; SimpleDateFormat simpleDateFormat; String strDate; Date date_current, date_diff; SharedPreferences mpref; SharedPreferences.Editor mEditor; private Timer mTimer = null; public static final long NOTIFY_INTERVAL = 1000; Intent intent; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); mpref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); mEditor = mpref.edit(); calendar = Calendar.getInstance(); simpleDateFormat = new SimpleDateFormat("HH:mm:ss"); mTimer = new Timer(); mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 5, NOTIFY_INTERVAL); intent = new Intent(str_receiver); } class TimeDisplayTimerTask extends TimerTask { @Override public void run() { mHandler.post(new Runnable() { @Override public void run() { calendar = Calendar.getInstance(); simpleDateFormat = new SimpleDateFormat("HH:mm:ss"); strDate = simpleDateFormat.format(calendar.getTime()); Log.e("strDate", strDate); twoDatesBetweenTime(); } }); } } public String twoDatesBetweenTime() { try { date_current = simpleDateFormat.parse(strDate); } catch (Exception e) { } try { date_diff = simpleDateFormat.parse(mpref.getString("data", "")); } catch (Exception e) { } try { long diff = date_current.getTime() - date_diff.getTime(); int int_hours = Integer.valueOf(mpref.getString("hours", "")); long int_timer = TimeUnit.HOURS.toMillis(int_hours); long long_hours = int_timer - diff; long diffSeconds2 = long_hours / 1000 % 60; long diffMinutes2 = long_hours / (60 * 1000) % 60; long diffHours2 = long_hours / (60 * 60 * 1000) % 24; if (long_hours > 0) { String str_testing = diffHours2 + ":" + diffMinutes2 + ":" + diffSeconds2; Log.e("TIME", str_testing); fn_update(str_testing); } else { mEditor.putBoolean("finish", true).commit(); mTimer.cancel(); } }catch (Exception e){ mTimer.cancel(); mTimer.purge(); } return ""; } @Override public void onDestroy() { super.onDestroy(); Log.e("Service finish","Finish"); } private void fn_update(String str_time){ intent.putExtra("time",str_time); sendBroadcast(intent); } } 
0
source

All Articles