Boot BroadcastReceiver does not work on Xiaomi devices - android

I have a following BroadcastReceiver which should run after boot completion. I have tested it on my Xiaomi device (Redmi 1s), it's not running, while on other devices like Samsung it's running as expected.
public class DeviceBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Toast.makeText(context, "I am Running", Toast.LENGTH_SHORT).show();
}
}
}
I have set permission in Manifest.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
And following is my broadcast receiver:
<receiver android:name=".receiver.DeviceBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>

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:
Open Security app on your phone.
Tap on Permissions, it'll show you two options: Autostart and
Permissions
Tap on Autostart, it'll show you list of apps with on or off toggle
buttons.
Turn on toggle of your app, you're done!
Now, reboot your phone, you'll see a Toast message I am Running

Related

BroadcastReceiver doesnt receive when app is closed

I know this question is similar to many questions of BroadcastReceiver but as I read, non of them have solutions.
the tutorial of BroadcastReceiver tells it will work even app was not running in the background, my question is why I can not use it when app is not running
I tried to call broadcast from main activity, use service and ....
but non of them solved my problem.
here is my CODE:
MyReceiver java Class:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"BroadCast Trigger",Toast.LENGTH_SHORT).show();
}
}
Also MyManifest Code:
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.media.VOLUME_CHANGED_ACTION" />
</intent-filter>
</receiver>
As I found there is no way to active BroadcastReceiver in Huawei Devices programmatically, but here is a solution to find device type and do needed action in this regard such as show an alert to user to activate it manually.
if ("huawei".equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
// Do Needed Action
}
I faced with same problem on Huawey Honor with Android 7. On Sony and ZTE devices BroadcastReceiver works as expected. But on Honor it work some time and suddenly stop.
I discover, that problem not related with re-boot. I reboot device and broadcast receiver work after it. But sometimes, it stop without rebooting.
First i add my app to protected list according this solution:
"Protected Apps" setting on Huawei phones, and how to handle it
But it didn't help :(
Then, i add a fake accessibility service to my app, according to this recommendation:
Broadcast Receiver Not Working After Device Reboot in Android
And problem was solved!

Auto Start Service after booting device even app not opened atonce. Android

I am making a system app. In that I have a requirement is to run a service after boot load WITHOUT A SINGLE TIME LUNCHING THE APP.
this question is bit similar to this
System App auto starting
But it does not have any appropriate solution.
Also read that BOOT_COMPLETE_RECEIVER works only when app launched at once.
Use Broadcast Receiver for getting action after that start service from that broad cast receiver and use START_STICKY service so that if it is killed because of some priority than it's recreate and if you want to continuously run this service in background than WAKE_Lock that service and using Alarm Manager check it is runnig or not.
Set this in manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:name="AutoStart"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
AutoStart class
public class AutoStart extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
// Start your service here..
}
}
}
Thanks all for your effort, I finally got answer.
Solution:
As I stated my app is system app, System work even they not opened at once. Because they are not in stopped state i.e enforce after android 3.1.
Secondly If a user app wants this then Its manifest don't have any "android.intent.category.LAUNCHER" category in activity.
Also by adb you can enable your app by using this command
adb shell am broadcast -a com.example.demo.action.LAUNCH --include-stopped-packages (This is not tested)
Some good link to this:
http://droidyue.com/blog/2014/01/04/package-stop-state-since-android-3-dot-1/
Static BroadcastReceiver not Working after Installation from ADB

BroadcastReceiver can't receive broadcast after reboot when boot completed permission id disabled

In XiaoMi 2A, Android Version 4.1, I cann't receive any broadcast after reboot unless I launch my app. XiaoMi disabled boot completed broadcast for apps in default unless users open it.
I have read a lot of posts here:
Broadcast receiver not working in ICS if the app is not started atleast once
http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html
Cannot receive broadcast in ICS
They all said google just made it happen after Android version 3.0. The truth is with my Galaxy Nexus of Android 4.3, the custom broadcast(not boot completed) can be received even app is not launched before after reboot.
Even with XiaoMi System, broadcast can be received after I open boot completed permission for my app. Can someone tell me why?
Here is the sending Intent:
Intent intent = new Intent();
intent.setAction("com.test.test");
intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES | intent.getFlags());
context.sendBroadcast(intent);
and the receive is below:
<receiver
android:name="com.test.TestReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.test.test"></action>
</intent-filter>
</receiver>
public class TestReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.test.test")) {
Log.e("Chris", "received test action");
}
}
}
In Xiaomi, you can select which all apps can auto start.
Security App > Permissions > Autostart
You can allow autostart for your app here.
I think this will fix your problem

How can I discover that my device has rebooted, given that HONEYCOMB BOOT COMPLETED is no longer supported?

I am developing app in Android 4.0.3. I've read that the HONEYCOMB BOOT COMPLETED event is no longer supported, according to Google's documentation.
Given this, how can I discover that my device has rebooted?
CODE -
Java Class :-
public class MyStartupIntentReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Logger.i("Device", "REBOOT");
Logger.i("Device", "REBOOT");
Logger.i("Device", "REBOOT");
Logger.i("Device", "REBOOT");
Logger.i("Device", "REBOOT");
}
}
Manifest File :-
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="MyStartupIntentReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
sorry , but perhaps you are wrong about the documentation , any way make sure that you are including <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> permission in your manifest . I use this broadcast in many apps and they works great.
Make sure you know aslo about this info BOOT_COMPLETE is sent to applications before external storage is mounted. So if application is installed to external storage it won't receive BOOT_COMPLETE broadcast message.
I am developing app in Android 4.0.3. I've read that the HONEYCOMB BOOT COMPLETED event is no longer supported, according to Google's documentation.
You misinterpreted the documentation. BOOT_COMPLETED is supported. However, it does require some component of your application to be run manually, before any registered BroadcastReceiver will work. Typically, this is done by starting an activity. Hence, if you just install an app and restart the device, you will not get control at boot time.
You can read more about this in the "Launch controls on stopped applications" of the Android 3.1 release notes, as well as in this blog post.

broadcastreceiver not working while phone is booting in Android

I have created a lock screen for ICS and it is placed in the frameworks and we can open applications using this. For the user effects I have started an animation when the lock screen is displayed. This animation is started using SCREEN_ON broadcastereceiver. But when the phone is booting up even though I registered broadcastereceiver it is not reaching to onReceive() and the animation is not starting. While phone bootup is taking place I thought that this broadcast is not having higher priority to execute and set the priority as high but it is also not working.
Check this out..
I also had the same problem Broadcast not invoking:
According to my knowledge the problem is with Android HoneyComb and ICS. I have tested same application on HoneyComb,ICS, Ginger Bread and Froyo. Worked perfectly for Froyo and Ginger bread but not for honeycomb or ics.
if you are just shutting down your phone and switching it on, then you might not get this broadcast. try to restart your phone. this would work.
you havent added any code in your question, so it woulld be really difficult to guess what you are missing and what probably is going wrong.
Well the way to receive system startup is as followsRegister receiver in manifest:
<receiver android:name=".StartupReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
Use the permission in manifest<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />Write the class below in your package
public class StartupReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Log.e("MyStrtupIntentReceiver" ,"################# onReceive() system boot");
}
}

Categories

Resources