38

在用户从列表中选择有时间的东西并在给定时间为其创建通知后,我试图发出一些警报。我的问题是广播接收器无法接收到我的 Intent 上的 putExtra 的“showname”。它总是得到空值。这是我为大多数意图执行此操作的方式,但我认为这一次可能是因为 pendingIntent 或 broadcastReceiver 需要以不同的方式完成某些事情。谢谢

通过待处理意图发送意图的函数

public void setAlarm(String showname,String time) {

    String[] hourminute=time.split(":");
    String hour = hourminute[0];
    String minute = hourminute[1];
    Calendar rightNow = Calendar.getInstance();
    rightNow.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
    rightNow.set(Calendar.MINUTE, Integer.parseInt(minute));
    rightNow.set(Calendar.SECOND, 0);
    long t=rightNow.getTimeInMillis();
    long t1=System.currentTimeMillis();

    try {   

    Intent intent = new Intent(this, alarmreceiver.class);  
    Bundle c = new Bundle();            
    c.putString("showname", showname);//This is the value I want to pass
    intent.putExtras(c);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 12345, intent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, rightNow.getTimeInMillis(),pendingIntent);
    //Log.e("ALARM", "time of millis: "+System.currentTimeMillis());
    Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();

    } catch (Exception e) {
        Log.e("ALARM", "ERROR IN CODE:"+e.toString());
    }
}

这是接收端

public class alarmreceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();      
    Bundle b = intent.getExtras();
    String showname=b.getString("showname");//This is where I suppose to receive it but its null
    NotificationManager manger = (NotificationManager) context
            .getSystemService(context.NOTIFICATION_SERVICE);

    Notification notification = new Notification(R.drawable.icon,
            "TVGuide Υπενθύμιση", System.currentTimeMillis());
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(context, tvguide.class), 0);

    notification.setLatestEventInfo(context, "Το Πρόγραμμα Ξεκίνησε",
            showname, contentIntent);

    notification.flags = Notification.FLAG_ONLY_ALERT_ONCE;

    notification.sound = Uri.parse("file:///sdcard/dominating.mp3");
    notification.vibrate = new long[]{100, 250, 100, 500};
    manger.notify(1, notification);
}           
}
4

5 回答 5

58

如果您在 Intent 中更改 Extra 的值,那么在创建待处理 Intent 时,您应该使用标志 PendingIntent.FLAG_CANCEL_CURRENT。

一个简单的例子是

PendingIntent pi = PendingIntent.getBroadcast(context, 0,intentWithNewExtras,PendingIntent.FLAG_CANCEL_CURRENT);

这是正确的方式,将确保您的新价值观得以传递。

希望能帮助到你。

于 2010-10-03T20:12:36.420 回答
24

意图在系统中被重用,除非它们在我相信的上下文/动作上有所不同。文档链接。也就是说,如果您已经构建了一个 Intent,那么该 Intent 也可能在以后使用。

作为调试测试,您可以尝试在intent.setAction("" + Math.random())下面添加intent.putExtras(c),看看另一端是否收到了您的额外内容。

相关文件

由于这种行为,重要的是要知道两个 Intent 何时被认为是相同的,以便检索 PendingIntent。人们犯的一个常见错误是创建多个 PendingIntent 对象,其 Intent 仅在其“额外”内容上有所不同,期望每次都获得不同的 PendingIntent。这不会发生。Intent 中用于匹配的部分与 Intent.filterEquals 定义的部分相同。如果您使用两个根据 Intent.filterEquals 等效的 Intent 对象,那么您将获得相同的 PendingIntent 对象。

于 2010-05-21T13:49:15.853 回答
9

对不同的警报通知使用不同的请求代码,以避免覆盖相同的警报时间。

于 2011-10-11T12:45:40.137 回答
0

你应该使用intent.putExtra()方法。未将捆绑包设置为附加部分。如果你设置了字符串,你应该intent.putExtra(<key>, value);试试这个,它会完成的。我用过。

于 2013-09-30T10:48:41.113 回答
0

我有同样的问题。我按照@aioobe 在这里的建议设置了一个动作来解决它,我的意图就像魔法一样工作。

这是我所做的

  ` intent.putExtra("Name", mName);
    intent.putExtra("dateTime", newdate);
    intent.setAction("" + Math.random());`

希望它会帮助某人,快乐编码..!:)

于 2015-02-12T09:40:14.500 回答