Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.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
Java Android Wear-长时间运行的应用程序失去状态_Java_Android_Wear Os_Android Wear 2.0 - Fatal编程技术网

Java Android Wear-长时间运行的应用程序失去状态

Java Android Wear-长时间运行的应用程序失去状态,java,android,wear-os,android-wear-2.0,Java,Android,Wear Os,Android Wear 2.0,我有一个Android Wear(WearOS)应用程序,在运行很长时间后(比如1小时以上),有时会失去状态 失去状态是指在长时间运行后,应用程序会从通知中恢复到“状态a” 状态A:停止-空闲 状态B:运行-收集GPS数据 然而,如果应用程序运行20分钟,它会从通知中正确地恢复到“状态B” 我的服务级别: public int onStartCommand(Intent intent, int flags, int startId) { Notification notificatio

我有一个Android Wear(WearOS)应用程序,在运行很长时间后(比如1小时以上),有时会失去状态

失去状态是指在长时间运行后,应用程序会从通知中恢复到“状态a”

  • 状态A:停止-空闲
  • 状态B:运行-收集GPS数据
然而,如果应用程序运行20分钟,它会从通知中正确地恢复到“状态B”


我的服务级别:

public int onStartCommand(Intent intent, int flags, int startId) {
  Notification notification = this.createNotification();
  super.startForeground(PID, notification);
  return START_STICKY;
}

private Notification createNotification(){
  Log.i("MyService", "createNotification");
  Intent mainIntent = new Intent(this, MyActivity.class);
  mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  PendingIntent pendingIntent = PendingIntent.getActivity(this,0, mainIntent,0);
  // shortened for breviety ... creates notification ...
  return notification;
}
我的活动课:

private void startWatching() {
  if(this.intentService != null){
    this.stopGpsAndCloseDb();
  }
  this.intentService = new Intent(this, MyService.class);
  this.intentService.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
  super.startService(intentService);
}
有人能解释一下这个问题吗?我遗漏了什么?
谢谢

在我的例子中,以下是成功的秘诀(已测试/已投入生产)

活动-单头:

this.intentService = new Intent(this, MyService.class);
this.intentService.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
服务-启动\u粘性和单个\u顶部和操作与类别:

Intent mainIntent = new Intent(this, MyActivity.class);
mainIntent.setAction(Intent.ACTION_MAIN);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mainIntent, 0);
AndroidManifest.xml-launchMode=“singleTop”:



我要感谢@String为我指明了正确的方向。

状态存储在哪里?在每种情况下,logcat都会得到什么?应用程序不会在任何地方存储其状态,我必须这样做吗?我认为应用程序状态是由Android自动管理的。而且,即使使用logcat也很难说会发生什么,因为正如我提到的,问题只是偶尔发生,而且确实很难重现。谢谢如果你的应用程序有它所依赖的状态,它负责存储/加载它-操作系统不会这样做。重读你的问题。。。你是说,有时候当操作系统终止你的进程时,它不会重新启动你的服务吗?附录:作为开始,我会加入存根方法,记录所有
服务
生命周期事件,以了解发生这种情况时会发生什么。嗨@String,我想你已经搞定了!我相信“活动”正在消亡,但“服务”仍在继续。这意味着当我点击通知以恢复“活动”时,它会将其恢复到初始状态。您能否提供一个示例,说明我如何在“服务”上“存储”应用程序状态,以便“活动”返回到正确的状态?谢谢!
<activity
        android:name=".MyActivity"
        android:label="@string/app_name"
        android:launchMode="singleTop">