0

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.

StudioB
  • 117
  • 9
  • yes you can do it but only if if have permissions. – Sohail Zahid Jun 01 '16 at 12:36
  • Generally the permission necessary to change settings is not available to 3rd party apps, and being "root" doesn't really even directly help, though it may make it possible to install an app as if it were a part of the build from the factory. However the permission required for a given goal depends on the goal, and there are some things you can obtain permission to do. **It turns out that enabling/disabling wifi is something which *is* allowed.** Only a specific form of your question fits with the intent of stackoverflow, so your question reduces to the only specific you gave, wifi. – Chris Stratton Jun 01 '16 at 12:40
  • I suppose I should have been more direct with my description of intention.. I was only using WiFi as an example, however specifically I need to switch the Location setting to OFF.. **Would that be easily doable as well?** I can imagine how it may be construed as dangerous to access someones Location setting, so I'm hoping I can still do it with permissions (without root)? – StudioB Jun 04 '16 at 10:44

1 Answers1

0

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

Atiq
  • 14,435
  • 6
  • 54
  • 69
  • Hey thanks for the great Answer man.. To be 100% specific, i would actually need to change the Location/GPS setting to OFF.. Do you know if that makes any difference? **In other words**, would substituting locationmanager for wifimanager work in the same way as in the code you provided? and would changing the location setting to "off" be allowed simply just the same? – StudioB Jun 04 '16 at 10:39
  • then in the question you should have asked for location setting instead of wifi, and no you cannot change location setting without permission. – Atiq Jun 04 '16 at 10:43
  • My apologies - I only used Wi-Fi as an example, because its one of the main settings, similar to Location. And yes, I understand I must use a permission, I just wanted to be reassured that I would in fact be able to accomplish the task (turning the Location setting OFF) easily once permissions are granted, and not need root or anything too crazy to access such settings. – StudioB Jun 04 '16 at 18:32