【问题标题】:passing variable to an activity through an Intent is always the same通过 Intent 将变量传递给活动总是相同的
【发布时间】:2011-05-13 18:45:03
【问题描述】:

我的应用程序收到 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");

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

【问题讨论】:

  • 首先,您能否用代码引号澄清您的问题,以设置 sn-p 样式。谢谢

标签: android variables notifications android-intent android-c2dm


【解决方案1】:

你需要使用标志PendingIntent.FLAG_UPDATE_CURRENT

在你的情况下:

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

也请看这里:Android PendingIntent

【讨论】:

    【解决方案2】:

    你可以尝试在Intent调用的Activity中附加这个sn-p。

    /**
     * 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);
    }
    

    【讨论】:

      【解决方案3】:

      覆盖 onNewIntent() 方法,像这样获取你的变量:

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

      }

      因为再次启动activity会触发onNewIntent()方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多