gpt4 book ai didi

java - 如何在 Android 中安排通知

转载 作者:IT老高 更新时间:2023-10-28 21:05:03 24 4
gpt4 key购买 nike

我正在尝试为将来的某个时间设置通知。我有创建通知的代码,但找不到安排它的选项。

如何安排通知?

最佳答案

不适用于 OREO+(编辑)

上面的答案很好 - 但不要考虑用户重启设备的可能性(这会清除 AlarmManager 安排的 PendingIntent)。

您需要创建一个 WakefulBroadcastReceiver,其中将包含一个 AlarmManager 来安排发送 PendingIntent。当 WakefulBroadcastReceiver 处理 Intent 时 - 发布您的通知并指示 WakefulBroadcastReceiver 完成。

WakefulBroadcastReceiver

    /**
* When the alarm fires, this WakefulBroadcastReceiver receives the broadcast Intent
* and then posts the notification.
*/
public class WakefulReceiver extends WakefulBroadcastReceiver {
// provides access to the system alarm services.
private AlarmManager mAlarmManager;

public void onReceive(Context context, Intent intent) {
//// TODO: post notification
WakefulReceiver.completeWakefulIntent(intent);
}

/**
* Sets the next alarm to run. When the alarm fires,
* the app broadcasts an Intent to this WakefulBroadcastReceiver.
* @param context the context of the app's Activity.
*/
public void setAlarm(Context context) {
mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, WakefulReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//// TODO: use calendar.add(Calendar.SECOND,MINUTE,HOUR, int);
//calendar.add(Calendar.SECOND, 10);

//ALWAYS recompute the calendar after using add, set, roll
Date date = calendar.getTime();

mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, date.getTime(), alarmIntent);

// Enable {@code BootReceiver} to automatically restart when the
// device is rebooted.
//// TODO: you may need to reference the context by ApplicationActivity.class
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}

/**
* Cancels the next alarm from running. Removes any intents set by this
* WakefulBroadcastReceiver.
* @param context the context of the app's Activity
*/
public void cancelAlarm(Context context) {
Log.d("WakefulAlarmReceiver", "{cancelAlarm}");

mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, WakefulReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

mAlarmManager.cancel(alarmIntent);

// Disable {@code BootReceiver} so that it doesn't automatically restart when the device is rebooted.
//// TODO: you may need to reference the context by ApplicationActivity.class
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}

引导接收器

    public class BootReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
context = ApplicationActivity.class;
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, WakefulReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//// TODO: use calendar.add(Calendar.SECOND,MINUTE,HOUR, int);
//calendar.add(Calendar.SECOND, 10);

//ALWAYS recompute the calendar after using add, set, roll
Date date = calendar.getTime();

alarmManager.setExact(AlarmManager.RTC_WAKEUP, date.getTime(), alarmIntent);
}
}
}

AndroidManifest.xml

<receiver android:name=".WakefulReceiver"/>

<receiver android:name=".BootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>

关于java - 如何在 Android 中安排通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36902667/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com