putExtra 使用挂起的意图不起作用 [英] putExtra using pending intent not working

本文介绍了putExtra 使用挂起的意图不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 GCMIntentservice 中编写了一个代码,用于向许多用户发送推送通知.我使用 NotificationManager 将在单击通知时调用 DescriptionActivity 类.我还将来自 GCMIntentService 的 event_id 发送到 DescriptionActivity

I have written a code in my GCMIntentservice that sends push notifications to many users. I use the NotificationManager that will call DescriptionActivity class when the notification is clicked. I also send the event_id form the GCMIntentService to the DescriptionActivity

protected void onMessage(Context ctx, Intent intent) {
     message = intent.getStringExtra("message");
     String tempmsg=message;
     if(message.contains("You"))
     {
        String temparray[]=tempmsg.split("=");
        event_id=temparray[1];
     }
    nm= (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    intent = new Intent(this, DescriptionActivity.class);
    Log.i("the event id in the service is",event_id+"");
    intent.putExtra("event_id", event_id);
    intent.putExtra("gcmevent",true);
    PendingIntent pi = PendingIntent.getActivity(this,0, intent, 0);
    String title="Event Notifier";
    Notification n = new Notification(R.drawable.defaultimage,message,System.currentTimeMillis());
    n.setLatestEventInfo(this, title, message, pi);
    n.defaults= Notification.DEFAULT_ALL;
    nm.notify(uniqueID,n);
    sendGCMIntent(ctx, message);

}

这里我在上述方法中得到的 event_id 是正确的,即我总是得到更新的.但是在下面的代码中(DescriptionActivity.java):

Here the event_id that I'm getting in the above method is correct i.e I always get the updated one. But in the code below (DescriptionActivity.java):

    intent = getIntent();
    final Bundle b = intent.getExtras();
    event_id = Integer.parseInt(b.getString("event_id"));

此处的 event_id 始终为5".无论我在 GCMIntentService 类中放了什么Extra,我得到的 event_id 总是 5.有人可以指出问题吗?是因为未决的意图吗?如果是,那我该如何处理?

The event_id here is always "5". No matter what I putExtra in the GCMIntentService class,the event_id I get is always 5. Can somebody please point out the problem? Is is because of the pending intent? If yes, then how should I handle it?

推荐答案

PendingIntent 与您提供的第一个 Intent 重用,那是您的问题.

The PendingIntent is reused with the first Intent you provided, that's your problem.

为避免这种情况,请使用标志PendingIntent.FLAG_CANCEL_CURRENTa> 当您调用 PendingIntent.getActivity() 以实际获得一个新的时:

To avoid this, use the flag PendingIntent.FLAG_CANCEL_CURRENT when you call PendingIntent.getActivity() to actually get a new one:

PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

或者,如果您只想更新附加内容,请使用标志PendingIntent.FLAG_UPDATE_CURRENT

Alternatively, if you just want to update the extras, use the flag PendingIntent.FLAG_UPDATE_CURRENT

这篇关于putExtra 使用挂起的意图不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆