10

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?

Adeel Iftikhar
  • 772
  • 7
  • 30

2 Answers2

10

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

Adeel Iftikhar
  • 772
  • 7
  • 30
0

From Android O Manufacturers set Battery optimization which can prevent Broadcast receiver from calling when app is removed from Background. So you can put popup which is redirect to app battery optimize setting and user can toggle to don't optimize option if they want to run app in background. Here I have implemented Battery Optimization for different Manufacturers .

In Manifest add permission of: android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"

private fun checkAndAskForBatteryOptimization() {
  
try {


        if (Build.VERSION.SDK_INT >= 28) {

            val powerManager = this.getApplicationContext().getSystemService(POWER_SERVICE) as PowerManager
            if (!powerManager.isIgnoringBatteryOptimizations(getPackageName())) {


                val intent = Intent()
                intent.action = "android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"
                if (Build.MANUFACTURER.toUpperCase() == "VIVO") {
                    startActivityForResult(Intent("android.settings.SETTINGS"), 50)
                    headsUpNotification()
                } else if (Build.MANUFACTURER.toUpperCase() == "OPPO" || Build.MANUFACTURER.toUpperCase() == "REALME") {
                    val intent3 = Intent("android.settings.APPLICATION_DETAILS_SETTINGS")
                    intent3.data = Uri.fromParts("package", packageName, null as String?)
                    startActivityForResult(intent3, 50)
                    headsUpNotification()
                } else if (Build.MANUFACTURER.toUpperCase() == "XIAOMI") {
                    intent.action = "android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"
                    intent.data = Uri.parse("package:" + packageName)
                    startActivityForResult(intent, 50)
                    headsUpNotification()
                } else if (Build.MANUFACTURER.toUpperCase().contains("ONEPLUS")) {
                    val intent4 = Intent("android.settings.APPLICATION_DETAILS_SETTINGS")
                    intent4.data = Uri.fromParts("package", packageName, null as String?)
                    startActivityForResult(intent4, 50)
                    headsUpNotification()
                } else if (Build.MANUFACTURER.toUpperCase().contains("SAMSUNG")) {

                } else {
                    intent.data = Uri.parse("package:" + packageName)
                    startActivityForResult(intent, 50)
                }
            } else {

            }
        } else {

        }
    } catch (e: java.lang.Exception) {

    }
}
Liju Thomas
  • 1,054
  • 5
  • 18
  • 25