Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.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 当我的前台服务在引导时启动时,TouchWiz停止并重新启动_Android_Android Intent_Android Service_Android Notifications - Fatal编程技术网

Android 当我的前台服务在引导时启动时,TouchWiz停止并重新启动

Android 当我的前台服务在引导时启动时,TouchWiz停止并重新启动,android,android-intent,android-service,android-notifications,Android,Android Intent,Android Service,Android Notifications,我是android开发初学者,以下是我的情况: 我正在实现一个前台服务,它可以从我的应用程序启动/停止 在我的设备上进行测试之前,一切都正常工作,每次启动完成后重新启动服务时,TouchWiz都会失败并重新启动 编辑:现在,当设备重新启动时,尽管服务根本没有运行,服务仍会启动(可能是我必须存储一些布尔变量,以便在接收引导完成时采取适当的操作) 我在这里遗漏了一些重要的东西,但我想不出来。 任何想法,可能是我错过了一些额外的权限,或者至少我如何从我的android设备日志中获得TouchWiz失败

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

我正在实现一个前台服务,它可以从我的应用程序启动/停止

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

编辑:现在,当设备重新启动时,尽管服务根本没有运行,服务仍会启动(可能是我必须存储一些布尔变量,以便在接收引导完成时采取适当的操作)

我在这里遗漏了一些重要的东西,但我想不出来。 任何想法,可能是我错过了一些额外的权限,或者至少我如何从我的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);
        }
}}
MyService

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
更新
该问题仅在“重新启动”时出现,而不是在我关闭-启动设备时出现。

So;问题刚刚消失,我唯一改变的是通知id和PendingEvent id是相同的。我不知道为什么这个问题一开始就出现了,我一直在继续,但这让我担心,我对它的起源很好奇

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

不管怎样,我一直坚持这个问题,直到有一天有人遇到同样的奇怪情况


感谢您的建议

是否存在任何logcat错误?你能发布你的代码吗?我正在真实设备上测试,使用android studio,我现在正在编辑我的问题。如果有错误,你会有logcat错误消息。请检查它。logcat上没有任何内容,因为关机时设备断开连接!!将日志写入文件。。。