简体   繁体   English

Android Wear-长时间运行的应用丢失状态

[英]Android Wear - Long Running App loses state

I have an Android Wear (WearOS) app that after running for a very long period of time ( say 1 hour plus ) sometimes loses its state. 我有一个Android Wear(WearOS)应用程序,该应用程序运行了很长一段时间(比如说超过1个小时)后, 有时会失去其状态。

By losing its state I mean after a long time running the app resumes in "State A" from its notification. 通过丢失其状态,我的意思是长时间运行后,该应用会从其通知中恢复为“状态A”。

  • State A: Stopped - idle. 状态A:已停止-空闲。
  • State B: Running - collecting GPS data. 状态B:运行-收集GPS数据。

However, if the app runs for let's say 20 mins it resumes rightly in "State B" from its notification. 但是,如果该应用程序运行了20分钟,则它会从通知中的“状态B”中正确恢复。


My Service Class: 我的服务等级:

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

My Activity Class: 我的活动课:

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

Could someone shed some light into this problem, what am I missing? 有人可以阐明这个问题,我想念什么? Thanks! 谢谢!

In my case, the following below did the trick ( tested / already in production ). 就我而言,下面的技巧可以解决(已测试/已经在生产中)。

Activity - SINGLE_TOP : 活动-SINGLE_TOP:

this.intentService = new Intent(this, MyService.class);
this.intentService.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

Service - START_STICKY & SINGLE_TOP & Action & Category: 服务-START_STICKY和SINGLE_TOP及操作和类别:

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" : AndroidManifest.xml-launchMode =“ singleTop”:

<activity
        android:name=".MyActivity"
        android:label="@string/app_name"
        android:launchMode="singleTop">

I would like to thank @String for pointing me in the right direction. 我要感谢@String为我指出正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM