Search code examples
androidbackgroundbroadcastreceiver

Broadcast receiver is not working only when app is closed in android pie


I am using Broadcast Receiver to trigger for incoming messages every time. Its working fine in Android O either app is closed or not. But in Android P its only working when app is live and when app is closed its not working. It should always work either app is close or not in Android P. I followed this link and many others but problem is still there.

Receiver Registration in Manifest

<receiver
            android:name=".Broadcast.SmsListener"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.BROADCAST_SMS">
            <intent-filter android:priority="999">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

Broadcast Receiver Class

    public class SmsListener extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("Resulted12", "Into onReceive()");
        context.startService(new Intent(context, BackgroundService.class));
    }
}

Is there anything else which i missed?


Solution

  • After spending few days I found the real issue.

    My code was working fine on Android O and Android P both in foreground and background but when I clear the app from the recent apps list, it stops working on some devices because of

    Manufacturers have added task manager feature by default which force stops the apps for memory/battery management. But few applications like Whatsapp, Facebook works. This can be because they would have whitelisted the most famous applications.

    For more information follow the below link

    clear Recent apps wipe the apps memory and my receiver stopped working