简体   繁体   中英

Android startForeground get restarted after activity close

I'm currently trying to execute some code on a foreground service. Everything is working on my phone on android 6 and android 7. But when I try it with an android 5 device the service is killed when the activity is destroyed.

I'm starting the service manually before calling bindService. I'm calling bindService on the onStart and unbindService on the onStop method.

Here is my service

public class ExecutionService extends Service {
    private final IBinder binder = new ExecutionBinder();
    private static final int NOTIFICATION_ID = 1;
    private static final String STOP_ACTION = "STOP_ACTION";

    private Notification.Builder builder;
    private boolean timerHeaderVisible;
    private NotificationManager nm;
    private Intent notificationIntent;
    private PendingIntent contentIntent;

    public class ExecutionBinder extends Binder {
        public ExecutionService getService() {
          return ExecutionService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        builder = new Notification.Builder(this);

        Intent stopSelf = new Intent(this, ExecutionService.class);
        stopSelf.setAction(STOP_ACTION);
        PendingIntent closePendingIntent = PendingIntent.getService(this, 0, stopSelf,PendingIntent.FLAG_CANCEL_CURRENT);

        builder.setSmallIcon(R.drawable.ic_time)
                .setContentTitle(getString(R.string.app_name))
                .addAction(R.drawable.ic_stop, getString(R.string.stop), closePendingIntent)
                .setVibrate(new long[]{0L});

        Notification notification = builder.build();
        notification.flags |= Notification.FLAG_NO_CLEAR;
        startForeground(NOTIFICATION_ID, notification);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        if(notificationManager != null)
            notificationManager.cancelAll();
        isStarted = false;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(intent != null && intent.getAction() != null && intent.getAction().equals(STOP_ACTION)) {
            stopForeground(true);
            stopSelf();
        }

        return START_STICKY;
    }

}

I have noticed that the onDestroy method is never called but the onCreate method is called twice.

How can I make it work on android 5 devices? Thanks for your help.

try to delete this method

 @Override
    public void onDestroy() {
        super.onDestroy();
        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        if(notificationManager != null)
            notificationManager.cancelAll();
        isStarted = false;
    }

Finally I found what was wrong. I was calling a method on a null object in the onDestroy of the activity. Then the service crashed (only with android ?). I was not seing the error in the console because the app was restarting.

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