簡體   English   中英

具有2個待定意圖的警報管理器只有1個有效?

[英]Alarm Manager with 2 pending intents only 1 works?

我設置了2個警報,一個用於通知,另一個用於執行某些任務。 我的問題是只有一個警報似乎起作用(通知服務一個,第一個警報設置)。 其他警報永遠不會響起。 這是我的代碼:

Intent myIntent1 = new Intent(getApplicationContext(), NotificationService.class);
        PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent1, 0);
        AlarmManager alarmManager1 = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTimeInMillis(System.currentTimeMillis());
        long frequency1 = 30 * 1000; // in ms
        alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), frequency1, pendingIntent);

        // Set alarm to fire go to Next day everyday at the same time
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 14); // For 1 PM or 2 PM
        calendar.set(Calendar.MINUTE, 57);
        calendar.setTimeInMillis(System.currentTimeMillis());
        Intent myintent = new Intent(getApplicationContext(), AlarmNextDayService.class);
        AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        PendingIntent pi = PendingIntent.getService(getApplicationContext(), 11, myintent,0 );
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi);

歡迎任何建議。 我看過其他資料來源,到目前為止,對我來說都沒有用。 我還在清單文件中添加了警報權限,如下所示:

<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>

謝謝

Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 14); // For 1 PM or 2 PM
    calendar.set(Calendar.MINUTE, 57);
    calendar.setTimeInMillis(System.currentTimeMillis());

您正在設置HOUR_OF_DAY和MINUTE,但是通過調用setTimeInMillis(System.currentTimeMillis())可以覆蓋它們。

之后,使用過去已設置的calendar.getTimeMillis()值設置警報,因此我認為該警報已取消。

您最有可能遇到問題,因為由於省電,不能保證Service在被警報觸發時運行。 如果該設備使用電池供電並且在該警報響起時處於空閑狀態,則只有在下一次設備充滿電或使用交流電源時,它才會觸發。 您將需要使用持有喚醒鎖的BroadcastReceiverService完成后將由Service釋放該鎖。 WakefulBroadcastReceiver使它更易於處理。 本文將幫助提供更多詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM