I have to monitor the connectivity of the device automatically, to do that I have my receiver registered with
<receiver android:name=".NetworkStateReceiver" android:exported="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
<action android:name="android.net.wifi.STATE_CHANGE"/>
<action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
</intent-filter>
</receiver>
It works fine, but when the WiFi goes to sleep mode, the network info will have the state DISCONNECTED/BLOCKED. This is ok, I understand why it does this to save battery etc.
The issue is that the receiver doesn't receive an intent when the WiFi goes to sleep mode. Same thing if I wake up the device, the WiFi will switch to connected state but I won't receive an intent.
(Tested on a Nexus 5X)
Any way I could receive those events without having to use an alarm that would fire every X minutes to check the state?
Checked Connectivity Change Broadcast receiver not triggering when phone is in sleep mode but answer seems irrelevant
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 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 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 ?
We can turn on/off WiFi/Bluetooth programmatically.
But is it possible that as long as the application is running that has
turned off WiFi/Bluetooth they would remain off.
I mean to say that if User turn WiFi on then application will again turn that to off?
You can add an intent filter and register a receiver when your application is started
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
<action android:name="android.net.wifi.STATE_CHANGE"/>
</intent-filter>
register receiver on onCreate
IntentFilter filters = new IntentFilter();
filters.addAction("android.net.wifi.WIFI_STATE_CHANGED");
filters.addAction("android.net.wifi.STATE_CHANGE");
super.registerReceiver(wifiStateListener, filters);
although in your case I think only "android.net.wifi.WIFI_STATE_CHANGED" will suffice. Whenever the state of WIFI (enable, disable, connect, disconnect) is changed it is fired by os.
You need to add the permission to read/modify WIFI state.
Hope this helps.
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