Question

I have a service with startForeground(requestCode, notification); and have it set so that setOngoing(true) so the user cannot remove notification. How long does this notification stay alive for? Will it stay alive forever (until user restarts phone)?

I have noticed that if I start the service and let the phone sit idle for a few hours that the notification is still present but becomes unresponsive to the button click service methods. Is this the case because I do not have a partial wake lock?

This notification is for controlling android settings like brightness setting which is why I need the notification running always.

Would it be better to use a normal notification with setOngoing(true) and then start a service which would call a method for each button that is clicked?

Was it helpful?

Solution 2

The solution that seems to keep the service/notification running is to use a repeating alarm:

public void startAlarm(){
    alarmMgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, NotificationService.class);
    alarmIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 200*1000, alarmIntent);
}

I am not certain of the optimum frequency for resetting the alarm, but I have set it to a few minutes for now. Every few minutes my service is started/resumed.

OTHER TIPS

Notification will be kept before rebooting phone, I learn it from own experience :).

After some time your service is killed by the system to obtain more memory, thats why notification became unresponsive.

If you want service run always, it is better to return START_STICKY from onStartCommand(). It will be automatically restarted after killing.

I don't know is using of normal notification better, try START_STICKY first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top