2013-01-04 60 views
0

我試圖做一個通知去關上週四和週五通過使用下面的代碼: 此代碼是爲週四具體的工作日Android通知熄滅直接

Calendar updateTime = Calendar.getInstance(); 
updateTime.set(Calendar.HOUR_OF_DAY, 14); 
updateTime.set(Calendar.MINUTE, 44); 
updateTime.set(Calendar.SECOND, 0); 
updateTime.set(Calendar.DAY_OF_WEEK, 5); 
if(updateTime.before(new GregorianCalendar())){ 
    updateTime.add(GregorianCalendar.DAY_OF_MONTH, 7); 
} 
Intent intent = new Intent(context, Alarm4.class); 
intent.putExtra(ONE_TIME, Boolean.TRUE); 
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0); 
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
alarmManager.set(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), pi); 

然而,這段代碼如果時間已經超過通知時間,則不會直接彈出通知。

我錯過了什麼?

+0

您是否嘗試過Android的內置報警調度? –

+0

不,我使用這個功能來處理所有的警報要編輯第一個帖子,以便更具體一些 – user1839995

回答

0

顯然,你的代碼是好的,但一半完成。現在,您必須創建一個BroadcastReceiver,您必須從中設置通知。你可以通過這個article,這篇文章會對你有所幫助。我想在這裏提到它會爲用戶設置單一警報,而不是重複。對於重複,您必須使用public void setRepeating (int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)而不是public void set (int type, long triggerAtMillis, PendingIntent operation)。它仍然會定期重複。

在我的想法中,您可以以不同的方式安排它。最初使用public void set (int type, long triggerAtMillis, PendingIntent operation)將鬧鐘設置爲第二天(在檢查第二天的鬧鐘後)。這將非常簡單。當警報將被系統廣播並且您的BroadcastReceiver將被呼叫。在onRecieve()您的BroadcastReceiver中,設置通知並設置第二天的警報(在檢查第二天的警報後)。通過這種方式,整個循環過程將在您的控制之下,您可以輕鬆地爲其設置任何邏輯。

+0

是不是有辦法像上面這樣做,我是Android開發的初學者,所以我我不是100%確定你的意思。 – user1839995

+0

編輯和增強。 –

1

這樣做。

  if (updateTime.getTimeInMillis() < System.currentTimeMillis() 

       || updateTime.before(new GregorianCalendar())) { 
      updateTime.set(Calendar.HOUR_OF_DAY, hourOfDay + 24); 
      updateTime.add(GregorianCalendar.DAY_OF_MONTH, 7); 
     } 

並設置一個類來設置鬧鐘。

 public class setAlarm { 

Context context; 

public setAlarm(Context c) { 
    // TODO Auto-generated constructor stub 
    this.context = c; 
} 

public void settingAlarm(Calendar calendar, int counter) { 
    Intent intent = new Intent(context, AlarmReceiver.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 
      counter, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    AlarmManager alarmManager = (AlarmManager) context 
      .getSystemService(Context.ALARM_SERVICE); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
      calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, 
      pendingIntent); 
} 

}

報警廣播接收器是 公共類AlarmReceiver擴展廣播接收器{

private NotificationManager manager; 
private int NOTIFICATION_ID; 

@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 
    Toast.makeText(context, "Alarm Received", Toast.LENGTH_LONG).show(); 
    manager = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(R.drawable.ic_launcher, 
      "Food Time", System.currentTimeMillis()); 
    Intent intent2 = new Intent(context, MainActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
      intent2, PendingIntent.FLAG_UPDATE_CURRENT); 
    notification.setLatestEventInfo(context, "FoodTime", 
      "Click to View your food", pendingIntent); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    notification.sound = Uri 
      .parse("android.resource://" 
        + "com.technogalax.sixsmallmealaday.alarm" + "/" 
        + R.raw.nsound); 

    manager.notify(NOTIFICATION_ID, notification); 

} 

}