【发布时间】:2021-08-21 20:14:51
【问题描述】:
美好的一天, 我在我的 Android 应用程序中内置了一个计时器,它会在一定时间后发送一条消息。为此,我添加了一个带有待处理意图的 AlarmManager。当我打开应用程序时,一切正常。它也适用于锁定屏幕。但是当我关闭应用程序时,未决意图不再被激活,并且在给定时间没有出现任何消息。 这是我的警报管理器:
Intent notifyIntent = new Intent(getContext(), MyReceiver.class);
alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
pendingIntent = PendingIntent.getBroadcast
(getContext(), NOTIFICATION_ID, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//when the alarm gets activated:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+delta_time2, pendingIntent);
}
else {
alarmManager.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+delta_time2, pendingIntent);
}
在 IntentService-Class 中(声明和调用通知的地方):
Intent notifyIntent = new Intent(this, BigTextMainActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent notifyPendingIntent =
PendingIntent.getActivity(
this,
0,
notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
Intent snoozeIntent = new Intent(this, BigTextIntentService.class);
snoozeIntent.setAction(BigTextIntentService.ACTION_SNOOZE);
PendingIntent snoozePendingIntent = PendingIntent.getService(this, 0, snoozeIntent, 0);
NotificationCompat.Action snoozeAction =
new NotificationCompat.Action.Builder(
R.drawable.ic_alarm_white_48dp,
"Snooze",
snoozePendingIntent)
.build();
Intent dismissIntent = new Intent(this, BigTextIntentService.class);
dismissIntent.setAction(BigTextIntentService.ACTION_DISMISS);
PendingIntent dismissPendingIntent = PendingIntent.getService(this, 0, dismissIntent, 0);
NotificationCompat.Action dismissAction =
new NotificationCompat.Action.Builder(
R.drawable.ic_cancel_white_48dp,
"Dismiss",
dismissPendingIntent)
.build();
NotificationCompat.Builder notificationCompatBuilder =
new NotificationCompat.Builder(
getApplicationContext(), notificationChannelId);
GlobalNotificationBuilder.setNotificationCompatBuilderInstance(notificationCompatBuilder);
Notification notification = notificationCompatBuilder
.setStyle(bigTextStyle)
.setContentTitle(bigTextStyleReminderAppData.getContentTitle())
.setContentText(bigTextStyleReminderAppData.getContentText())
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(
getResources(),
R.drawable.ic_alarm_white_48dp))
.setContentIntent(notifyPendingIntent)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setCategory(Notification.CATEGORY_REMINDER)
.setPriority(bigTextStyleReminderAppData.getPriority())
.setVisibility(bigTextStyleReminderAppData.getChannelLockscreenVisibility())
.addAction(snoozeAction)
.addAction(dismissAction)
.build();
//I am not quite shure, if I declared the foregroundservice for the notification at the right place
startForeground(NOTIFICATION_ID, notification);
mNotificationManagerCompat.notify(NOTIFICATION_ID, notification);
Vibrator vibrator = (Vibrator) getApplication().getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2 * 1000);
这些是我的权限:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
感谢您的帮助! (顺便说一句,如果这个问题很愚蠢或者是因为我没有真正处理这个话题,只是想快速为我的应用程序设置警报,请提前抱歉)
【问题讨论】:
-
你在什么设备上测试这个?它运行的是什么版本的 Android?
-
AlarmManager 将在其关闭时触发
MyReceiver。您是否添加了日志记录以查看是否发生这种情况?MyReceiver.onReceive()的代码在哪里? -
我在运行 Android 10 的设备上对其进行了测试。我检查了 logcat 和消息“java.lang.RuntimeException: Unable to start receiver com.example.myapplication.ui.dashboard.MyReceiver : java.lang.IllegalStateException: 不允许启动服务 Intent { cmp=com.example.myapplication/.ui.dashboard.MyNewIntentService }: 应用程序在后台 uid UidRecord{1ea9ce7 u0a353 RCVR 空闲更改:idle|uncached procs:1 seq (0,0,0)}”出现。在模拟器设备(在 Android 11 上)上一切正常。
-
MyReceiver.onReceive() 的代码就是:“public class MyReceiver extends BroadcastReceiver { public MyReceiver() { } @Override public void onReceive(Context context, Intent intent) { Intent intent1 = new Intent (context, MyNewIntentService.class); context.startService(intent1); } }"
-
我必须纠正自己:它也不适用于 Android 模拟器。出现相同的消息,说明我的应用程序在后台。为了让我的应用保持在前台,我需要进行哪些更改?
标签: android permissions alarmmanager android-pendingintent