0
這是我的代碼。 離我是不能夠通過編程產生報警..在android中生成警報和通知問題
Calendar cal = Calendar.getInstance();
int id = (int) cal.getTimeInMillis();
Intent myIntent = new Intent(this,MyScheduledReceiver.class);
myIntent.putExtra("taskTitle", taskTitle.getText().toString());
myIntent.putExtra("taskDetails", taskDetails.getText().toString());
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(year, mon, day,tp.getCurrentHour(),tp.getCurrentMinute());
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), id,
myIntent,PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
我在廣播reciever清單文件
<receiver android:name=".MyScheduledReceiver"></receiver>
的聲明。
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationManager manger = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Combi Note",
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(context,
NOTIFICATION_ID,
new Intent(context, MyScheduledReceiver.class), 0);
Bundle extras=intent.getExtras();
String title=extras.getString("taskTitle");
String note=extras.getString("taskDetails");
notification.setLatestEventInfo(context, note, title, contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
notification.defaults |= Notification.DEFAULT_SOUND;
manger.notify(NOTIFICATION_ID++, notification);
我有點困惑 - 你提到你無法產生警報,我認爲你的Receiever的onReceive(...)永遠不會被調用?是這樣嗎? – Nick 2012-03-16 14:09:11
好的離開它,如果我想生成通知而不是它是正確的方式......這是一種提醒..我想你明白了我的觀點。任務任務通知2012年3月17日下午7:00 – 2012-03-16 14:28:45
我發現在你的鬧鐘代碼中沒有明顯的錯誤,所以很可能它的其他類似於你設置的鬧鐘時間不是你所期望的或者你的Receiver沒有做的你期望什麼等等。爲了簡單起見,我只是在你的Receiver的onReceive(...)中顯示一個toast消息。我也會註釋掉你的cal.set(...)調用,並將你的cal.setTimeInMillis改爲cal.setTimeInMillis(System.currentTimeMillis()+ 5000);所以當你運行你的應用程序時,如果你沒有看到Toast味精,你就知道它沒有啓動。 – Nick 2012-03-16 14:54:06