第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

AlarmManager 不觸發(fā) AlarmNotificationReceiver

AlarmManager 不觸發(fā) AlarmNotificationReceiver

翻過高山走不出你 2023-05-10 15:41:28
我正在構(gòu)建一個(gè)讓用戶選擇困難的健身房應(yīng)用程序。難度根據(jù)時(shí)間設(shè)置,用戶查看難度后點(diǎn)擊健身房姿勢。時(shí)間將根據(jù)困難運(yùn)行,如果時(shí)間用完,將觸發(fā)警報(bào)。但在我的條件下,時(shí)間用完后警報(bào)不會(huì)觸發(fā)。我已經(jīng)在清單中添加了接收器 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貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊

在 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>

像這樣設(shè)置你的意圖并像這樣設(shè)置警報(bào):


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在設(shè)置的確切時(shí)間被調(diào)用。


請注意,在 Android Oreo 中,通知需要顯示通知渠道。

像這樣創(chuàng)建通知通道:


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);

}

像這樣創(chuàng)建通知:


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);


查看完整回答
反對 回復(fù) 2023-05-10
  • 1 回答
  • 0 關(guān)注
  • 191 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號