Java AlarmManager不工作

Java AlarmManager不工作,java,android,alarmmanager,reboot,Java,Android,Alarmmanager,Reboot,我有一个闹钟管理器。它必须每天工作一次。若我从任务中删除应用程序或重新启动设备,则警报必须存在。但当我重新启动设备时,警报不存在。若我从任务中删除应用程序,则警报也不存在 main活动: @Override protected void onResume() { if (isAlarmEnableded) { if (!isAlarmSheduled) { setAlarm();

我有一个闹钟管理器。它必须每天工作一次。若我从任务中删除应用程序或重新启动设备,则警报必须存在。但当我重新启动设备时,警报不存在。若我从任务中删除应用程序,则警报也不存在

main活动:

@Override
    protected void onResume() {
             if (isAlarmEnableded) {
            if (!isAlarmSheduled) {
                setAlarm();
                isAlarmSheduled = true;
              putInSharedPreferences(Boolean.toString(isAlarmSheduled));
            }
        } else {
            cancelAlarm(MainActivity.this);
            isAlarmSheduled = false;
            putInSharedPreferences(isAlarmSheduled);
        }

        super.onResume();
    }
isalarmenabled
-SwitchPreference=true。如果
已启用报警
-我将关闭报警。
IsAlarmScheduled
-我保存在首选项中的值。它显示报警状态

 private void setAlarm() {

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

        if (android.os.Build.VERSION.SDK_INT >= 19) {
            alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, setAlarmTime(),
                    AlarmManager.INTERVAL_DAY, pendingIntent);
        } else {
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, setAlarmTime(),
                    AlarmManager.INTERVAL_DAY, pendingIntent);
        }

private long setAlarmTime() {

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());

        calendar.set(Calendar.HOUR_OF_DAY, 20);
        calendar.set(Calendar.MINUTE, 48);
        calendar.set(Calendar.SECOND, 0);

        if (System.currentTimeMillis() > calendar.getTimeInMillis()) {
            calendar.add(Calendar.DAY_OF_YEAR, 1);
        }
        return calendar.getTimeInMillis();
    }
如果当前时间>该计划时间,我将为明天设置闹钟

Constants.ALARM\u INTENT
-“com.blabla.AlarmReceiver”

报警接收器

@Override
    public void onReceive(Context context, Intent intent) {
        intent.setClass(context, NotificationActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
NotificationActivity在报警时间显示一个对话框。 我还有一个DeviceBootReceiver,它调用
setAlarm()
方法并将
isAlarmScheduled
放入首选项中。 清单

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.mypackage..AlarmReceiver">
            <intent-filter>
                <action android:name="com.blabla.AlarmReceiver" />
            </intent-filter>
        </receiver>

        <receiver
            android:name="com.mypackage.DeviceBootReceiver">

            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

我正在通过adb shell dumpsys alarm>dump.txt检查报警。我需要一个稳定的闹钟,在任何情况下每天工作一次。如果我关闭应用程序并重新启动它,警报应该是相同的
警报在所有情况下都有效,但在我重新启动设备和从任务中删除应用程序时不起作用。也许我的代码有一些错误。请帮助我解决我的问题,因为我不知道我做错了什么/谢谢

您必须在启动设备后处理启动接收器以初始化报警,例如:

public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
         List<Note> notes = dbHelper.getAllNotesAlarms();
    for (Note note : notes) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(note.getAlarm());
        Intent intent = new Intent(App.getAppContext(), AlarmService.class);
        intent.putExtra("NOTE_ID", note.getId());
        PendingIntent pendingIntent = PendingIntent.getService(App.getAppContext(),
                (int) note.getId(), intent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am =
                (AlarmManager) App.getAppContext().getSystemService(Activity.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                pendingIntent);
    } 
    }

}
公共类BootReceiver扩展了BroadcastReceiver{
@凌驾
公共void onReceive(上下文、意图){
List notes=dbHelper.getAllNotesAlarms();
用于(注释:注释){
Calendar cal=Calendar.getInstance();
cal.setTimeInMillis(note.getAlarm());
意向意向=新意向(App.getAppContext(),AlarmService.class);
intent.putExtra(“NOTE_ID”,NOTE.getId());
PendingEvent PendingEvent=PendingEvent.getService(App.getAppContext(),
(int)note.getId(),intent,pendingent.FLAG_CANCEL_CURRENT);
报警管理器am=
(AlarmManager)App.getAppContext().getSystemService(Activity.ALARM_服务);
am.set(AlarmManager.RTC_唤醒,cal.getTimeInMillis(),
悬挂式);
} 
}
}

您应该添加引导完整接收器,然后再次设置报警
导游

我知道。我的接收器看起来像你描述的。它调用setAlarm()方法并将isAlarmScheduled放入首选项中。但是它不起作用关于com.mypackage.DeviceBootReceiver类,您可以发送类fileprotected void onResume(){setAlarm();IsAlarmScheduled=true;putInSharedPreferences(Boolean.toString(IsAlarmScheduled));}