3

I want to switch on GPS and WiFi by code forcefully without going to setting, so I any suggestions as to how to achieve this in ICS .

String provider = Settings.Secure.getString(getContentResolver(),
                Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        if (!provider.contains("gps")) { // if gps is disabled
            final Intent poke = new Intent();
            poke.setClassName("com.android.settings",
                    "com.android.settings.widget.SettingsAppWidgetProvider");
            poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
            poke.setData(Uri.parse("3"));
            sendBroadcast(poke);
        }

This code is working fine in 2.1 but in ICS it isn't.

Bleeding Fingers
  • 6,993
  • 7
  • 46
  • 74

3 Answers3

2

Please use this code for manual enable through setting because due to security reason in ics this features has been removed.

Intent callGPSSettingIntent = new Intent(
        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(callGPSSettingIntent);
Pramod
  • 172
  • 3
  • 10
1

To enable GPS, there is no API provided by Android because of Security.So you will not be able to turn ON GPS without user intervention.

TO enable Wi-Fi,

WifiManager wifiManager = new WifiManager()

if(wifiManager.isWifiEnabled())
     wifiManager.setWifiEnabled(false);

Choose whatever you want, either to set it ON or OFF.

Custadian
  • 845
  • 1
  • 11
  • 37
0

The code which you wrote in the question was working fine previously, But since it was security loophole, Android developer has fixed it now a days.

So even if you use the above code, it gives GPS blinking animation, but in real it is not working at all. You can see the GPS Blinking even after you remove you application from the device.

Vigbyor
  • 2,568
  • 4
  • 21
  • 35