2013-03-08 54 views
0

我的應用程序使用下面的代碼來設置Alaram在「每日」基礎上執行服務。android alarmmanager reset

AlarmManager alarmManager = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE); 
     Intent i = new Intent(ctx, SchedulerEventReceiver.class); // explicit// intent 

     Calendar now = Calendar.getInstance(); 
     now.add(Calendar.SECOND, 20); 
     PendingIntent intentExecuted = PendingIntent.getBroadcast(ctx, new Random().nextInt(), i,PendingIntent.FLAG_CANCEL_CURRENT); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), AlarmManager.INTERVAL_DAY, intentExecuted); 

adb shell dumpsys alarm向我顯示了適當的細節。就像它預定每天運行一樣。但是,當我將.apk安裝到我的手機時,它正在反覆執行。它不會等待一天的執行。

我不知道如何重置手機中設置的鬧鐘。我卸載了應用程序和新安裝的應用程序,但它不起作用。有人能告訴我什麼是錯的嗎?

+0

需要觸發安裝的應用程序時,報警器和手機重啓 – DjHacktorReborn 2013-03-08 18:48:17

回答

0

現在,你告訴它到20秒熄滅到分鐘

now.add(Calendar.SECOND, 20); 

您需要添加一天的時間和可能分鐘

now.add(Calendar.HOUR, 12) //set your hour here. noon right now 

now.add(Calendar.HOUR_OF_DAY, 12) 

Calendar

+0

但我說在AlarmManager.INTERVAL_DAY喚醒。這是否足夠或我需要別的東西? – Chintan 2013-03-08 19:00:36

+0

我沒有看到'param',對不起。它應該但我會刪除之前檢查它。我想它可能不知道什麼時候應該是20秒,所以我會嘗試添加一小時,看看它是否有效 – codeMagic 2013-03-08 19:06:12

+0

我會這樣做,但我的問題是,爲什麼它在模擬器中表現不同。根據我的應用中的邏輯,一旦鬧鐘開始,我就發送通知。我在模擬器中看不到任何通知。不過,我可以在手機中看到經常出現的通知。 – Chintan 2013-03-08 19:07:41

1

我已經使用這個,這對我來說工作得很好。我已經使用這個鬧鐘在18天后觸發。然後再經過18天等:

long time = 1555200000L; //18 days = 18*24*60*60*1000 = 1555200000L 
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
Intent i = new Intent(context, MyReceiver.class); 
PendingIntent pi = PendingIntent.getBroadcast(context, tag, i, PendingIntent.FLAG_CANCEL_CURRENT); 
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+time, time, pi); 
+0

帶此代碼警報不會喚醒執行task.private static final int EXEC_INTERVAL = 24 * 60 * 60 * 1000; AlarmManager alarmManager =(AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE); Intent i = new Intent(ctx,SchedulerEventReceiver.class); \t \t PendingIntent intentExecuted = PendingIntent.getBroadcast(ctx,new Random()。nextInt(),i,PendingIntent.FLAG_CANCEL_CURRENT); (AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+ EXEC_INTERVAL,AlarmManager.INTERVAL_DAY,intentExecuted); – Chintan 2013-03-11 16:45:13

+0

100+保存我的日子哥們謝謝 – chhameed 2014-01-20 10:55:19