Java 设置的报警没有';Don’我不能在正确的时间开始

Java 设置的报警没有';Don’我不能在正确的时间开始,java,android,alarmmanager,Java,Android,Alarmmanager,我正在创建一个应用程序,该应用程序应根据日期在正确的时间显示通知。但通知会延迟或在打开应用程序后发出。我请求您帮助我,指出代码中的错误,并说明其不正确的原因 AndroidManifest.xml: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission

我正在创建一个应用程序,该应用程序应根据日期在正确的时间显示通知。但通知会延迟或在打开应用程序后发出。我请求您帮助我,指出代码中的错误,并说明其不正确的原因

AndroidManifest.xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.SET_ALARM" />

<receiver
    android:name="com.zalj.schedule.MyNotifications.MyAlarm"
    android:enabled="true"
    android:exported="true"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
</receiver>
MyAlarm类别:

public class MyAlarm extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    int idCase = intent.getIntExtra(
            IntentHelper.COMMAND,
            IntentHelper.COMMAND_NOTIFICATION_UpdateAlarmToDay);

    switch (idCase){
        case IntentHelper.COMMAND_NOTIFICATION_SetAlarm:{
            showNotification(context ,intent);
        }break;

        case IntentHelper.COMMAND_NOTIFICATION_UpdateAlarmToDay:{
            updateAlarmsToNextDay(context, intent);
        }break;
    }
}

private void showNotification(Context context, Intent intent){
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

    String title;
    String message;
    String chanelId;
    String chanelNameDiscipline;
    int notificationId;

    title = intent.getStringExtra(IntentHelper.NOTIFICATION_TITLE);
    message = intent.getStringExtra(IntentHelper.NOTIFICATION_MESSAGE);
    chanelId = intent.getStringExtra(IntentHelper.CHANEL_ID);
    chanelNameDiscipline = intent.getStringExtra(IntentHelper.CHANEL_NAME);
    notificationId = intent.getIntExtra(IntentHelper.NOTIFICATION_ID, 0);

    NotificationCompat.Builder notification = new NotificationCompat.Builder(context, chanelId);
    notification
            .setSmallIcon(R.drawable.discipline_notification)
            .setContentTitle(title)
            .setContentText(message)
            .setStyle(new NotificationCompat.BigTextStyle())
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setSound(NotificationHelper.getSound())
            .setVibrate(NotificationHelper.getVibrate(NotificationHelper.LONG_VIBRATE))
            .setAutoCancel(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
    {
        NotificationChannel nc =
                new NotificationChannel(
                        chanelId,
                        chanelNameAction,
                        NotificationManager.IMPORTANCE_DEFAULT);

        notificationManager.createNotificationChannel(nc);
    }

    notificationManager.notify(notificationId, notification.build());
}

private void updateAlarmsToNextDay(Context context, Intent intent){
    Action action;

    String name = intent.getStringExtra(IntentHelper.ACTIONS_NAME);

    if (name == null){
        SharedPreferences settings = context.getSharedPreferences(
                DataContract.MyAppSettings.LAST_VIEWED_ACTIONS,
                Context.MODE_PRIVATE);

        name = settings.getString(
                DataContract.MyAppSettings.LAST_ACTIONS,
                DataContract.MyAppSettings.NULL);
    }

    ScheduleBuilder scheduleBuilder = ScheduleBuilder.getInternalSchedule(context, name);
    schedule = scheduleBuilder.build();
    schedule.updateTimes(context);
    schedule.updateActions(context, Calendar.getInstance());

    MyDisciplineNotificationManager.updateAllAlarm(context, schedule);
}

}

有时它还取决于移动设备。一些手机中有更干净的应用程序,防止应用程序触发通知
public class MyAlarm extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    int idCase = intent.getIntExtra(
            IntentHelper.COMMAND,
            IntentHelper.COMMAND_NOTIFICATION_UpdateAlarmToDay);

    switch (idCase){
        case IntentHelper.COMMAND_NOTIFICATION_SetAlarm:{
            showNotification(context ,intent);
        }break;

        case IntentHelper.COMMAND_NOTIFICATION_UpdateAlarmToDay:{
            updateAlarmsToNextDay(context, intent);
        }break;
    }
}

private void showNotification(Context context, Intent intent){
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

    String title;
    String message;
    String chanelId;
    String chanelNameDiscipline;
    int notificationId;

    title = intent.getStringExtra(IntentHelper.NOTIFICATION_TITLE);
    message = intent.getStringExtra(IntentHelper.NOTIFICATION_MESSAGE);
    chanelId = intent.getStringExtra(IntentHelper.CHANEL_ID);
    chanelNameDiscipline = intent.getStringExtra(IntentHelper.CHANEL_NAME);
    notificationId = intent.getIntExtra(IntentHelper.NOTIFICATION_ID, 0);

    NotificationCompat.Builder notification = new NotificationCompat.Builder(context, chanelId);
    notification
            .setSmallIcon(R.drawable.discipline_notification)
            .setContentTitle(title)
            .setContentText(message)
            .setStyle(new NotificationCompat.BigTextStyle())
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setSound(NotificationHelper.getSound())
            .setVibrate(NotificationHelper.getVibrate(NotificationHelper.LONG_VIBRATE))
            .setAutoCancel(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
    {
        NotificationChannel nc =
                new NotificationChannel(
                        chanelId,
                        chanelNameAction,
                        NotificationManager.IMPORTANCE_DEFAULT);

        notificationManager.createNotificationChannel(nc);
    }

    notificationManager.notify(notificationId, notification.build());
}

private void updateAlarmsToNextDay(Context context, Intent intent){
    Action action;

    String name = intent.getStringExtra(IntentHelper.ACTIONS_NAME);

    if (name == null){
        SharedPreferences settings = context.getSharedPreferences(
                DataContract.MyAppSettings.LAST_VIEWED_ACTIONS,
                Context.MODE_PRIVATE);

        name = settings.getString(
                DataContract.MyAppSettings.LAST_ACTIONS,
                DataContract.MyAppSettings.NULL);
    }

    ScheduleBuilder scheduleBuilder = ScheduleBuilder.getInternalSchedule(context, name);
    schedule = scheduleBuilder.build();
    schedule.updateTimes(context);
    schedule.updateActions(context, Calendar.getInstance());

    MyDisciplineNotificationManager.updateAllAlarm(context, schedule);
}