Android Wear - Long 运行 应用丢失状态
Android Wear - Long Running App loses state
我有一个 Android Wear (WearOS) 应用程序,在 运行 之后很长 一段时间(比如 1 小时以上)有时 失去它的状态。
失去它的状态我的意思是在 运行 很长一段时间后,应用程序从它的通知中 "State A" 恢复。
- 状态 A:停止 - 空闲。
- 状态 B:运行 - 收集 GPS 数据。
但是,如果应用程序运行了 20 分钟,它会根据通知在 "State B" 中正确恢复。
我的服务 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;
}
我的ActivityClass:
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);
}
有人可以阐明这个问题吗,我错过了什么?
谢谢!
在我的例子中,下面的代码成功了(经过测试/已经在生产中)。
Activity - 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 - 启动模式="singleTop" :
<activity
android:name=".MyActivity"
android:label="@string/app_name"
android:launchMode="singleTop">
感谢@String 为我指明了正确的方向。
我有一个 Android Wear (WearOS) 应用程序,在 运行 之后很长 一段时间(比如 1 小时以上)有时 失去它的状态。
失去它的状态我的意思是在 运行 很长一段时间后,应用程序从它的通知中 "State A" 恢复。
- 状态 A:停止 - 空闲。
- 状态 B:运行 - 收集 GPS 数据。
但是,如果应用程序运行了 20 分钟,它会根据通知在 "State B" 中正确恢复。
我的服务 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;
}
我的ActivityClass:
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);
}
有人可以阐明这个问题吗,我错过了什么? 谢谢!
在我的例子中,下面的代码成功了(经过测试/已经在生产中)。
Activity - 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 - 启动模式="singleTop" :
<activity
android:name=".MyActivity"
android:label="@string/app_name"
android:launchMode="singleTop">
感谢@String 为我指明了正确的方向。