Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android startForeground在活动关闭后重新启动_Android_Service_Android Service - Fatal编程技术网

Android startForeground在活动关闭后重新启动

Android startForeground在活动关闭后重新启动,android,service,android-service,Android,Service,Android Service,我目前正在尝试在前台服务上执行一些代码。 我的手机在安卓6和安卓7上一切正常。 但当我在安卓5设备上尝试时,当活动被破坏时,服务被终止 在调用bindService之前,我正在手动启动服务。 我在onStart方法上调用bindService,在onStop方法上调用unbindService 这是我的服务 public class ExecutionService extends Service { private final IBinder binder = new Executio

我目前正在尝试在前台服务上执行一些代码。 我的手机在安卓6和安卓7上一切正常。 但当我在安卓5设备上尝试时,当活动被破坏时,服务被终止

在调用bindService之前,我正在手动启动服务。 我在onStart方法上调用bindService,在onStop方法上调用unbindService

这是我的服务

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;
    }

}
我注意到onDestroy方法从未被调用,但onCreate方法被调用了两次

我如何让它在安卓5设备上工作?
谢谢您的帮助。

尝试删除此方法

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

最后我发现了问题所在。我正在对活动的onDestroy中的null对象调用一个方法。然后服务崩溃了(只有安卓?)。
我没有在控制台中发现错误,因为应用程序正在重新启动。

我想这里发生了其他事情。。。早在API级别17时,我就使用了前台服务,这防止了服务的意外破坏(这是应该的)。您确定
startForeground()
正在执行,而
stopForeground()
没有执行吗?请接受您自己的答案,以便将问题从未回答的问题列表中删除。