繁体   English   中英

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

[英]Android Wear - Long Running App loses state

我有一个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);
}

有人可以阐明这个问题,我想念什么? 谢谢!

就我而言,下面的技巧可以解决(已测试/已经在生产中)。

活动-SINGLE_TOP:

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

服务-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”:

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

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

暂无
暂无

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

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