当应用在后台被杀死并且设备被锁定时,警报管理器将无法工作

编程入门 行业动态 更新时间:2024-10-09 13:25:39
本文介绍了当应用在后台被杀死并且设备被锁定时,警报管理器将无法工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在这种情况下已经停留了很长时间...

I have been stuck in this situation for a long time...

我想使用警报管理器在特定时间显示通知,现在在以下情况下正常工作:

I want to use the alarm manager to show notification at the specific time, and now it worked in the situation listed below:

  • 当应用在后台运行时,通知将在正确的时间显示,无论设备是否已锁定。
  • 应用程序在后台被杀死后,当设备未锁定时,我仍会收到正确的通知,但是当设备锁定时,事情会出错。无法接收任何通知。
  • 这是代码AlarmReceiver.java,所有必需的权限已添加到AndroidManifest.xml中:

    Here's the code AlarmReceiver.java, all the needed permission has been added into AndroidManifest.xml already:

    @Override public void onReceive(Context context, Intent intent) { WakeLocker.acquire(context); String action = intent.getAction(); Log.d(TAG, action); //when app is killed and device is locked, no info is shown at the logcat if (ACTION_ALARM.equals(action)) { Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(2 * 1000); notify(context, "Jello!"); } WakeLocker.release(); } public static void alarm(Context context) { AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, AlarmReceiver.class); intent.setAction(ACTION_ALARM); PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 5 * 1000, pi); } else { alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 5 * 1000, pi); } } private void notify(Context context, String msg) { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, InfoActivity.class), 0); Notification notification = new NotificationCompat.Builder(context) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(context.getString(R.string.alarm)) .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) .setContentText(msg) .setAutoCancel(true) .setContentIntent(contentIntent).build(); notificationManager.notify(1, notification); }

    添加的权限:

    <uses-permission android:name="android.permission.WAKE_LOCK"/> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <uses-permission android:name="android.permission.VIBRATE"/>

    推荐答案

    我刚刚找到解决方案,将名为FLAG_INCLUDE_STOPPED_PACKAGES的标志设置为发出警报的意图,一切都会顺利进行。 这是 Android开发人员中的插图

    I just found the solution, set flag named FLAG_INCLUDE_STOPPED_PACKAGES to intent of the alarm, things will go right. Here are the illustration in Android Developers

    更多推荐

    当应用在后台被杀死并且设备被锁定时,警报管理器将无法工作

    本文发布于:2023-11-27 11:13:21,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1637835.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:管理器   警报   用在   被杀   后台

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!