亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 193 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號