我正在开发一个 Android 应用程序。抽象地说,这个应用程序有一个 UI 来与用户交互,它还与远程服务交互。远程服务向 Android 启动栏添加通知,此通知允许重新显示 UI。应用程序和服务在同一个包中。服务通知功能代码:
private void showNotification (String contentText) {
Intent intent = new Intent (this, my_app.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mNotification = new Notification.Builder(this)
.setContentIntent(pendingIntent)
.setSmallIcon(ICON)
.setWhen(System.currentTimeMillis())
.setContentTitle(CONTENT_TITLE)
.setContentText(contentText)
.setAutoCancel(false)
.setOngoing(true)
.build();
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
}
注意:它是当前版本的方法...以前我也尝试过以下 FLAGS
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
应用程序清单包含以下配置:
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:allowBackup="true"
android:launchMode="singleTop"
android:uiOptions="splitActionBarWhenNarrow"
android:allowTaskReparenting="true">...</application>
注意:我也尝试了其他礼仪,但这些预计不会产生影响......
当应用程序启动但不再显示时,可以重新显示其使用启动栏通知或使用“应用程序列表”中的应用程序条目,但是当我执行以下场景时,UI 显示两次:
- 从列表启动应用程序
- 主页按钮上的胶带
- (重新)使用通知显示应用程序 ==> UI 正确重新显示
- 主页按钮上的胶带
- (重新)使用“应用程序列表”显示应用程序 ==> 显示一个新的 UI 实例
场景日志:
#start from applications list
I/ActivityManager( 504): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.domain.my_app/.My_App bnds=[40,833][200,1033]} from pid 871
I/my_app( 6915): onCreate
I/my_app::Service( 6930): onCreate
I/my_app( 6915): onResume
#tape the home button
I/my_app( 6915): onPause
#restart from notification in launcher bar
I/ActivityManager( 504): START u0 {flg=0x34400000 cmp=com.domain.my_app/.My_App bnds=[0,102][720,230]} from pid -1
I/my_app( 6915): onResume
#tape the home button
I/my_app( 6915): onPause
#start from application list
I/ActivityManager( 504): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.domain.my_app/.My_App bnds=[40,833][200,1033]} from pid 871
I/my_app( 6915): onCreate
I/my_app( 6915): onResume
#tape the home button
I/my_app( 6915): onPause
#restart from notification in launcher bar
I/ActivityManager( 504): START u0 {flg=0x34400000 cmp=com.domain.my_app/.My_App bnds=[0,102][720,230]} from pid -1
I/PSI Recorder( 6915): onResume
#it is necessary to use two times the my_app quit button in order to exit the application, and then in log, I noted the following error
E/StrictMode( 6915): class com.domain.my_app; instances=2; limit=1
E/StrictMode( 6915): android.os.StrictMode$InstanceCountViolation: class com.domain.my_app; instances=2; limit=1
我阅读了很多关于应用程序、远程服务和通知的内容、提示等,但我无法理解为什么从列表中重新启动应用程序时会重新创建 UI,并且只有在以前,它是从通知中重新显示的......注意:如果 FLAG_ACTIVITY_NEW_TASK 标志未添加到意图,则会记录以下警告,并且不会更改观察到的行为...
W/ActivityManager( 504): startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { flg=0x24400000 cmp=com.domain.my_app/.My_App bnds=[0,102][720,230] }
我将不胜感激。
谢谢你。此致,
编辑:这是我的错,而不是“应用程序”清单字段,android:launchMode =“singleTop”属性应该在“活动”元素中:/
我搬家了,它有效
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:allowBackup="true">
<activity
android:name=".my_app"
android:configChanges="orientation"
android:label="@string/app_name"
android:launchMode="singleTop"