通知和主页按钮

时间:2012-04-19 16:39:37

标签: android notifications android-activity android-homebutton

我写了一个Notification函数来发送notificaiton,当用户点击顶栏上的通知时,它会切换到一个已定义的活动。我的代码做了我想要的。但是有一个问题。

假设有3个活动(A,B,C),当用户点击顶栏上的通知时,它会切换到A.

流程是:
(1)用户打开A
(2)单击A处的按钮并切换到B
(3)单击顶栏上的通知
(4)切换到A(很好)
(5)再次单击A处的按钮并切换到B
(6)按Home键隐藏App
(7)长按主页按钮,然后单击应用程序图标
(8)切换到A(???我应该在活动B上切换到B!)

----当我通过长按主页按钮切换到我的应用程序时,如何避免切换到活动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++;          
    }
}

1 个答案:

答案 0 :(得分:0)

这是“活动”和“任务”之间的区别,如here所述。基本上,它是在后台堆栈中显示的任务,用户可以返回,而不是活动。

您可以尝试在清单中使用“android:exported =”true“”标记活动B.