美好的一天,我在 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);
}
Run Code Online (Sandbox Code Playgroud)
在 IntentService 类中(声明和调用通知的地方):
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, …Run Code Online (Sandbox Code Playgroud)