getIntra从一个pendingIntent启动的Intent

我想在用户从列表中select一段时间后发出警报,并在给定时间为其创build通知。 我的问题是,我的意图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); } } 

如果您在意图中更改Extra的值,那么在创build挂起的意图时,应该使用PendingIntent.FLAG_CANCEL_CURRENT标志。

一个简单的例子是

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

这是正确的方式,并将确保您的新价值被交付。

希望能帮助到你。

意图在系统中被重用,除非它们在我认为的上下文/行动上有所不同。 文档链接 。 也就是说,如果你已经构build了一个意图,那么这个意图也可能在以后使用。

作为一个debuggingtesting,你可以尝试在intent.putExtras(c)下面添加intent.setAction("" + Math.random()) intent.putExtras(c) ,看看你的额外接收是否在另一端。

相关文件

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

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

你应该使用intent.putExtra()方法。 没有设置捆绑额外的细分市场。 如果你设置string,你需要intent.putExtra(<key>, value); 试试这个将会完成。 我用它。

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

在这里,我做了什么

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

希望它会帮助别人,快乐的编码..! 🙂