简体   繁体   English

具有2个待定意图的警报管理器只有1个有效?

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

I have 2 alarms set, one for notifications, and the other one to do some tasks. 我设置了2个警报,一个用于通知,另一个用于执行某些任务。 My problem is that only one alarm seems to work( the notifications service one, the first alarm set). 我的问题是只有一个警报似乎起作用(通知服务一个,第一个警报设置)。 The Other alarm never goes off. 其他警报永远不会响起。 Here is my code : 这是我的代码:

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);

Any suggestions are welcome. 欢迎任何建议。 I have looked atother sources as well nothing works for me till now. 我看过其他资料来源,到目前为止,对我来说都没有用。 I have also added alarm permisison in the manifest file as the following : 我还在清单文件中添加了警报权限,如下所示:

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

Thank you 谢谢

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());

You are setting the HOUR_OF_DAY and the MINUTE but them you override that by calling setTimeInMillis(System.currentTimeMillis()); 您正在设置HOUR_OF_DAY和MINUTE,但是通过调用setTimeInMillis(System.currentTimeMillis())可以覆盖它们。

After that you set the alarm with the calendar.getTimeMillis() value which is already in the past, so the alarm is cancelled I think. 之后,使用过去已设置的calendar.getTimeMillis()值设置警报,因此我认为该警报已取消。

You're most likely seeing an issue because a Service is not guaranteed to run when triggered by an alarm, due to power save. 您最有可能遇到问题,因为由于省电,不能保证Service在被警报触发时运行。 If your device is on battery and idle when that alarm goes off, it will not trigger until the next time the device is full on or on AC power. 如果该设备使用电池供电并且在该警报响起时处于空闲状态,则只有在下一次设备充满电或使用交流电源时,它才会触发。 You'll need to use a BroadcastReceiver which holds a wake lock which is then released by the Service when it is done. 您将需要使用持有唤醒锁的BroadcastReceiverService完成后将由Service释放该锁。 The WakefulBroadcastReceiver makes this a little easier to handle. WakefulBroadcastReceiver使它更易于处理。 This article will help provide more details. 本文将帮助提供更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM