Search code examples
androidalarmmanager

Android AlarmManager stops when activity die


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

Solution

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