with - using timer in android

Android count down timer in background (6)

Minimal working example based on https://stackoverflow.com/a/14295193/895245 :

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

public class Main extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        final int i = 0;
        final TextView textView = new TextView(this);
        textView.setText(String.format("%d", i));
        final Handler handler = new Handler();
        class MyRunnable implements Runnable {
            private Handler handler;
            private int i;
            private TextView textView;
            public MyRunnable(Handler handler, int i, TextView textView) {
                this.handler = handler;
                this.i = i;
                this.textView = textView;
            public void run() {
                this.handler.postDelayed(this, 500);
                this.textView.setText(String.format("%d", i));
        handler.post(new MyRunnable(handler, i, textView));

You can just copy paste this into the main activity generated by android create project [...], and you will see a counter on your app.

Tested on Android 22.

I have an Android app with manage a countdowntimer (class CountDownTimer) that it is shown in app screen, to show how much time left to arrive 00:00.

My problem now is that when I press home button or when I launch another app the app/timer does not run in the background. So I suspect I must do a service, but how? Colleagues have said to me that I must do something special for when the device gets locked in order for the timer to continue. I want to wake up when arrived to 0.

What is the best way to do this?

As I have seen it, java.util.Timer is the most used for implementing a timer.

For a repeating task:

new Timer().scheduleAtFixedRate(task, after, interval);

For a single run of a task:

new Timer().schedule(task, after);

task being the method to be executed
after the time to initial execution
(interval the time for repeating the execution)

The easiest way is use the service with timer async. You can download the source from here (Android Countdown Timer Run In Background)

Service Class:

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;

public IBinder onBind(Intent intent) {
    return null;

public void 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 {

    public void run() {
        mHandler.post(new Runnable() {

            public void run() {

                calendar = Calendar.getInstance();
                simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
                strDate = simpleDateFormat.format(calendar.getTime());
                Log.e("strDate", strDate);




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);

        } else {
            mEditor.putBoolean("finish", true).commit();
    }catch (Exception e){


    return "";


public void onDestroy() {
    Log.e("Service finish","Finish");

private void fn_update(String str_time){


You must create your CountDownTimer class and extend it from Service class. By doing so your activity becomes a service and it runs in the background even if you close the application or the screen it doesnt matter it just does the task that you assign to it in the background .

How to set a timer in android

Standard Java way to use timers via java.util.Timer and java.util.TimerTask works fine in Android, but you should be aware that this method creates a new thread.

You may consider using the very convenient Handler class (android.os.Handler) and send messages to the handler via sendMessageAtTime(android.os.Message, long) or sendMessageDelayed(android.os.Message, long). Once you receive a message, you can run desired tasks. Second option would be to create a Runnable object and schedule it via Handler's functions postAtTime(java.lang.Runnable, long) or postDelayed(java.lang.Runnable, long).

How to update a TextView in an activity constantly in an infinite loop?

use Handler and a separate thread/runnable for updating TextView constantly instead of While loop :

Handler handler=new Handler();
handler.post(new Runnable(){ 
    public void run() {
        // upadte textView here
        handler.postDelayed(this,500); // set time here to refresh textView