2012-03-16 87 views
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); 
+0

我有點困惑 - 你提到你無法產生警報,我認爲你的Receiever的onReceive(...)永遠不會被調用?是這樣嗎? – Nick 2012-03-16 14:09:11

+0

好的離開它,如果我想生成通知而不是它是正確的方式......這是一種提醒..我想你明白了我的觀點。任務任務通知2012年3月17日下午7:00 – 2012-03-16 14:28:45

+0

我發現在你的鬧鐘代碼中沒有明顯的錯誤,所以很可能它的其他類似於你設置的鬧鐘時間不是你所期望的或者你的Receiver沒有做的你期望什麼等等。爲了簡單起見,我只是在你的Receiver的onReceive(...)中顯示一個toast消息。我也會註釋掉你的cal.set(...)調用,並將你的cal.setTimeInMillis改爲cal.setTimeInMillis(System.currentTimeMillis()+ 5000);所以當你運行你的應用程序時,如果你沒有看到Toast味精,你就知道它沒有啓動。 – Nick 2012-03-16 14:54:06

回答

0

我有類似的東西,它的工作原理。

首先喜聲明掛起:

Intent intent = new Intent(Global.a, EventAlarmReceiver.class); 
    intent.putExtra("title", ""+cd.title); 
    intent.putExtra("desc", ""+cd.description); 

    PendingIntent pendingIntent = PendingIntent.getBroadcast(
      Global.a.getApplicationContext(), (int) (cd.alarmTime), intent, PendingIntent.FLAG_CANCEL_CURRENT); 

    AlarmManager alarmManager = (AlarmManager) Global.a.getSystemService(Activity.ALARM_SERVICE); 
    alarmManager.set(AlarmManager.RTC, cd.alarmTime, pendingIntent); 

和EventAlarmReceiver i類有:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE); 

    String text = String.valueOf(intent1.getCharSequenceExtra("title")); 

    Notification notification = new Notification(R.id.icon, 
      text, System.getTimeInMillis()); 

    notification.vibrate = new long[]{100,250,300,330,390,420,500}; 

    // Hide the notification after its selected  
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    Intent intent = new Intent(context, ShowEventActivity.class); 
    intent.putExtra("title", String.valueOf(intent1.getCharSequenceExtra("title"))); 
    intent.putExtra("desc", String.valueOf(intent1.getCharSequenceExtra("desc"))); 


    String text1 = String.valueOf(intent1.getCharSequenceExtra("desc")); 

    PendingIntent activity = PendingIntent.getActivity(context, (int) System.getTimeInMillis() , intent, PendingIntent.FLAG_CANCEL_CURRENT); 

    notification.setLatestEventInfo(context, text, text1, activity); 

    notificationManager.notify((int)System.getTimeInMillis(), notification); 

確保在清單聲明聲明包,其中接收器是:例如,在我的情況EventAlarmReceiver類是包com.app.name.notifications,所以在清單中我有:

<receiver android:name=".notifications.EventAlarmReceiver"></receiver>