從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