AlarmManager 不触发 AlarmNotificationReceiver

我正在构建一个让用户选择困难的健身房应用程序。难度根据时间设置,用户查看难度后点击健身房姿势。时间将根据困难运行,如果时间用完,将触发警报。但在我的条件下,时间用完后警报不会触发。我已经在清单中添加了接收器 android:name。这是我的代码:


 private void saveAlarm(boolean checked) {


    if (checked) {

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        Intent alarmIntent;

        PendingIntent pendingIntent;

        int Hour, Minute;


        alarmIntent = new Intent(Setting.this, AlarmNotificationReceiver.class);

        pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);


        if (Build.VERSION.SDK_INT >= 23) {


            Hour = timePicker.getHour();

            Minute = timePicker.getMinute();


        } else {


            Minute = timePicker.getCurrentMinute();

            Hour = timePicker.getCurrentHour();

        }


        Date dat = new Date();

        Calendar cal_alarm = Calendar.getInstance();

        Calendar cal_now = Calendar.getInstance();

        cal_now.setTime(dat);

        cal_alarm.setTime(dat);

        cal_alarm.set(Calendar.HOUR_OF_DAY, Hour);

        cal_alarm.set(Calendar.MINUTE, Minute);

        cal_alarm.set(Calendar.SECOND, 10);

        if (cal_alarm.before(cal_now)) {

            cal_alarm.add(Calendar.DATE, 1);

        }

        alarmManager.set(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(), pendingIntent);

    }

}


翻过高山走不出你
浏览 146回答 1
1回答

慕斯王

在 AndroidManifest.xml 中像这样注册你的接收器<receiver&nbsp; &nbsp; android:name="com.example.AlarmNotificationReceiver"&nbsp; &nbsp; android:enabled="true"&nbsp; &nbsp; android:exported="false">&nbsp; &nbsp; <intent-filter>&nbsp; &nbsp; &nbsp; &nbsp; <action android:name="com.example.AlarmNotificationReceiver" />&nbsp; &nbsp; </intent-filter></receiver>像这样设置你的意图并像这样设置警报:Intent intent = Intent();intent.setClass(context,AlarmNotificationReceiver.class);intent.setAction("com.example.AlarmNotificationReceiver");PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(), pendingIntent);setExact在设置的确切时间被调用。请注意,在 Android Oreo 中,通知需要显示通知渠道。像这样创建通知通道:NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){&nbsp; &nbsp; NotificationChannel channel = new NotificationChannel("default","Default",NotificationManager.IMPORTANCE_DEFAULT);&nbsp; &nbsp; manager.createNotificationChannel(channel);}像这样创建通知:Notification notification = new NotificationCompat.Builder(context, "default")&nbsp; &nbsp; &nbsp; &nbsp; .setDefaults(Notification.DEFAULT_ALL)&nbsp; &nbsp; &nbsp; &nbsp; .setWhen(System.currentTimeMillis())&nbsp; &nbsp; &nbsp; &nbsp; .setSmallIcon(R.mipmap.ic_launcher_round)&nbsp; &nbsp; &nbsp; &nbsp; .setContentTitle("It's time")&nbsp; &nbsp; &nbsp; &nbsp; .setContentText("Time to training")&nbsp; &nbsp; &nbsp; &nbsp; .setContentInfo("Info")&nbsp; &nbsp; &nbsp; &nbsp; .build();manager.notify(1, notification);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java