On Boot Broadcast Receiver not working in miui of Xiaomi (Poco x3)

Viewed 616

I know many similar questions have been asked before, but I couldn't make it work no matter what solution I tried.

I have a broadcast receiver code like the following.

class OnBootBroadcast : BroadcastReceiver() {

    override fun onReceive(context: Context?, intent: Intent?) {
        logD("onReceive() started -> intent action: [${intent?.action}]")

        // this is only to test if on boot broadcast is working       
        context?.let {
            val i = Intent()
            i.setClass(it, MainActivity::class.java)
            i.flags = Intent.FLAG_ACTIVITY_NEW_TASK;
            it.startActivity(i);
        }
        // tried to test by adding notification as well, didn't show

        // do stuff here
    }
}

My manifest file is like

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
    android:name=".CustomApplication"
    android:allowBackup="true"
    android:fullBackupContent="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".view.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".broadcast.NotificationBroadcast" />
    <receiver
        android:name=".broadcast.OnBootBroadcast"
        android:enabled="true" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>

    <provider
        android:name="androidx.work.impl.WorkManagerInitializer"
        android:authorities="${applicationId}.workmanager-init"
        tools:node="remove" />
</application>

I know Chinese custom ROMs like MIUI kill apps' background services. To handle that, I tried the following things.

  1. Turn on "Autostart" in Security > Manage apps This fixed issue of work manager background service not working when app is closed by swiping. But didn't fix the broadcast issue.

  2. Set "No restrictions" in Settings > Battery & Performance > App battery saver

I am trying to use the on boot broadcast to re-add some alarm managers for exact timed notifications. If there is some alternative which can achieve this, that info would be appreciated too.

Thanks in advance :)

2 Answers

I had the exact same problem with my Mi phone. I tried many suggested solutions but it didn't work. So I got a bit carried away and added every possible BOOT_COMPLETE trigger to my intent-filter and it worked.

        <receiver android:name=".receiver.BootReceiver"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
                <action android:name="android.intent.action.REBOOT" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
                <action android:name="android.intent.action.ACTION_SHUTDOWN" />
            </intent-filter>
        </receiver>

In the end, I didn't need to change anything in manifest file. Only android.intent.action.BOOT_COMPLETED was fine as the rest of the implicit broadcasts are not in exception list.

The main problem was that MIUI is taking some minutes to start the broadcast, which I was not noticing in the log. And when it did start, since the test code was starting an activity from background process, MIUI was killing it (according to log, starting a foreground UI is not enabled for the background process started by MIUI for boot broadcast). So after I removed the activity starting code, further logs started showing too.

Related