我正在寻找一种在Android应用程序中创建"偏好设置",使得用户可以在设置中设定特定时间,在该时间发送通知的方法。我查看了不同的线程,例如这个,但是在Android Oreo上无法使用。
有人能帮助我或者指导我如何实现吗?
我正在寻找一种在Android应用程序中创建"偏好设置",使得用户可以在设置中设定特定时间,在该时间发送通知的方法。我查看了不同的线程,例如这个,但是在Android Oreo上无法使用。
有人能帮助我或者指导我如何实现吗?
查看不同帖子并进行了一些对AlarmManager
实现的研究后,这是对我有效的内容。
这是我的当前实现:
我有一个SwitchPreference
和一个TimePicker
实现在Settings
中。
SwitchPreference
询问用户是否要启用每日重复通知。
TimePicker
设置通知时间。
在MainActivity
的OnCreate
方法或者你正在读取SharedPreferences
的任何地方做如下操作:
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
Boolean dailyNotify = sharedPref.getBoolean(SettingsActivity.KEY_PREF_DAILY_NOTIFICATION, true);
PackageManager pm = this.getPackageManager();
ComponentName receiver = new ComponentName(this, DeviceBootReceiver.class);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// if user enabled daily notifications
if (dailyNotify) {
//region Enable Daily Notifications
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, sharedPref.getInt("dailyNotificationHour", 7));
calendar.set(Calendar.MINUTE, sharedPref.getInt("dailyNotificationMin", 15));
calendar.set(Calendar.SECOND, 1);
// if notification time is before selected time, send notification the next day
if (calendar.before(Calendar.getInstance())) {
calendar.add(Calendar.DATE, 1);
}
if (manager != null) {
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
}
//To enable Boot Receiver class
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
//endregion
} else { //Disable Daily Notifications
if (PendingIntent.getBroadcast(this, 0, alarmIntent, 0) != null && manager != null) {
manager.cancel(pendingIntent);
//Toast.makeText(this,"Notifications were disabled",Toast.LENGTH_SHORT).show();
}
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
接下来添加AlarmReceiver
类,该类实现BroadcastReceiver
,代码如下:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(Objects.requireNonNull(context));
SharedPreferences.Editor sharedPrefEditor = prefs.edit();
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingI = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("default",
"Daily Notification",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("Daily Notification");
if (nm != null) {
nm.createNotificationChannel(channel);
}
}
NotificationCompat.Builder b = new NotificationCompat.Builder(context, "default");
b.setAutoCancel(true)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher_foreground)
.setTicker("{Time to watch some cool stuff!}")
.setContentTitle("My Cool App")
.setContentText("Time to watch some cool stuff!")
.setContentInfo("INFO")
.setContentIntent(pendingI);
if (nm != null) {
nm.notify(1, b.build());
Calendar nextNotifyTime = Calendar.getInstance();
nextNotifyTime.add(Calendar.DATE, 1);
sharedPrefEditor.putLong("nextNotifyTime", nextNotifyTime.getTimeInMillis());
sharedPrefEditor.apply();
}
}
}
如果设备被关闭或重新启动,则系统将关闭AlarmManager
,因此在BOOT COMPLETE
上重新启动它,请添加此类:
public class DeviceBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Objects.equals(intent.getAction(), "android.intent.action.BOOT_COMPLETED")) {
// on device boot complete, reset the alarm
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(Objects.requireNonNull(context));
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, sharedPref.getInt("dailyNotificationHour", 7));
calendar.set(Calendar.MINUTE, sharedPref.getInt("dailyNotificationMin", 15));
calendar.set(Calendar.SECOND, 1);
Calendar newC = new GregorianCalendar();
newC.setTimeInMillis(sharedPref.getLong("nextNotifyTime", Calendar.getInstance().getTimeInMillis()));
if (calendar.after(newC)) {
calendar.add(Calendar.HOUR, 1);
}
if (manager != null) {
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
}
}
最后不要忘记将这些权限添加到AndroidManifest
中:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
并在AndroidManifest
中注册您的接收器。
<application
<!--YOUR APPLICATION STUFF-->
<receiver android:name=".DeviceBootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".AlarmReceiver" />
通知
应在由TimePicker
指定的特定时间设置,并且如果用户启用了SwitchPreference
,则应该设置它。更新(2022年8月):对于Android 12设备,您需要将pendingIntent
的标志从0修改为FLAG_IMMUTABLE
,如下所示:PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, FLAG_IMMUTABLE);
在 Build.VERSION_CODES.R 版本之前,默认情况下假定 PendingIntents 是可变的,除非设置了 FLAG_IMMUTABLE 标志。从 Build.VERSION_CODES.S 开始,在创建 PendingIntents 时必须显式指定其可变性,可以使用 FLAG_IMMUTABLE 或 FLAG_MUTABLE 标志。您可以在此处找到有关 PendingIntent 标志的更多信息。
在StackOverflow上有很多关于如何实现这种功能的例子,但不幸的是,它们都无法正常工作,因为谷歌改变了AlarmManager的工作方式。
唯一允许开发人员在特定时间唤醒设备的AlarmManager选项是AlarmManager.setAlarmClock。
除了闹钟外,您还可以设置一个意图,以便在点击闹钟时启动您的应用程序。
通过高优先级FCM(Firebase Cloud Message),也可以在特定时间唤醒设备。
但总之,没有其他选择:'setExact'和相关方法不再像名称所述那样工作。