点击通知进入当前活动

yea*_*man 11 notifications android

我正在使用这个解决方案:如何使通知意图恢复而不是制定新的意图?

当我正常运行我的应用程序时工作正常..但我的应用程序有共享功能.

当我选择要从图库应用程序共享的图像并在我的活动中打开它时,我在活动中创建通知.我希望当用户点击通知时,它会打开现有活动(由图库应用打开)

问题是,当我点击通知时,它不会恢复该活动,而是打开活动的新实例或之前已打开的另一个实例

编辑:更多解释

我的应用程序名为ShareApp,活动名为ShareActivity,问题是当我通过图库应用程序打开ShareActivity时,会在图库应用程序任务的堆栈顶部创建一个ShareActivity实例.现在,当我创建指向我的ShareActivity的通知时,我使用了intent:

Intent intent = new Intent(this, ShareActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Run Code Online (Sandbox Code Playgroud)

问题是,当我点击通知时,它指向ShareApp任务的堆栈中的ShareActivity实例,而不是gallery app任务的堆栈.

任何想法如何指向正确的任务的堆栈?

编辑2:我的代码

        int defaults = Notification.FLAG_NO_CLEAR;
        NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle(getString(R.string.app_name))
                        .setContentText(text)
                        .setDefaults(defaults);
        Intent intent = new Intent(this, this.getClass());
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        mBuilder.setContentIntent(pendingIntent);
        NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(_ID, mBuilder.build());
Run Code Online (Sandbox Code Playgroud)

编辑3:adb shell dumpsys活动(在应用David代码之后/sf/answers/1183112241/)

我的应用程序名为shareApp,我的活动是ShareActivity

在点击通知之前:

Main stack:
    TaskRecord{41965708 #6 A com.android.gallery3d}
    Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.gallery3d/.app.Gallery bnds=[364,50][108,108]}
      Hist #2: ActivityRecord{417ccc28 com.yeahman.shareapp/.ShareActivity}
        Intent { act=android.intent.action.SEND_MULTIPLE typ=image/* cmp=com.yeahman.shareapp/.ShareActivity (has extras) }
        ProcessRecord{41c868d0 6088:com.yeahman.shareapp/10116}
      Hist #1: ActivityRecord{4135e540 com.android.gallery3d/.app.Gallery}
        Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.gallery3d/.app.Gallery bnds=[364,50][108,108] }
Run Code Online (Sandbox Code Playgroud)

点击通知后:

 Main stack:
    TaskRecord{41965708 #6 A com.android.gallery3d}
    Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.gallery3d/.app.Gallery bnds=[364,50][108,108]}
      Hist #3: ActivityRecord{4169f358 com.android.gallery3d/.app.Gallery}
        Intent { flg=0x20000000 cmp=com.android.gallery3d/.app.Gallery bnds=[0,205][480,301] }
        ProcessRecord{4175dd28 5808:com.android.gallery3d/10036}
      Hist #2: ActivityRecord{417ccc28 com.yeahman.shareapp/.ShareActivity}
        Intent { act=android.intent.action.SEND_MULTIPLE typ=image/* cmp=com.yeahman.shareapp/.ShareActivity (has extras) }
        ProcessRecord{41c868d0 6088:com.yeahman.shareapp/10116}
      Hist #1: ActivityRecord{4135e540 com.android.gallery3d/.app.Gallery}
        Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.gallery3d/.app.Gallery bnds=[364,50][108,108] }
Run Code Online (Sandbox Code Playgroud)

Has*_*sud 27

    Notification.Builder mBuilder =
            new Notification.Builder(this)
            .setSmallIcon(R.drawable.cmplayer)
            .setContentTitle("CoderoMusicPlayer")
            .setContentText("PLayer0!");

    Intent resultIntent = new Intent(this, AndroidBuildingMusicPlayerActivity.class);
    resultIntent.setAction(Intent.ACTION_MAIN);
    resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            resultIntent, 0);

    mBuilder.setContentIntent(pendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());
Run Code Online (Sandbox Code Playgroud)

只需复制代码并将其粘贴到主启动器活动中即可.

  • Welcome.Happy Coding. (2认同)
  • 非常简单。您可以在任务栏中显示应用程序的图标和说明。如果有人单击任务栏上的图标,他将返回在后台运行的当前活动。 (2认同)
  • 美丽没想到它会起作用!这是两行“动作和类别” (2认同)

Dav*_*ser 7

如果您ShareActivity在不同的任务中有多个活动实例,则无法通过启动您的活动来告知Android要重新激活哪个任务.听起来像你想做的事情,在这种情况下,不是启动你的活动,而是启动Gallery应用程序.如果Gallery应用程序已在运行且您的活动位于任务堆栈的顶部,那么您需要做的就是将Gallery应用程序置于前台.为此,请为Gallery应用程序创建"启动Intent",并确保Intent.FLAG_ACTIVITY_NEW_TASK在Intent 上设置.您应该可以PackageManager通过使用获得"启动意图"getLaunchIntentForPackage()

编辑:使用ActivityManager的示例代码

你可以试试这个:

// Get the root activity of the task that your activity is running in
ActivityManager am = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
ActivityManager.RunningTaskInfo task = tasks.get(0); // Should be my task
ComponentName rootActivity = task.baseActivity;

// Now build an Intent that will bring this task to the front
Intent intent = new Intent();
intent.setComponent(rootActivity);
// Set the action and category so it appears that the app is being launched
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
Run Code Online (Sandbox Code Playgroud)

注意:您需要GET_TASKS获得此许可.我没有试过这个,不能保证它会起作用.getRunningTasks()文档不鼓励使用,但这并不意味着它不适合您的目的.

编辑为意图添加了动作/类别

注意:我实际上构建了一个小应用程序来测试它.我需要将ACTION = MAIN和CATEGORY = LAUNCHER添加到Intent中(如果你使用它会在那里PackageManager.getLaunchIntentForPackage().我有一个HTC One S,所以在我的设备上实际上调用了"Gallery"应用程序com.htc.album/.AlbumMain.ActivityMainDropList,但这仍然应该为你工作.


Ken*_*olf 5

android:launchMode="singleTop"
Run Code Online (Sandbox Code Playgroud)

在您manifest.xml的主要活动中,看看是否有帮助。