1

我正在开发一个 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 显示两次:

  1. 从列表启动应用程序
  2. 主页按钮上的胶带
  3. (重新)使用通知显示应用程序 ==> UI 正确重新显示
  4. 主页按钮上的胶带
  5. (重新)使用“应用程序列表”显示应用程序 ==> 显示一个新的 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"
4

1 回答 1

2

我认为问题的根源是您的意图标志组合。为了帮助您调试它,请尝试在您的活动中实现onNewIntent()方法,并记录它收到的每个意图,如下所示:

protected void onNewIntent(Intent intent){
    super.onNewIntent(intent);
    Log.i("my_app", "New intent with flags "+intent.getFlags());
}

这应该允许您在日志中查看导致第二次调用 onCreate() 的标志。

至于 StrictMode 实例计数违规,我认为不值得调查这里解释。

编辑 尝试仅使用标志 Intent.FLAG_ACTIVITY_NEW_TASK,例如,删除其他三个标志。此外,将 android:launchMode 保持为 singleTop。

于 2013-05-31T13:09:33.447 回答