我有一個擴展Application的類。在課堂上,我打電話給AlarmManager並傳遞意圖。按照計劃,我的EventReceiver類擴展了BroadcastReceiver,它在onReceive方法中處理調用。我如何從onReceive方法再次調用intent來安排另一個事件?安排重複報警/事件
0
A
回答
0
可以使用setRepeating()
代替set()
,要把它自動復發。或者,只需在等效的Intent
(相同的動作,相同的Uri
,相同的組件等)上創建另一個PendingIntent
。您每次都不需要原始的PendingIntent
對象。
0
final Intent intent = new Intent(context, YourService.class);
final PendingIntent pending = PendingIntent.getService(context, 0, intent, 0);
final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pending);
long interval = 30000;//milliseconds
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),interval, pending);
更完整的示例可以在Photostream示例應用程序http://code.google.com/p/apps-for-android/中找到。
我不相信我可以使用設置,因爲時間重複會有所不同,我需要讓我的queue.It接下來,如果有使用相同的意圖和安排活動在不同發生的方式將是不錯倍。例如,如果我有一系列我想傳入的時間,而不必一個接一個地鏈接事件。 – JDM 2010-04-27 00:59:21
然後使用幾個稍微不同的'Intents'並將它們全部安排在一個塊中。通過「略有不同」,他們不得不超過額外差異,否則他們將等同於PendingIntent的觀點。如果你正在使用一個指定組件的'Intent'(也就是說,它將'Class'作爲構造函數的第二個參數),那麼就集中一個獨特的動作字符串,這將使它們保持獨特但不會干擾你的收據。 – CommonsWare 2010-04-27 01:37:08