使用启动器标志获取活动中的待处理意图

时间:2015-12-17 17:58:45

标签: android android-intent notifications

在我的应用中,我会在某些情况下显示通知,当用户点击通知时,它会通过此代码导航到应用:

Intent onNotificationClicked = new Intent(G.context, MainActivity.class);
onNotificationClicked.putExtra("DO", "clicked");
onNotificationClicked.setAction(Intent.ACTION_MAIN);
onNotificationClicked.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent ponNotificationClicked = PendingIntent.getActivity(ctx, 113, onNotificationClicked, PendingIntent.FLAG_UPDATE_CURRENT);
this.contentIntent = ponNotificationClicked;

现在在我的MainActivity中我希望得到这个意图的额外内容。但这是不可用的,因为这个意图不会创建一个新的活动,只是将已启动的活动带到前面。所以如何在我的应用程序中处理这种意图

谢谢

1 个答案:

答案 0 :(得分:1)

我找到了答案:

在您的活动中设置清单:

android:launchMode="singleTask"

然后对于你的未决意图你应该这样设置:

Intent onNotificationClicked = new Intent(G.context, MainActivity.class);
onNotificationClicked.putExtra("DO", "clicked");
PendingIntent ponNotificationClicked = PendingIntent.getActivity(ctx, 113, onNotificationClicked, PendingIntent.FLAG_UPDATE_CURRENT);
this.contentIntent = ponNotificationClicked;

现在在您的活动中覆盖onNewIntent并在其中编写您的函数:

 @Override
protected void onNewIntent(Intent intent) {

    String action = intent.getStringExtra("DO");

    if (action != null) {
        Log.d("this is creating", "shiva said right");
        mSlidingLayout.setPanelState(SlidingUpPanelLayout.PanelState.EXPANDED);

    }
    super.onNewIntent(intent);
}