当活动消失时,Android AlarmManager会停止

5er*_*5er 18 android alarmmanager

我用PendingIntent启动AlarmManager,在几部手机上报警没有响应.在某些设备上工作正常,其他设备失败.我在不同的手机上做了一些测试.

Nexus工作正常,三星Galaxy S4 zoom(4.2)工作正常.

三星笔记2(4.3)工作正常.

OPPO(4.4.4)报警死机.

我还实现了广播接收器,它们可以在所有设备上正常工作.

    Log.v(TAG, "START ALARM");

    Intent intentAlarm = new Intent(context, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, 1000, 5000, pendingIntent);
Run Code Online (Sandbox Code Playgroud)

rup*_*ain 7

检查应用程序是否处于停止状态.当应用程序处于停止状态时,它不会收到任何警报或事件.

此外,我想它可能是OEM /制造商特定的固件/操作系统问题.要检查警报是否实际上正在计划使用adb shell dumpsys警报并检查您的应用程序警报是否已实际安排.

要检查它是否处于停止状态,请使用以下命令:

adb shell dumpsys包"com.package.name"并检查 "stopped = true"

要了解有关停止状态的更多信息,请

Launch controls on stopped applications

Starting from Android 3.1, the system's package manager keeps track of applications that are in a stopped state and provides a means of controlling their launch from background processes and other applications.

Note that an application's stopped state is not the same as an Activity's stopped state. The system manages those two stopped states separately.

The platform defines two new intent flags that let a sender specify whether the Intent should be allowed to activate components in stopped application.

FLAG_INCLUDE_STOPPED_PACKAGES — Include intent filters of stopped applications in the list of potential targets to resolve against.
FLAG_EXCLUDE_STOPPED_PACKAGES — Exclude intent filters of stopped applications from the list of potential targets.
When neither or both of these flags is defined in an intent, the default behavior is to include filters of stopped applications in the list of potential targets.

Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. It does this to prevent broadcasts from background services from inadvertently or unnecessarily launching components of stoppped applications. A background service or application can override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES flag to broadcast intents that should be allowed to activate stopped applications.

Applications are in a stopped state when they are first installed but are not yet launched and when they are manually stopped by the user (in Manage Applications).
Run Code Online (Sandbox Code Playgroud)

请注意停止状态与应用程序进程未运行不同.


Lar*_*fer 5

这里可能有几个不同的问题:

  • 您请求的警报类型(ELAPSED_REALTIME)不会唤醒设备以发出警报.相反,如果它在设备休眠时到期,它将在下次设备唤醒时传送.
  • triggerAtMillis1000是在设备启动后1秒钟请求第一个警报.如果设备已经启动并运行并且您请求此警报,则第一个警报可能不会触发,并可能导致后续警报无法安排.这只是一个猜测,我没有通过查看4.4.4 AOSP来源进行验证

在API 19(Android 4.4)中更改了警报处理以处理警报计时器的整理(默认情况下都是不精确的),并且此更改可能会影响第二个项目符号的内容.您可以尝试将triggerAtMillis值更改为(SystemClock.elapsedRealtime() + 1000)

请注意,如果你需要的设备从睡眠状态唤醒,你需要使用一个_WAKEUP报警变种,也有你BroadcastReceiver走哪你唤醒锁定Service或者Activity释放时,它完成处理报警.