当我的前台服务在 Boot 上启动时,TouchWiz 停止并重新启动

TouchWiz stops and restarts when my foreground service starts on Boot

我是 android 开发初学者,这是我的情况 :

我正在实施一个前台服务,可以从我的应用程序 started/stopped。

在我的设备上进行测试之前一切正常,每次在启动完成时重新启动服务时,TouchWiz 都会失败并重新启动。

EDIT :现在当设备重新启动时,尽管事实上根本不是 运行 服务也启动了(可能是我必须存储一些布尔变量在接收引导完成时采取适当的操作)

我在这里遗漏了一些重要的东西,但无法弄清楚。 任何想法,可能是我缺少的一些额外权限,或者至少我如何才能从我的 android 设备日志中了解 TouchWiz 失败时发生的事情。

收件人代码:

public class MyServiceReceiver extends BroadcastReceiver {
//Ctor:
public MyServiceReceiver() {super();}
//Receive Boot Action:
@Override
public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            Intent restartServiceIntent = new Intent(context,MyService.class);
            context.startService(restartServiceIntent);
        }
}}

我的服务

public class MyService extends Service {
//ctor:
public MyService() {}
//On Create Service:
@Override
public void onCreate() {
    super.onCreate();
    pNotification = setNotification();
    isRunning = false;
    this.startForeground(SERVICE_NOTIFICATION_ID,pNotification);
}

//Notifications:
public static final String SERVICE_NOTIFICATION_TAG = "My_SERVICE";
public static final int SERVICE_NOTIFICATION_ID = 22101982;
private Notification pNotification;
private Notification setNotification(){
    NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(MyApplication.getMyAppContext());
    notifBuilder.setAutoCancel(false);
    notifBuilder.setContentTitle(getResources().getString(R.string.service_notification_title));
    notifBuilder.setContentText(getResources().getString(R.string.service_notification_message));
    notifBuilder.setSmallIcon(R.drawable.ic_action_record);
    Intent mainIntent = new Intent(this,MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,SERVICE_NOTIFICATION_ID,mainIntent,0);
    notifBuilder.setContentIntent(pendingIntent);
    return notifBuilder.build();
}

public static boolean IsRunning(){return isRunning;}
private static boolean isRunning;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    //TODO:Initialisations
    isRunning = true;
    return START_STICKY;
}

//On Destroy Service:
@Override
public void onDestroy() {
    super.onDestroy();
    isRunning = false;
}

@Override
public IBinder onBind(Intent intent) {return null;}}//My service

更新 该问题仅在 "Reboot" 上出现,但在我关闭 - 启动设备时不会出现。

所以;问题就消失了,我唯一改变的是通知 id 和 PendingIntent id 是一样的..我不知道为什么这个问题首先出现,我继续前进,但这让我担心,我很好奇它的起源.

我认为这是来自服务显示正在与 touchwiz 启动序列或类似内容竞争的通知。

无论如何我都会坚持这个问题,直到有一天有人遇到同样奇怪的情况。

谢谢你的建议