3

I've a sticky background service which has to run all the time. Basically it keeps track of the time, its a custom clock timer. But once the device goes to idle state (when phone screen is turned off) the timer (background service) also gets paused.

What should I do to make it always keep running even when the screen is turned off?

    public class TimerService extends Service {
    private Context context;
    @Override
        public IBinder onBind(Intent intent) {
            Logger.LogI(TAG, "Service binding");
            return null;
        }

      @Override
        public void onCreate() {
            super.onCreate();
            context = getApplicationContext();
        }

     @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
       return Service.START_STICKY;
        }
    }
Ahmad Shahwaiz
  • 1,432
  • 1
  • 17
  • 35

2 Answers2

9

So there were some ways for a service not to be killed when device goes in sleep mode. I started my service as a foreground service attached with the notification. I don't say that its the better approach for a long term service. But of-course this solution is open for more optimization. This solution doesn't get the service go in pause state while in sleep mode.

Following is the whole service code:

public class TimerService extends Service {
    private ScheduledExecutorService scheduleTaskExecutor;
    private Context context;
    @Override
    public IBinder onBind(Intent intent) {
        Logger.LogI(TAG, "Service binding");
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        setNotification();
        if (scheduleTaskExecutor == null) {
            scheduleTaskExecutor = Executors.newScheduledThreadPool(1);
            scheduleTaskExecutor.scheduleAtFixedRate(new mainTask(), 0, 1, TimeUnit.SECONDS);
        }
        return Service.START_STICKY;
    }

    public void setNotification() {
        PendingIntent contentIntent;
        contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, Main_Activity.class), 0);

        NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.notification_icon)
            .setColor(this.getResources().getColor(R.color.action_bar_color))
            .setContentTitle("MainActivity")
            .setOngoing(true)
            .setAutoCancel(false)
            .setContentText("MyApp");
        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

        Notification notification = mBuilder.build();
        startForeground(NOTIFICATION_ID, notification); //NOTIFICATION_ID is a random integer value which has to be unique in an app
    }
}
private class mainTask implements Runnable {

    public void run() {
        // 1 Second Timer
    }
}

Helpful Links:

Run a service in the background forever..? Android

How to run background service after every 5 sec not working in android 5.1?

How to run a method every X seconds

How to make service run even in sleep mode?

part-1 persistent foreGround android service that starts by UI, works at sleep mode too, also starts at phone restart

part-2 persistent foreGround android service that starts by UI, works at sleep mode too, also starts at phone restart

Android - implementing startForeground for a service?

Community
  • 1
  • 1
Ahmad Shahwaiz
  • 1,432
  • 1
  • 17
  • 35
1

If you want make your service as never ending service then User this code in onStartCommand

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
     //Put code here
    return START_REDELIVER_INTENT;

}

You can use START_STICKY also.

samsad
  • 1,241
  • 1
  • 10
  • 15
  • The case didn't reproduce itself in my already written code. I'm already using Start_Sticky service. The main difference between this and start_redeliver_intent is that in start_redeliver_intent service the previous intent is again sent when service is restarted. I don't see how start_redeliver_intent would pause the service when it goes in idle state. I will wait for it to reproduce and then I'll use your solution if it work then I'll mark your answer. – Ahmad Shahwaiz Jan 10 '17 at 12:01
  • So, the case reproduced when I "detached" the wire which was attached for debugging. Your solution didn't work. The service still gets pause when the phone went to sleep mode. Primarily, it shuts down the CPU. Along the way, non-essential radios (WiFi, GPS) will have been shut down as well. – Ahmad Shahwaiz Jan 11 '17 at 13:40