如果活动已在运行,则通过单击具有待处理意图的通知来启动活动时将调用哪个函数

时间:2015-01-06 07:35:27

标签: android android-intent android-pendingintent

我想在用户点击通知时打开一个活动。我可以使用挂起的意图来做到这一点,即使活动已经运行也可以正常工作。但我想在点击操作上做一些事情。现在我在onCreate方法中这样做。它仅在活动未运行时有效。我尝试使用onNewIntent方法,但未达到上述条件。我现在能做什么?

2 个答案:

答案 0 :(得分:1)

NOTIFICATION_ID =>用于标识进一步操作的通知的数字。 mContext =>应用背景。 ResultActivity =>要开启的活动。

    //creating intent
    Intent resultIntent = new Intent(mContext, ResultActivity.class);
    resultIntent.putExtra("update_request", true);
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    //creating pending intent
    PendingIntent resultPendingIntent =
        PendingIntent.getActivity(
        mContext,
        0,
        resultIntent,
        PendingIntent.FLAG_UPDATE_CURRENT
    );

    //Build the notification using Notification.Builder
    Notification.Builder builder = new Notification.Builder(mContext)
    .setSmallIcon(R.drawable.icon)
    .setAutoCancel(true)
    .setContentTitle("Title")
    .setContentIntent(resultPendingIntent)  //adding pending intent
    .setContentText("content");

    NotificationManager mNotificationManager =
   (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    //Get current notification
    Notification mNotification = builder.getNotification();

    //Show the notification
    mNotificationManager.notify(NOTIFICATION_ID, mNotification);

现在,您可以对结果活动的protected void onNewIntent(Intent intent)功能进行操作。

您还可以从setIntent(intent)函数将新意图设置为活动的目标newIntent

答案 1 :(得分:0)

在待处理意图中使用PendingIntent.FLAG_UPDATE_CURRENT标志来更新当前活动。然后在onResume()

中实施登录
相关问题