私はこのソリューションを使用しています:新しいインテントを作成するのではなく、通知インテントを再開するにはどうすればよいですか?
アプリを正常に実行すると正常に動作しますが、アプリには共有機能があります。
ギャラリー アプリから共有したい画像を選択してアクティビティで開くと、アクティビティに通知が作成されます。ユーザーが通知をクリックすると、既存のアクティビティが開きます(ギャラリーアプリによって開かれます)
問題は、通知をクリックすると、そのアクティビティが再開されず、代わりにアクティビティの新しいインスタンスまたは以前に開かれた別のインスタンスが開かれることです
編集:より多くの説明
私のアプリは ShareApp と呼ばれ、アクティビティは ShareActivity と呼ばれます。問題は、ギャラリー アプリを介して ShareActivity を開くと、ShareActivity のインスタンスがギャラリー アプリ タスクのスタックの一番上に作成されることです。ShareActivity を指す通知を作成するときは、次のインテントを使用します。
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と呼ばれ、私のアクティビティは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] }