我有一个Android Wear(WearOS)应用,在运行了很长一段时间(例如1小时以上)有时后,有时失去了状态。
通过丢失状态,我的意思是长时间运行后,该应用会从其通知中恢复为“状态A”。
但是,如果该应用运行了20分钟(假设它运行了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);
}
有人可以阐明这个问题,我想念什么? 谢谢!
答案 0 :(得分:0)
就我而言,以下代码可以解决问题(已测试/已经在生产中)。
活动-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向我指出正确的方向。