从Intent启动的getExtra我想在用户从列表中选择一段时间并在给定时间为它创建通知之后发出警报。我的问题是,我的意图putExtra无法在广播接收器上收到的“showname”。它总是得到空值。 这是我为我的大部分意图做的方式,但我认为这一次可能是因为pendingIntent或broadcastReceiver需要做不同的事情。 谢谢从一个pendingIntent
穿过未决意图
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);
}
}
这是正确的答案......... – Necronet 2012-10-11 15:38:12
+1,这是正确的答案,它应该被接受。这真的很有用,Thx。 (顺便说一下,在创建待定目标时,您还可以使用不同的请求代码,并使用散列,这非常方便)。 – Snicolas 2013-07-25 09:11:44