为了账号安全,请及时绑定邮箱和手机立即绑定

AlarmManager 不触发 AlarmNotificationReceiver

AlarmManager 不触发 AlarmNotificationReceiver

翻过高山走不出你 2023-05-10 15:41:28
我正在构建一个让用户选择困难的健身房应用程序。难度根据时间设置,用户查看难度后点击健身房姿势。时间将根据困难运行,如果时间用完,将触发警报。但在我的条件下,时间用完后警报不会触发。我已经在清单中添加了接收器 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);    }}
查看完整描述

1 回答

?
慕斯王

TA贡献1864条经验 获得超2个赞

在 AndroidManifest.xml 中像这样注册你的接收器


<receiver

    android:name="com.example.AlarmNotificationReceiver"

    android:enabled="true"

    android:exported="false">

    <intent-filter>

        <action android:name="com.example.AlarmNotificationReceiver" />

    </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){

    NotificationChannel channel = new NotificationChannel("default","Default",NotificationManager.IMPORTANCE_DEFAULT);

    manager.createNotificationChannel(channel);

}

像这样创建通知:


Notification notification = new NotificationCompat.Builder(context, "default")

        .setDefaults(Notification.DEFAULT_ALL)

        .setWhen(System.currentTimeMillis())

        .setSmallIcon(R.mipmap.ic_launcher_round)

        .setContentTitle("It's time")

        .setContentText("Time to training")

        .setContentInfo("Info")

        .build();

manager.notify(1, notification);


查看完整回答
反对 回复 2023-05-10
  • 1 回答
  • 0 关注
  • 145 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信