我编写了一个通知函数来发送通知,当用户单击顶部栏上的通知时,它将切换到定义的活动。我的代码做了我想要的。但有一个问题。
假设有 3 个活动(A、B、C),当用户单击顶部栏上的通知时,它会切换到 A。
流程是:
(1)用户打开A
(2)点击A处的按钮切换到B
(3)点击顶栏的通知
(4)切换到A(没问题)
(5)再次点击a A 上的按钮并切换到 B
(6) 按 Home 按钮隐藏应用程序
(7) 长按 Home 按钮并单击应用程序图标
(8) 切换到 A (??? 它应该切换到 B 因为我在 Activity乙!)
---- 长按 Home 键切换到 App 时,如何避免切换到 Activity A?谢谢!
这是代码片段:
int notificationCounter = 0;
private void displayNotificationMessage(String title, String message, int notification_id, String fromUser, boolean vibrate, boolean playSound, String notificationActionStr)
{
if (receiverMessageBroadcast || !currentTargetUser.equals(fromUser))
{
Intent intent = new Intent();
intent.setClass(this, MainActivity.class);
//intent.setAction(notificationAction + "_" + System.currentTimeMillis());
Bundle bundle = new Bundle();
bundle.putInt("notificationAction", 1);
bundle.putString("targetUser", fromUser);
intent.putExtras(bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
Notification notification = new Notification(R.drawable.notification, message, System.currentTimeMillis());
notification.setLatestEventInfo(this, title, message, PendingIntent.getActivity(this.getBaseContext(), notificationCounter, intent, PendingIntent.FLAG_UPDATE_CURRENT));
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
if (playSound)
notification.defaults |= Notification.DEFAULT_SOUND;
if (vibrate)
notification.vibrate=new long[] {100L, 100L, 200L, 500L};
notification.ledARGB = Color.WHITE;
notification.ledOnMS = 1500;
notification.ledOffMS = 1500;
notificationMgr.notify(fromUser, 0, notification);
notificationCounter++;
}
}