I will get " An app requesting permission to use WLAN. Allow?" prompt window in real mobile phone Samsung SM-J5008 with Android 5.1 when I try to change status of WiFi.
I have google some information ,such as https://groups.google.com/d/msg/tasker/C5ZgPA2J7aM/bH7j85buAAAJ
A dialog box "Ask to use WLAN" will be displayed, and require me to choice whether to display the dialog box when I try to change the status of WiFi.
I hope to set "Ask to use WLAN" dialog box off programmatically , so the dialog will not be displayed when I change WiFi status, how can I do? Thanks!
Image
Added Content
I set the status of WiFi using the following code.
fun setWiFi(aWiFiDef: WiFiDef){ val wifiManager =mContext.applicationContext.getSystemService(WIFI_SERVICE) as WifiManager wifiManager.isWifiEnabled=aWiFiDef.status }
2 Answers
Answers 1
I think you can do it by adding CHANGE_WIFI_STATE
permission to your manifest file.
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
CHANGE_WIFI_STATE is a normal permission so maybe that "Ask to use WLAN" popup can be skipped. I am saying maybe because it depends upon OEM's customization, how they are handling this "Ask to use WLAN" popup.
Hope this helps.
Answers 2
Use the below code to switch WiFi ON or OFF. Make sure you have added the <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
PERMISSION and also note that you might need to replace "this" by your activity's current context.
WifiManager wifiManager = (WifiManager) this.getApplicationContext().getSystemService(Context.WIFI_SERVICE); if(wifiManager.isWifiEnabled()){ // Is ON , switch it OFF wifiManager.setWifiEnabled(false); }else{ // Is OFF, switch it ON wifiManager.setWifiEnabled(true); }
Using this code i am able to switch the WiFi adapter ON and OFF with no additional dialog prompt.
Here is the documentation taken from developer.android.com :
Make sure that you only try to change the WiFi status using the above code and that there isn't any piece of code elsewhere in your activity trying to accomplish the same thing. There might be a listener in your code that detects your intent to change the WiFi status (switch it ON or OFF) and displays the dialog you've shown us.Find it and remove it.
If after those changes the dialog is still being displayed, then as @VicJordan said, it might be an OEM implementation issue in which case i am sorry but i cannot help you.
Best of luck!
0 comments:
Post a Comment