- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在寻找一种在 Settings
中创建 preference
的方法,以便在 Android 中的一天中的特定时间(由用户在设置中设置)发送通知应用程序。我看过不同的线程,例如 this ,但是这在 Android Oreo 中不起作用。
有人可以帮我解决这个问题或给我指点教程吗?
最佳答案
在查看不同的帖子和对 AlarmManager
的一些研究之后实现,这对我有用。
它的基础是 this发布和安排重复警报 Android Documentation .
这是我当前的实现:
我有一个 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);
}
}
}
}
最后不要忘记将这些权限添加到 AndroidManidest
:
<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" />
Notification
应该在 TimePicker
指定的一天中的特定时间由此设置如果用户启用了 SwitchPreference
.
更新(2022 年 8 月):适用于 Android 12 设备
要使其适用于 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 开始,需要在创建时使用 FLAG_IMMUTABLE 或 FLAG_MUTABLE 明确指定 PendingIntents 的可变性。您可以找到有关 PendingIntent 标志的更多信息 here
关于android - 如何在 Android Oreo 中的特定时间在 Android 上发出通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51343550/
如何从 BroadcastReceiver 发出通知(不能使用大多数方法,也不能使用“this”)?我需要它来打开一个包含数据库信息的 Activity 我已经做到了,但现在必须的方法不起作用,我不能
所以基本上我会更新我的应用程序的 GUI,所以我使用静态方法 void javax.swing.SwingUtilities.invokeLater(Runnable doRun) 但我希望将初始化代
curl_setopt_array( $ch, $curl_opt ); 这段代码给出了一个通知 Notice: Array to string conversion in ... 这是 $curl_
我正在用 PHP 构建一个网站,以 JSON 格式处理 Redis 中的 session 。 这样 PHP 解释器和 Node.js 服务器都可以访问 session 。 我现在想做的是在所述网站中添
我想在单击“添加到购物车”时使用 ajax laravel 发出通知,并且通知成功或失败。 这是我的ajax脚本 $('#formCart').on('click','.add_cart_b
我是一名优秀的程序员,十分优秀!