java - 在 Android Studio 中使用警报管理器设置多个警报会导致问题
问题描述
我目前有一个在 Android Studio 中运行的小项目,我需要帮助。我想在通知中包含提醒功能。我总共有 4 个时间选择器,我想在相应的选定时间为每个时间选择器设置警报。使用我当前的代码,计划的功能只工作了一半,存在以下问题:
如果我选择一次,那么我有时会根据需要在此时收到通知。然而,并不总是准时。大多数情况下,警报不会出现,如果出现,则在半分钟到一分钟后出现。如果我设置了所有 4 个警报,在最好的情况下,我会在最后选择的时间收到通知。在最坏的情况下,什么也不会发生。
但我想要的只是在选定的 4 次获得每日通知。
关于我的代码,我使用了一个警报管理器,我为 4 个警报调用了 4 次。我还使用触发通知的广播接收器。我RequestCode
对每个PendingIntent
.
我真的搜索了关于 SO 的所有相关帖子,但没有一个对我有用。也许我以错误的方式包含它。我希望有一个人可以帮助我。这是我的方法:
Activity.class 中的 Alarm 方法
(例如 UhrzeitInMillis 描述了 timepicker 选择的时间16.03
):
public void SetAlarm(Context context, long UhrzeitInMillis) {
Intent intent = new Intent(context, Optionen_Alarm.class);
final int id = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), id, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, UhrzeitInMillis, AlarmManager.INTERVAL_DAY, pendingIntent);
}
广播接收器(Optionen_Alarm.java):
public class Optionen_Alarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "myapp:mywakelocktag");
wl.acquire();
createNotificationChannel(context);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "1");
builder.setContentTitle("titel");
builder.setContentText("text!");
builder.setSmallIcon(R.drawable.picture);
builder.setColor(context.getResources().getColor(R.color.red));
builder.setVibrate(new long[]{0, 300, 300, 300});
builder.setLights(Color.WHITE, 1000, 5000);
builder.setAutoCancel(true);
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
builder.setStyle(new NotificationCompat.BigTextStyle().bigText("text!"));
Intent notifyIntent = new Intent(context, Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 2, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
Notification notificationCompat = builder.build();
NotificationManagerCompat managerCompat = NotificationManagerCompat.from(context);
managerCompat.notify(15, notificationCompat);
wl.release();
private void createNotificationChannel(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "name";
String description = "description";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel("1", name, importance);
channel.setDescription(description);
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
```java
解决方案
出于电池原因,从 android 6.0 开始,您在闹钟管理器上设置的时间将不能保证在您设置的完全相同的时间触发。您可以使用方法 '''setExactAndAllowWhileIdle()''' 使警报按照您的意愿行事。您可以在此处阅读有关此内容的更多信息https://developer.android.com/training/scheduling/alarms
推荐阅读
- python - 理解 Django 模型类的定义
- python - Flask web 应用无限进程启动最佳实践
- mysql - MySQL 使用 WHERE 加入同一张表
- android - 房间 UNIQUE 约束失败
- mysql - SQL:创建自动将一些值复制到另一个表中的触发器
- php - Laravel timestampTz 列的时区错误
- vb.net - 有没有办法检查是否存在引用以防止编译错误?
- c# - 单元测试错误 System.InvalidCastException:
- python-3.x - 适用于 Azure SQl 数据库和 python 的带有 python 3 的 Databricks
- c# - Roslyn SyntaxTree - 更改字段值