This question already has answers here:
android turning on wifi programmatically
(2 answers)
Closed 6 years ago.
Thanks for helping with my Question!
Q : I'm working on an App, which has a Timed mechanism, and needs to change a Setting via Code (Ex: Turn Off WiFi, etc).. Is this possible to accomplish, obtaining Permissions for it via Manifest & Runtime Permission Requests, without needing Root?
I'm hoping I can simply do it via a Permission Request! Otherwise months of time lost :(
Thanks ahead of time for answering! Just need to know for sure.
well <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"> is not considered dangrous so you don't need to ask it on run time in marshmallow
All you need to do is declared it in the manifest like
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
and then in your activity you can enable and disable the wifi like this
WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
// TO CHECK WEATHER ITS ENABLED OR DISABLED
boolean wifiEnabled = wifiManager.isWifiEnabled()
// TO ENABLE
if(!wifiEnabled) wifiManager.setWifiEnabled(true);
// TO DISABLE
if(wifiEnabled) wifiManager.setWifiEnabled(false);
Hope it helps
Related
I'm building a Cordova app that needs to access WiFi on Android and disable the currently connected network in order to prevent automatic disconnection from the network i'm having it connect to (since there is no internet).
The problem is that when testing on Android Oreo 8.0+ I am getting this error in adb logcat and I can't figure out what I need to do to fix this:
E/WifiConfigManager: UID 10315 does not have permission to update configuration "Test SSID"WPA_PSK
E/WifiStateMachine: Failed to disable network
These are the perms listed in manifest:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.OVERRIDE_WIFI_CONFIG" />
<uses-permission android:name="android.permission.INTERNET" />
The OVERRIDE_WIFI_CONFIG perm I found on this post, but that doesn't seem to help in my specific situation: Changing Android hotspot settings
I found this specific error located in this file:
https://android.googlesource.com/platform/frameworks/opt/net/wifi/+/master/service/java/com/android/server/wifi/WifiConfigManager.java#984
Which calls canModifyNetwork which I found here:
https://android.googlesource.com/platform/frameworks/opt/net/wifi/+/master/service/java/com/android/server/wifi/WifiConfigManager.java#651
Can anybody more experienced with Android help me to resolve this issue, and what needs to be done in order to allow my app to disable networks?
Does this mean that apps are not allowed to disable a network if it wasn't created by the app?? Please help I don't know where to go from here!
I did find this post as well, which references 6.0, but is this true that we're basically completely locked out of disabling networks we didn't create? Android 6.0 Cannot add WifiConfiguration if there is already another WifiConfiguration for that SSID
I am not able to answer your question per se, but I can answer this
Does this mean that apps are not allowed to disable a network if it
wasn't created by the app?
That's correct, as according the documentation of the method disableNetwork:
Disable a configured network. The specified network will not be a
candidate for associating. This may result in the asynchronous
delivery of state change events. Applications are not allowed to
disable networks created by other applications.
So if the user has already connected to this network using the Android system, in Oreo you won't be able to disable the network.
Though the method disableNetwork returns true or false in the case of success or failure
So I've created the majority of my application but I am having an issue with power saving applications interfering with it. I use the AlarmManager to run a piece of code that send information to a server every x minutes (minimum 1h), the main issue I am having is that power managers are disabling with WiFi because the device is sleeping.
What's the most effective way to ensure WiFi is available at wakeup? Is it to simply enable WiFi and reconnect it?
Maybe an other way to your solution is to listen for the connection_changed intents. That way you know there is a connection to the internet and you can upload.
You can also enable WiFi but you will need permissions for that: (I guess these)
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(enabled);
Haven't tried it but this should do the trick.
can we switch on/off the wi-fi of the device in a test cases in robotium?
because i am testing an issue which needs wifi to be on in initial phase then turning off the wi-fi and continue with testing.
Yes you can do it, see the example:
public void testNoNetworkConnection() throws Exception {
setWifiEnabled(false);
// do stuff solo.something
setWifiEnabled(true);
}
private void setWifiEnabled(boolean state) {
WifiManager wifiManager = (WifiManager)solo.getCurrentActivity().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(state);
}
Remember to add permission in your Manifest file:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
EDIT: With the new Robotium 5.3.1 you can use setWiFiData(Boolean turnedOn) to turn wifi on or off (See documentation)
Enjoy
You can use ExtSolo provided by Testdroid. Library can be found on https://github.com/bitbar/robotium-extensions and api for needed method: turn wifi. If you don't want to use extra library you can use code as below:
protected void turnWifi(boolean enabled) {
try {
WifiManager wifiManager = (WifiManager) getInstrumentation()
.getTargetContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(enabled);
} catch (Exception ignored) {
// don't interrupt test execution, if there
// is no permission for that action
}
}
You need permission in your application to change state of Wifi
Since, you will test the application where you know the behavior(when wifi is on and when wifi is off) of application , you can mock the data accordingly. Mocking the data will make the application to behave as it is expected in that particular situation.
There is not an easy way to do this, that is different to me saying that it is not possible though but might be enough to disuade you from doing this.
If your application has the correct permissions then you can from your robotium script in fact turn on or off the wifi just as any application can. But remember the permission needs to be in your applications manifest not your test apk manifest.
If your application doesn't have the correct permission (and you are unwilling to change it) there is another way but is quite a lot of work (working on getting the solution I use open sourced). Make an application that does have the permission as a service, bind to that service in your tests and ask that service to turn off the wifi.
On android 2.3 the permission <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /> is not allowed anymore for non-system applications. Is there an alternative to enable/disable data connection, besides that one where you modify APN name? I found Data Enabler Widget on Android Market that does that, but I can't seem to understand how. Can anyone help me?
Thanks! - Alex Ady
I don't know how to change data connection (3g, 2g, etc), but you can enable/disable wifi connection through this:
WifiManager wifiManager = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(boolean enabled);
I found a solution to my problem, so I'm closing this question. The alternative is to simply display the mobile settings activity if a level 10 API or higher is detected, or continue with direct enable from code otherwise. I keep the android.permission.MODIFY_PHONE_STATE, but only use it the API is under level 10.
I am new to android i stuck for using coding enable location and sectury in that use wireless network enabled so please tell mi how it's possible
thanks
priya naral
From what I gathered, you want to programmatically turn on Wi-Fi.
If so, first you need to add these to your AndroidManifest.xml:
<application>
.
.
.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
</application>
Then you have the ability to use WifiManager, so your code would look something like the following, where this is your application Context:
WifiManager manager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
if(!manager.isWifiEnabled()){
manager.setWifiEnabled(true);
}