简体   繁体   中英

How to restart sticky service after power saver kills it on android

I tried by setting android:isolatedProcess="true" but it's not working

actually I want to show a permanent notification all time

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)

.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), resourceIcon/*R.mipmap.ic_launcher*/))
                .setSmallIcon(R.drawable.ic_icon_flo_not)
                .setContentTitle(title)
                .setOngoing(onGoing)
                .setAutoCancel(autoCancelable)
                .setPriority(priority)
                .setContentText(message);
        Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        if (playSound)
            mBuilder.setSound(soundUri);
        if (remoteViews == null) {
            // Creates an explicit intent for an Activity in your app
            Intent resultIntent = new Intent(context, resultClass);
            resultIntent.setAction(action);

            // The stack builder object will contain an artificial back stack for the
            // started Activity.
            // This ensures that navigating backward from the Activity leads out of
            // your application to the Home screen.
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            // Adds the back stack for the Intent (but not the Intent itself)
            stackBuilder.addParentStack(MainActivity.class);
            // Adds the Intent that starts the Activity to the top of the stack
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
            mBuilder.setContentIntent(resultPendingIntent);
        } else {
            mBuilder.setContent(remoteViews);
        }

Override onTaskRemoved() in your service and use alarm manager to start the service again. Below is code .

    @Override
public void onTaskRemoved(Intent rootIntent) {
    super.onTaskRemoved(rootIntent);

    Log.d(TAG, "TASK REMOVED");

    PendingIntent service = PendingIntent.getService(
            getApplicationContext(),
            1001,
            new Intent(getApplicationContext(), MyService.class),
            PendingIntent.FLAG_ONE_SHOT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000, service);
}  

Service.START_STICKY service are those which are restarted if they are terminated due to some reason.You just need to return Service.START_STICKY from onStartCommand

public int onStartCommand(Intent intent, int flags, int startId) { return START_STICKY; }

You just need to do above, There is no separate thing that you have to do

The service does get re-created, not re-started. If you override the onCreate and do a Log.d or a Toast, you will see that onCreate gets called after your activity and app is destroyed.

So the trick to keep it running after it is re-created is to do your code on the onCreate method and use the onStartCommand just to return START_STICKY .


Another thing to notice (and since I can't comment, I'd like to add to Abdul Kawee answer with the AlarmManager ): It is SOLVED on api27: all you have to do is move your super.onTaskRemoved(rootIntent) to be the last line of code.

I have found that destructive methods should only call super in the end of the overriden method, otherwise some resources are no longer available in runtime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM