I am trying to get a BOOT_COMPLETED receiver to work on a Galays S5 Neo device (running 5.1.1) to start a service right after the device is fully booted.
So in the manifest i defined
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and also added a receiver for the BOOT_COMPLETED intent
<receiver android:name="com.xyzMyApp.BootCompleteReceiver" >
<intent-filter android:priority="999" >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
This works fine on nearly all devices no matter if used on battery or not. On the Galaxy S5 Neo this is only working when the device is connected to power on boot. If i use the device on battery and do a reboot, it can take up to 5 minutes until the intent is received.
I also tried to receive other broadcasts like android.net.wifi.RSSI_CHANGED or android.intent.action.USER_PRESENT or android.intent.action.TIME_TICK to maybe use them as a replacement for the boot completed event. The idea was that those events happen pretty often and so after a reboot it won't take very long until one of the receivers is triggered. Turns out that the behavior is the same, on battery it can take up to some minutes until ANY of the receivers receives ANY intent.
I also disabled all kinds of battery optimization for apps i found in the system settings but that did not solve the issue.
Thanks for every hint or idea.
Try to add QUICKBOOT_POWERON as some devices have issues in receiving BOOT_COMPLETED broadcast:
<receiver android:name="com.xyzMyApp.BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
Also make sure your app is not installed on external storage(SD card) as suggested on this answer
Related
When I restart my device, I am receiving BOOT_COMPLETED broadcast much slower (around 10-15 seconds later) than other apps.
I have seen this. But I do not think this is completely true. How (I think) I proved it wrong:
(Let's define X as an application that receives BOOT_COMPLETED broadcast faster than mine).
Installed X
Installed my application.
Restarted device.
RESULT: X received BOOT_COMPLETED broadcast very fast. My applicated received the broadcast slow.
Uninstalled both applications.
Installed my application once again.
Restarted device.
RESULT: My app received BOOT_COMPLETED broadcast slow again.
Uninstalled my application.
Installed X.
Restarted device.
RESULT: X received BOOT_COMPLETED broadcast very fast once again.
CONCLUSION: My app receives BOOT_COMPLETED broadcast slow whether X is installed or not. X always receives BOOT_COMPLETED broadcast very fast. How X is being able to receive the boot completed broadcast much faster than my application even though X is installed after my application?
That is why I do not think this is completely true answer. There is something X is doing that causing it to receive BOOT_COMPLETED at a higher priority than my app.
Maybe there are some other broadcasts other than BOOT_COMPLETED which are much faster?
Any suggestions are appreciated.
Manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
...
<receiver
android:name=".receivers.BootCompletedIntentReceived">
<intent-filter android:priority="2147483647">
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<!--For HTC devices-->
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
BootCompletedIntentReceived.kt
class BootCompletedIntentReceived: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
//DO SOMETHING HERE
}
}
The value of priority of intent-filter must be between -1000 and 1000.
Try this:
<intent-filter android:priority="999">
I suspect that the app X is a priveledged application.
From the documentation for android:priority attribute:
In certain circumstances the requested priority is ignored and the
value is capped to 0. This occurs when: A non-privileged application
requests any priority >0
So if X is a priveledged app and it requests a high priority, it will be granted, and your app, being non-priveledged, will always get the broadcast later.
Experiment
I created 2 sample apps - app1 and app2, each of which registered its own BroadcastReceiver for receiving an android.intent.action.BOOT_COMPLETED. So, BroadcastReceiver for app1 had a priority of 10 and the other - 20.
app1
...
<receiver
android:name="com.example.app1.BootCompletedIntentReceiver">
<intent-filter android:priority="10">
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
...
app2
...
<receiver
android:name="com.example.app2.BootCompletedIntentReceiver">
<intent-filter android:priority="20">
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
...
Result
2021-03-30 18:14:07.337 3453-3453/com.example.app1 E/Anatolii: BootCompletedIntentReceiver called
2021-03-30 18:14:07.509
3498-3498/com.example.app2 E/Anatolii: BootCompletedIntentReceiver
called
As it can be seen from the output, app1 received the intent earlier, event though it had a higher priority.
Answer
Your android:priority doesn't matter because android.intent.action.BOOT_COMPLETED is probably not sent in the synchronous way, and so priority is ignored as stated in the documentation:
android:priority
...
It controls the order in which broadcast receivers are executed to
receive broadcast messages. Those with higher priority values are
called before those with lower values. (The order applies only to
synchronous messages; it's ignored for asynchronous messages.)...
I want to listen to APN Changes in my Android App.
Therefore I start a Service on android.intent.action.BOOT_COMPLETED.
This Service starts a ContentObserver which listens to changes to
content://telephony/carriers/preferapn.
I tested this setup on a few different devices (e.g. LG Spirit with Android 5.0, Samsung A3 with 6.0, Emulator Nexus5 with 7.0 and Huawei P9 Lite with 7.0)
On the Huawei phone onCreate of my Service is not called.
My other approach with android.intent.action.ANY_DATA_STATE in combination with a BroadcastReceiver which is registerd in the Manifest doesn't work either on this phone.
relevant parts of my manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
...
<receiver android:name=".ConnectivityChangeReceiver">
<intent-filter>
<action android:name="android.intent.action.ANY_DATA_STATE" />
</intent-filter>
</receiver>
<receiver android:name=".APNChangedServiceStarter" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
<service android:name=".APNChangedService"></service>
Just for an update, Huawei changed the menu path in the Android 9 version.
On Huawei Mate 10 pro the way to get to this menu:
Settings -> Battery -> App launch -> Disable automatic management for your app.
A pop-up will appear, asking what you want to allow (all true by default). make sure that the first, Auto-launch is enabled
Huawei Phones have a built-in startup manager, it could be that the app is not yet enabled.
Go to Settings > All, and choose Startup manager.
This program is used to manage startup apps on Android phone.
Allow or disallow app to run automatically after the Huawei phone starts up.
i have been trying to overcome issue of Boot_complete receiver not working in certain devices.
Like Vivo device which have iManager app with auto-start manager.
Where user can toggle app from auto start on device boot.
What should i use along with below as intent-filter to restart my service after device reboot.
Earlier thought of using Battery_Change receiver but it won't work from manifest, as i has to be runtime receiver register.
Any suggestion would be really help-full.
Below is what i have used as intent-filter for my app. In most devices its working as expected. But not in all.
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.REBOOT" />
</intent-filter>
There is one thing my team and I discovered when facing a similar issue.
You can monitor the usb state like so:
<receiver
android:name=".MyReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_STATE" />
</intent-filter>
</receiver>
And if memory serves me right, this will send a broadcast before the regular BOOT_COMPLETED action telling you there is or isn't anything USB connected.
Some manufacturers use their own version of BOOT_COMPLETED as you can read here but the USB_STATE is an alternative, based on the things you want to do. Do note you can get multiple broadcasts using this method!
Alternatively you could look into using an AccessibilityService or the JobService from the firebase sdk. More details here.
Could you post a simple code to handle shutdown event.
I need manifest and receiver.
I need the following:
I have a service running in background and I want to detect phone shutting down in order to make it in silent mode so that the terrible operator music doesn't play.
After restarting the phone (I already have on boot receiver) the service will enable phone sound.
You need to listen for the following intent action in your receiver:
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
To make it compatible with some HTC (or other) devices which offer quick power-off feature, you need to include the following action too with in the same intent-filter:
<action android:name="android.intent.action.QUICKBOOT_POWEROFF" />
For more info, read this
<receiver android:name=".ShutdownReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
<action android:name="android.intent.action.QUICKBOOT_POWEROFF" />
</intent-filter>
</receiver>
public static final String ACTION_SHUTDOWN
Broadcast Action: Device is shutting down. This is broadcast when the device is being shut down (completely turned off, not sleeping). Once the broadcast is complete, the final shutdown will proceed and all unsaved data lost. Apps will not normally need to handle this, since the foreground activity will be paused as well.
This is a protected intent that can only be sent by the system.
Constant Value: "android.intent.action.ACTION_SHUTDOWN"
And this ACTION_SHUTDOWN may differ from device to device
Like HTC its android.intent.action.QUICKBOOT_POWEROFF
I am using proximity alerts in one of my applications, however it seems that whenever I reset my phone (via battery pull) or just in general, the proximity alerts are no longer active.
The only way they work once again is if I uncheck and recheck the checkbox (which removes, then re-adds the proximity alert)
Any idea's or reason?
You must reset the alerts. The Android OS does not persist your alerts when rebooting, that's up to your app. Create a BroadcastReceiver (I called mine BootReceiver in my example below) to handle the "android.intent.action.BOOT_COMPLETED" action (this is defined in the manifest). With the BroadcastReceiver you can then restart all of your alerts. Don't forget to add the "android.permission.RECEIVE_BOOT_COMPLETED" permission.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>