Boot Broadcastreceiver Does Not Work on Xiaomi Devices

Boot BroadcastReceiver does not work on Xiaomi devices

I searched around the web and found a solution, I've decided to answer my own question. Follow the same code given in the question.

In Xiaomi devices, you just have to add your app to Autostart list, to do so, follow these simple steps given below:

  1. Open Security app on your phone.

  2. Tap on Permissions, it'll show you two options: Autostart and
    Permissions

  3. Tap on Autostart, it'll show you list of apps with on or off toggle
    buttons.

  4. Turn on toggle of your app, you're done!

Now, reboot your phone, you'll see a Toast message I am Running

BOOT COMPLETE is not working in Android (Redmi)

This is solution for your bug.

What you need to do is that , you need to mention this permission also in your Intent-Filter.

               <receiver android:name="com.example.demoapp.MyReceiver"
android:enabled="true"
android:exported="true" >

<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.REBOOT"/>
</intent-filter>
</receiver>

Here you need to do one thing also.

Go to "Settings-Apps_open your app info-Manage permisson" in yourdevice

Here check all things here.

Because for some devices it will not work.
Ex.Red mi

Try this !

Xiaomi BOOT_COMPLETED not received

Hey little late but I just got to this issue also. Problem with Xiaomi devices is that they block autostarting of the application so it does not receive the BOOT_COMPLETED Broadcasts. To be able to receive it the user has to enable the application to autostart in settings.

There are already multiple solutions for this issue: answer from Mohit Mathur or answer from Dika.

There are two main things to note:

  • You cannot actually check if the app has the autostart enabled or not.
  • This is not issue only for Xiaomi but for some Huawei, HTC and other devices too.

BroadcastReceiver stop working on Xiaomi after couple of hours

Google Awareness:

And whilst doing so, the API collates data from multiple sources to
ensure that the data provided is both accurate and of a high quality.
It also takes into account both the power and memory consumption when
accessing these signals - helping to ensure that the battery life and
memory usage of our applications do not have a detrimental effect on
the users device.

As this paragraph says:

It also takes into account both the power and memory consumption when
accessing these signals

and you say:

The problem is that everything works fine in all android version
except in Xiaomi devices. Where BroadcastReciver not receiving
anything after couple of hours

after couple of hours device may going to save more energy

You are listening for "Fence API" to register for changes in the users current environment it may consumes power and thus you do not receive broadcasts you may need to change the device settings to allow using of sensors such as location GPS and other relevant sensors

Broadcast Receiver Not Working After Device Reboot in Android

Here's a tested and working solution on both the devices that you mentioned, OnePlus and Mi.

As you said the auto-start prevention feature on OnePlus and Mi devices prevent apps from starting up their services automatically on boot complete so as to improve the overall device boot speed and battery performance. However, there's a workaround to get your app working even when this feature is turned on.

I have noticed that if you have an AccessibilityService in your app and it is turned on by the user, then your app passes the filter that these manufacturers apply and the app receives it's boot complete event and any other BroadcastReceiver works as expected.

The possible explanation of this trick can be that since AccessibilityService is a system level service, so by registering your own service you are passing the certain filter applied by these manufacturers and as soon as your custom AccessibilityService gets triggered by the OS, your app becomes active in receiving the eligible BroadcastReceiver that you had registered.

So, here's how to do it,

Start by adding this permission to your AndroidManifest.xml,

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

This will allow you to register your app's AccessibilityService with the system.

Now, add a very basic configuration for your AccessibilityService by creating a file for example my_accessibility_service.xml inside XML folder under your res folder in your project.

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityFeedbackType="feedbackSpoken"
android:description="@string/service_desc"
android:notificationTimeout="100"/>

There's just one more step left to do, define your custom AccessibilityService in your project,

public class MyAccessibilityService extends AccessibilityService {

@Override
public void onAccessibilityEvent(AccessibilityEvent event) { }

@Override
public void onInterrupt() {

}
}

Note, since you're not needing the AccessibilityService for any purpose rather than this workaround, you can leave the overridden methods empty.

Finally, just declare your AccessibilityService in your AndroidManifest.xml,

<service
android:name=".MyAccessibilityService"
android:label="@string/app_name"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService"/>
</intent-filter>

<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/my_accessibility_service"/>
</service>

That's all. Now within your app, just ask your users to turn on the accessibility service for your app from the settings and leave it on and voila! Your app works fine on all devices even where the OS puts a filter on which apps should auto-start on boot.

EDIT 1

Here's how you can check if accessibility service is turned ON or not for your app,

private static final int ACCESSIBILITY_ENABLED = 1;

public static boolean isAccessibilitySettingsOn(Context context) {
int accessibilityEnabled = 0;
final String service = context.getPackageName() + "/" + MyAccessibilityService.class.getCanonicalName();
try {
accessibilityEnabled = Settings.Secure.getInt(
context.getApplicationContext().getContentResolver(),
android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
} catch (Settings.SettingNotFoundException e) {
Log.e("AU", "Error finding setting, default accessibility to not found: "
+ e.getMessage());
}
TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');

if (accessibilityEnabled == ACCESSIBILITY_ENABLED) {
String settingValue = Settings.Secure.getString(
context.getApplicationContext().getContentResolver(),
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
if (settingValue != null) {
mStringColonSplitter.setString(settingValue);
while (mStringColonSplitter.hasNext()) {
String accessibilityService = mStringColonSplitter.next();

if (accessibilityService.equalsIgnoreCase(service)) {
return true;
}
}
}
}

return false;
}

Hope this helps.



Related Topics



Leave a reply



Submit