2012-07-31 78 views
0

嗨,我通過推動創建了一個報警,所以當我收到通知我設置了報警。所有的工作都很好,但是在我推動的那一刻,警報顯示,而不是我說的那一刻。Android - 設置報警問題

這裏是我的代碼:

public void set_alarm(int year, int month, int day, int hour, int minute, 
     String title) { 

    Calendar cal = Calendar.getInstance(); 
    cal.set(Calendar.MONTH, 5); 
    cal.set(Calendar.YEAR, 2012); 
    cal.set(Calendar.DAY_OF_MONTH, 31); 
    cal.set(Calendar.HOUR_OF_DAY, 9); 
    cal.set(Calendar.MINUTE, 46); 

    Intent intent = new Intent(c, AlarmActivity.class); 
    intent.putExtra("title", title); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
      c.getApplicationContext(), 1253, intent, 
      PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA); 

    AlarmManager alarmManager = (AlarmManager) c 
      .getSystemService(c.ALARM_SERVICE); 

    alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 
      pendingIntent); 
} 

這裏是接收通知的類:

public class AlarmActivity extends BroadcastReceiver { 

@Override 
public void onReceive(Context c, Intent i) { 
    Toast.makeText(c, "Alarm worked.", Toast.LENGTH_LONG).show(); 

    int icon = R.drawable.ic_dialog_alert; 
    long when = System.currentTimeMillis(); 

    Bundle bundle = i.getExtras(); 
    String title = bundle.getString("title"); 

    final int NOTIF_ID = 1234; 
    NotificationManager notofManager = (NotificationManager) c 
      .getSystemService(Context.NOTIFICATION_SERVICE); 

    Intent notificationIntent = new Intent(c, AnonymeActivity.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(c, 0, 
      notificationIntent, 0); 
    Notification notification = new Notification(icon, "Faraway", when); 

    notification.setLatestEventInfo(c, "Faraway alarm", title, 
      contentIntent); 

    notification.flags = Notification.FLAG_INSISTENT; 
    notification.defaults |= Notification.DEFAULT_SOUND; 

    notofManager.notify(NOTIF_ID, notification); 
} 

}

感謝

回答

1

你在創建日曆實例日期:2012年5月31日,即當前日期之前。

據商務部:

如果發生在過去的時候,報警器會立即觸發。

只需設置正確的日期和時間。實際上,您應該使用傳遞給set_alarm()函數的參數。

(來源:http://developer.android.com/reference/android/app/AlarmManager.html#set(int, long, android.app.PendingIntent

+0

謝謝!多麼錯誤... :) – 2012-07-31 08:01:27

+0

不客氣;)你能接受答案嗎?謝謝 :) – mithrop 2012-07-31 08:04:34