Search code examples
androidbroadcastreceiveralarmmanagerandroid-pendingintentandroid-broadcast

AlarmManager is discarded when application is cleared from background


This is how I am creating an alarm. When the app is still running in background the alarm is fired. but when it is cleared from the recent apps, the alarm is getting discarded. I am registering a new receiver and providing a unique action for differentiation between two alarms. Is there any mistake in my code.

        String filter_action = "myPackageName" + request_code_value +"_time";

        IntentFilter filter = new IntentFilter(filter_action);

        registerReceiver(new AlarmReciever(), filter);

        Intent intent = new Intent(filter_action);
        intent.putExtra(getString(R.string.get_current_intent_value), request_code_value);
        intent.putExtra(getString(R.string.alarmtext), alarm_title);
        intent.putExtra(getString(R.string.alarm_time), newAlarm_Choose_Alarm_Value.getText().toString());

        Calendar calNow = Calendar.getInstance();
        Calendar calSet = (Calendar) calNow.clone();


        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, request_code_value, intent, 0);


        calSet.setTimeInMillis(timeInMillis);

        if (calSet.compareTo(calNow) <= 0) {
            // Today Set time passed, count to tomorrow
            calSet.add(Calendar.DATE, 1);
        }

        alarmManager.set(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), pendingIntent);

Manifest file.

<receiver android:name="com.bigbangpartners.YoWakeUp.activities.AlarmReciever" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="AlarmManager_Start" />
        </intent-filter>
</receiver>

Please help me. Thanks in advance.


Solution

  • If you are registering and de-registering the BroadcastReceiver programmatically in your Activitys, then a BroadcastReceiver created in this way will remain in existence only as long as the app process is alive. So when you remove your app from the list of recent apps, this BroadcastReceiver is also, in effect, destroyed.

    On the other hand, a BroadcastReceiver registered in the manifest is a global, permanent component of an Android app that remains in existence forever, whether the app process is alive or not.

    If you want your alarms to be activated even when the app is not in the foreground, then you need to register the BroadcastReceiver in the app manifest and specify the alarm action as well. You are not doing that, and this, I believe, is the root of your problem.

    What you should do:

    1. Define your BroadcastReceiver in manifest like this:

    <receiver android:name="com.bigbangpartners.YoWakeUp.activities.AlarmReciever" >        
    </receiver>
    

    2. Remove all calls to registerReceiver() and unRegisterReceiver() in your app code as that is not needed.

    3. Define your alarm Intent like this:

    Intent intent = new Intent(this, AlarmReceiver.class);
    

    Rest of code remains same. First try this and see if it works.