簡體   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