I have a mobile application that allows users to enable/disable WiFi on click of a button.
However I noticed today that my app is no longer able to change the WiFi status. It was working since before few weeks. I tried to debug it but the following method always returns false.
boolean result = wifiManager.setWifiEnabled(true);
I am testing it on Samsung Galaxy Note 9 and Android 10.
This API is no longer supported when targeting Android 10 or higher.
Starting with Build.VERSION_CODES#Q, applications are not allowed to enable/disable Wi-Fi. Compatibility Note: For applications targeting Build.VERSION_CODES.Q or above, this API will always fail and return false. If apps are targeting an older SDK (Build.VERSION_CODES.P or below), they can continue to use this API.
Instead, you should use the Settings.Panel API to present a system UI allowing users to enable or disable Wi-Fi.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startActivity(Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY))
}
Related
https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#disable()
public boolean disable ()
This method was deprecated in API level 33.
Starting with Build.VERSION_CODES.TIRAMISU, applications are not allowed to enable/disable Bluetooth. Compatibility Note: For applications targeting Build.VERSION_CODES.TIRAMISU or above, this API will always fail and return false. If apps are targeting an older SDK (Build.VERSION_CODES.S or below), they can continue to use this API.
How to implement properly disabling bluetooth?
I have no problem with enable bluetooth, I do like this (following https://developer.android.com/guide/topics/connectivity/bluetooth/setup):
if (bluetoothAdapter?.isEnabled == false) {
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
}
I was searching for solution, but in Android documentation they did not add proper implementation for disabling BT in Android API 33.
There is a simple reason why a normal application cannot simply turn off the Bluetooth sub-system in Android.
Nowadays, the Bluetooth sub-system is used by a large number of applications running simultaneously. One example is the Exposure Notifications Framework introduced for COVID 19 which is even part of the operating system. So if individual applications could simply switch off this sub-system, a large number of other applications would be negatively affected.
If you still feel that it is imperative for your application that Bluetooth be turned off, then ask the user to do so via the System menu.
I require the 'Draw over other apps' functionality to launch my activity from the background at a specific period. This works well on some devices, however I am testing it on a Samsung A01 device with Android 10 and the feature isn't available.
Is there a way I can check if the feature is available?
Actually I came to learn that the feature doesn't exists on Android Go devices.
Using the method isLowRamDevice() from ActivityManager, I was able to tell whether or not the device supports the feature.
How it works
ActivityManager activityManager = (ActivityManager) getContext().getSystemService(ACTIVITY_SERVICE);
boolean drawOverOtherAppsFeatureAvailable = activityManager.isLowRamDevice();
On the android 11 device, I want to check for the "data limit" option is enabled or disabled on the device those are running Android 11. Currently, I am using NetowrkPolicy, NetworkPolicyManager, NetworkTemplate classes. But those all are hidden APIs and now blocked or restricted in targeting device android 11. And code is below -
if (networkPolicy.limitBytes == NetworkPolicy.LIMIT_DISABLED) {
//currentlly data limit is not enabled on device
} else {
//currentlly data limit is enabled on device, so that we can calculate how much is remaining.
}
I have already searched in for ConnectivityManager [Not provide much information related to my task]. I am guessing this may be currently in any setting package or not sure.
Note - I just want to check if the option is enabled or not on device those are targeting android 11.
Update - 1
I am not sure how this can be useful - KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG (Controls the cellular data limit), this says - If the user uses more than this amount of data in their billing cycle, as defined by KEY_MONTHLY_DATA_CYCLE_DAY_INT, cellular data will be turned off by the user's phone. If the value is set to DATA_CYCLE_THRESHOLD_DISABLED, the data limit will be disabled.
On Android Oreo or lower, WifiManager#getConfiguredNetworks() method was available to list saved network.
I tested this out in Android 10 emulator and it returns a blank list (as mentioned in the docs). Does anyone know whats the alternate?
Official docs
This method was deprecated in API level 29. a) See
WifiNetworkSpecifier.Builder#build() for new mechanism to trigger
connection to a Wi-Fi network. b) See
addNetworkSuggestions(java.util.List),
removeNetworkSuggestions(java.util.List) for new API to add Wi-Fi
networks for consideration when auto-connecting to wifi. Compatibility
Note: For applications targeting Build.VERSION_CODES.Q or above, this
API will return an empty list, except for:
I have implemented the system to connect to wifi networks from my app programmatically, now I want to forget configured WIFI networks programmatically from the application.
I have implemented this into my application already and its been working fine on the Android 5.0 and lower devices (Less then API 22).
For Android 6.0 and the higher device it is not working (Higher and equal then API 23).
Please refer the following code:
val wifiManager = this#SelectWifiSettingsActivity.baseContext!!.getSystemService(android.content.Context.WIFI_SERVICE) as WifiManager
val list = wifiManager.configuredNetworks
for (i in list) {
wifiManager.disableNetwork(i.networkId)
wifiManager.saveConfiguration()
}
I have also referred the following link:
https://stackoverflow.com/a/33075445/9360112
As there are some changes in WIFI configurations in Android 6.0.
Please, help me if anyone has solution for this on Android 6.0 onwards.
First thing is you don't need to use saveConfiguration().
This method was deprecated in API level 26.
There is no need to call this method - addNetwork(WifiConfiguration), updateNetwork(WifiConfiguration) and removeNetwork(int) already persist the configurations automatically.
Secondly, what you're looking for is removeNetwork().
Your code will look like:
val wifiManager = this#SelectWifiSettingsActivity.baseContext!!.getSystemService(android.content.Context.WIFI_SERVICE) as WifiManager
val list = wifiManager.configuredNetworks
for (i in list) {
wifiManager.removeNetwork(i.networkId)
}
Being said that... There are some changes in the Android M APIs for WifiManager.
Your apps can now change the state of WifiConfiguration objects only
if you created these objects. You are not permitted to modify or
delete WifiConfiguration objects created by the user or by other apps.
See Network Changes in Android M