Issue
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?
Solution
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.
Answered By - JunaidS
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.