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?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With