简体   繁体   English

警报管理器不起作用

[英]Alarm Manager does not work

Alarm manager in my app does not work. 我的应用程序中的警报管理器不起作用。 I do everything like described here https://developer.android.com/training/scheduling/alarms.html but the alarm does not work. 我所做的一切都像这里描述的https://developer.android.com/training/scheduling/alarms.html,但是警报不起作用。
Here is my code: 这是我的代码:

manifest 表现

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

Reciever (Reciever registered in the Manifest) 收件人 (在清单中注册的收件人)

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

    Intent alarmIntent = new Intent(Constants.ALARM_INTENT);
    alarmIntent.setClass(context, NotificationActivity.class);
    alarmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(alarmIntent);
}

Activity 活动

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 1);
calendar.set(Calendar.MINUTE, 30);

alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

I do not know how to make it work. 我不知道如何使它工作。 Please, help me to fix it. 请帮我修复它。

First, remove the SET_ALARM permission, as it is not relevant here. 首先,删除SET_ALARM权限,因为这里与权限无关。

Second, you need to ensure that your Calendar object is in the future. 其次,您需要确保Calendar对象在将来。 Most of the time, yours will be in the past, because you are using calendar.set(Calendar.HOUR_OF_DAY, 1) and set(Calendar.MINUTE, 30) . 大多数时候,您会过去,因为您正在使用calendar.set(Calendar.HOUR_OF_DAY, 1)set(Calendar.MINUTE, 30) If you execute this code after 01:30, it will be in the past. 如果您在01:30之后执行此代码,它将是过去的代码。 In that case, you need to add() one day to make it be tomorrow at 01:30. 在这种情况下,您需要一天add()一次add()使其成为明天的01:30。

Third, use adb shell dumpsys alarm to see if your alarm is scheduled, once you make the above fixes. 第三,完成上述修复后,请使用adb shell dumpsys alarm来查看是否已安排警报。

Fourth, use LogCat to see if there are any warnings or errors coming from your code, such as perhaps AlarmReceiver or NotificationActivity not being registered in the manifest. 第四,使用LogCat查看您的代码中是否存在任何警告或错误,例如,可能未在清单中注册AlarmReceiverNotificationActivity

You're asking for an alarm based on the system's elapsed realtime clock here: 您在此处根据系统的实时时钟请求警报:

alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

But you're passing it a time based on a normal calendar clock. 但是您要给它传递一个基于正常日历时钟的时间。 If you want an alarm to happen using a calendar-based clock, say this instead: 如果您希望使用基于日历的时钟来发出警报,请改为这样:

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
        calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

Note the differnce in javadoc between ELAPSED_REALTIME_WAKEUP and RTC_WAKEUP . 注意ELAPSED_REALTIME_WAKEUPRTC_WAKEUPjavadoc中的差异。

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

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