Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 769
Adeel Iftikhar Avatar asked Apr 08 '20 06:04

Adeel Iftikhar


People also ask

Does broadcast receiver work in background?

A broadcast receiver will always get notified of a broadcast, regardless of the status of your application. It doesn't matter if your application is currently running, in the background or not running at all.

How do I know if my broadcast receiver is registered Android?

A simple solution to this problem is to call the registerReceiver() in your Custom Application Class. This will ensure that your Broadcast receiver will be called only one in your entire Application lifecycle.

Which broadcast receiver is available in Android?

There are mainly two types of Broadcast Receivers: Static Broadcast Receivers: These types of Receivers are declared in the manifest file and works even if the app is closed. Dynamic Broadcast Receivers: These types of receivers work only if the app is active or minimized.

How do I turn off broadcast receiver?

To stop receiving broadcasts, call unregisterReceiver(android. content. BroadcastReceiver) . Be sure to unregister the receiver when you no longer need it or the context is no longer valid.


1 Answers

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

like image 132
Adeel Iftikhar Avatar answered Oct 21 '22 07:10

Adeel Iftikhar