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
Related
I am writing an Android app and need to performe some code before phone switches off. Previously, this was possible by defining a BroadcastReceiver and registering it in AndroidManifest.xml with:
<receiver android:name="MyBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
In this case, before phone is switched off, an intent with action ACTION_SHUTDOWN is sent to MyBroadcastReceiver and the code in the methode onReceive is executed.
But this is not working anymore in API 28 because ACTION_SHUTDOWN cannot be registered in the manifest anymore.
I tried to register a BroadcastReceiver with ACTION_BOOT_COMPLETED (because it is still allowed) that triggers on boot a service whose task is to monitor and detect when the phone is shut down, but it didn't work.
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.
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
I am making an android app, in which the app must launch just before the phone is shutting down. Is that possible?
Here i am not talking about encountering phone shutdown event when my app is running.
You will need this permission in your manifest file
<uses-permission android:name="android.permission.DEVICE_POWER" />
And you will listen to the intent like this
<receiver android:name=".ShutdownReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
</intent-filter>
</receiver>
You can look at the links here for some description and here for official documentation.
I don't know the exact application where you want to use it, but you can have a background service running with the receiver for this intent.
I have both CONNECTIVITY_CHANGE & STATE_CHANGE set in my manifest
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.STATE_CHANGE" />
but when i turn wifi on/off manually it take sometimes up to 1 minute before my receiver receives the information, even though I can see that the wifi is back (other app can resume their actions)
Changing the intent-filter priority doesn't change anything, event when setting MAX_INTEGER as a priority.
Any idea how to get this information faster ?