首页 文章

通过Intent将变量传递给活动始终是相同的

提问于
浏览
0

我的应用程序收到C2DM消息,并使用C2DM消息发送状态错误通知 . 到现在为止还挺好 . 当用户单击通知时,将调用一个活动,将C2DM消息作为变量传递 .

现在,它第一次运行顺利,第二次传递的变量没有刷新 . 它始终是第一个传递的变量 . 我错过了什么吗?

以下是片段:

C2DM通知

Intent notificationIntent = new Intent(context, BMBPad.class);
notificationIntent.putExtra("seqid", message);              
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

这就是我如何读取Intent调用的Activity中的变量 .

extra = this.getIntent().getExtras();
seqidi = extra.getString("seqid");

有人知道为什么会这样吗?

3 回答

  • 0

    你需要使用标志 PendingIntent.FLAG_UPDATE_CURRENT

    在你的情况下:

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    请看这里:Android PendingIntent

  • 0

    您可以尝试将此片段附加到Intent调用的Activity中 .

    /**
     * Override super.onNewIntent() to let getIntent() fetch the latest intent
     * that was used to start this Activity rather than the first intent.
     */
    @Override
    public void onNewIntent(Intent intent){
        super.onNewIntent(intent);
        setIntent(intent);
    }
    
  • 0

    覆盖onNewIntent()方法,得到如下变量:

    @Override
    public void onNewIntent(Intent intent){
    super.onNewIntent(intent);
    seqid = intent.getStringExtra("seqid","");
    

    }

    因为再次启动活动会触发onNewIntent()方法 .

相关问题