使用暂挂意图的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类中放什么,我得到的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_CURRENT >实际上得到一个新的:

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天全站免登陆