Android的AlarmManager停止活动时死亡 [英] Android AlarmManager stops when activity die

查看:166
本文介绍了Android的AlarmManager停止活动时死亡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始AlarmManager与PendingIntent和几部电话报警没有响应。在某些设备上的工作确定别人失败。我曾在不同的手机一些测试。

Nexus的工程确定,同时三星Galaxy S4变焦(4.2)工作正常。

三星注2(4.3)工作正常。

OPPO(4.4.4)报警死亡。

我也实施了哪些工作,因为他们应该在所有设备上广播接收机。

  Log.v(TAG,启动报警);

    意图int​​entAlarm =新的意图(背景下,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);
 

解决方案

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

另外,我想这可能是OEM /制造商特定固件/ OS issue.To检查报警是否已被实际安排使用亚行外壳dumpsys报警并检查是否你的应用程序警报被实际计划。

要检查它是否在停止状态下使用下面的命令:

亚行外壳dumpsys包com.package.name并检查     停止=真正的

要了解更多关于停止状态是指:

 上停止应用程序启动控制

从安卓3.1开始,系统的软件包管理器会跟踪那些处于停止状态的应用,并提供了从后台进程和其他应用程序的控制他们推出的一种手段。

注意,应用程序的停止状态是不一样的活动的停止状态。该系统独立管理这两个停止状态。

该平台定义了两种新的意图的标志可以让发件人指定意向是否应该允许激活停止的应用程序组件。

FLAG_INCLUDE_STOPPED_PACKAGES  - 包括在解决对潜在目标名单停止应用程序的意图过滤器。
FLAG_EXCLUDE_STOPPED_PACKAGES  - 排除从潜在目标名单停止应用程序的意图过滤器。
如果没有这些标志或两者意图定义,默认的行为是包括在潜在目标名单停止应用程序的过滤器。

注意,系统增加FLAG_EXCLUDE_STOPPED_PACKAGES到所有的广播意图。它这样做是从后台服务prevent广播从stoppped应用程序无意或不必要的启动组件。后台服务或应用程序可以通过添加FLAG_INCLUDE_STOPPED_PACKAGES标志广播应该允许激活停止应用程序意图覆盖此行为。

应用是在停止状态被首先安装时,但尚未启动,当它们由用户手动停止(在管理应用程序)。
 

请注意的停止状态是从应用程序的过程不是运行的不同。

I start AlarmManager with PendingIntent and on few phones Alarm is not responding. On some devices is working ok on others it fails. I have made a few tests on different phones.

Nexus works ok, also Samsung Galaxy S4 zoom (4.2) works ok.

Samsung note 2 (4.3) works ok.

OPPO (4.4.4) alarm dies.

I have also implemented broadcast receivers which are working as they should on all devices.

    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);

解决方案

Check whether the app is in stopped state.When the app is in stopped state it wont receive any alarm or events.

Also, I guess it might be OEM/manufacturer specific firmware/OS issue.To check whether the alarm has being actually schedule use adb shell dumpsys alarm and check whether your app alarm has being actually scheduled.

To check whether it is in stopped state use the following command:

adb shell dumpsys package "com.package.name" and check "stopped=true"

To know more about stopped state refer:

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).

Please note stopped state is different from app process not running.

这篇关于Android的AlarmManager停止活动时死亡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆