2012-11-23 116 views
2

我有這樣的代碼:AlarmManager觸發的PendingIntent太早第二次

設定的報警:

public void setAlarm() 
{ 
    Calendar Calendar_Object = Calendar.getInstance(); 

    Calendar_Object.add (Calendar.DAY_OF_YEAR, 1); 

    Calendar_Object.set(Calendar.HOUR_OF_DAY, 0); 
    Calendar_Object.set(Calendar.MINUTE, 0); 
    Calendar_Object.set(Calendar.SECOND, 1); 

    Intent myIntent = new Intent(Main.this, AlarmReceiver.class); 

    PendingIntent pendingIntent = PendingIntent.getBroadcast(Main.this, 
      0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT); 

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

    alarmManager.setRepeating(AlarmManager.RTC, Calendar_Object.getTimeInMillis(),1000*60*60*24, pendingIntent); 
} 

廣播接收器:

public class AlarmReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     Intent myIntent = new Intent(context, NotificationService.class); 
     context.startService(myIntent); 
    } 

} 

服務:

public class NotificationService extends Service { 

private NotificationManager mManager; 

@Override 
public IBinder onBind(Intent arg0) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 

} 

@SuppressWarnings("deprecation") 
@Override 
public void onStart(Intent intent, int startId) { 
    super.onStart(intent, startId); 

    mManager = (NotificationManager) this.getApplicationContext() 
      .getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE); 

    Intent intent1 = new Intent(this.getApplicationContext(), ABC.class); 

    Notification notification = new Notification(R.drawable.ic_launcher, 
      "xxx", System.currentTimeMillis()); 

    intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP 
      | Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
      this.getApplicationContext(), 0, intent1, 
      PendingIntent.FLAG_UPDATE_CURRENT); 

    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    notification.setLatestEventInfo(this.getApplicationContext(), 
      "abc", "xyz", 
      pendingNotificationIntent); 

    mManager.notify(0, notification); 
    } 
} 
設置完al後的

在00:00:01一切都很完美,但下一次出現問題時。 PendingIntent在24小時間隔內觸發6到8次。我不記得每個觸發器的時間是否相同,但我希望每一次都有一次。代碼有什麼問題?

+0

爲了編譯,請使用以小寫字母開頭並且沒有下劃線的混合大小寫變量名稱。 – njzk2

+0

我不明白爲什麼'Calendar_Object.add(Calendar.DAY_OF_YEAR,1);'? – njzk2

+0

你什麼時候調用setAlarm()方法,因爲它每次都會重置定時器 –

回答

1

這將創建一個報警,每天將關閉在00:05,重要的部分是AlarmManager.INTERVAL_DAY

Date date = new Date(); 
Calendar cal = Calendar.getInstance(); 
cal.setTime(date); 
cal.set(Calendar.HOUR_OF_DAY, 0); 
cal.set(Calendar.MINUTE, 5); 
cal.set(Calendar.SECOND, 0); 
cal.set(Calendar.MILLISECOND, 0); 
cal.add(Calendar.HOUR, 24); 

PendingIntent pi = PendingIntent.getBroadcast(context, 0, alarmIntent, 0); 
mgr.cancel(pi); 
mgr.setRepeating(AlarmManager.RTC, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi); 

另外,不要忘記隨時取消以前類似的報警,當重新設置一個新的,或者他們都會關閉,舊的設置警報,新的,請參閱上面的mgr.cancel(pi);

+0

發生了同樣的問題。它會在實際時間之前觸發。任何人都可以幫忙嗎? @marmor – mostashaar

+0

它聽起來像你有很多先前設置的警報,嘗試重新啓動,然後調用上面的代碼。重新啓動應該取消設置所有警報。 – marmor