gpt4 book ai didi

android - 单击通知以转到当前 Activity

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:52:45 25 4
gpt4 key购买 nike

我正在使用这个解决方案:How to make notification intent resume rather than making a new intent?

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

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

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

编辑:更多解释

我的应用名为 ShareApp, Activity 名为 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);

问题是,当我点击通知时,它指向 ShareApp 任务堆栈中的 ShareActivity 实例,而不是图库应用程序任务堆栈中的实例。

知道如何指向正确的任务堆栈吗??

编辑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());

编辑 3:adb shell dumpsys Activity (应用 David https://stackoverflow.com/a/16901603/1334268 的代码后)

我的应用名为 shareApp,我的 Activity 是 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] }

点击通知后:

 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] }

最佳答案

    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());

只需复制代码并将其粘贴到您的主要启动器 Activity 中。

关于android - 单击通知以转到当前 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16885706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com