0

I want to turn off sensors like GPS programaticaly. how to do that?

2 Answers2

2

Enable GPS

Intent intent=new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);

Disable GPS

Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
sendBroadcast(intent);
IMRAN
  • 171
  • 6
  • [This answer](http://stackoverflow.com/questions/22528984/android-device-gps-on-off-programatically#22529296) suggests that this doesn't work in modern Android versions. I guess nowadays the user's action is needed to change the GPS state. – Markus Kauppinen May 06 '16 at 08:16
  • 1
    Thank you dear..Is it possible to turn off other sensors also? – lakshitha madusanka May 06 '16 at 09:31
  • yes it is possible to turn off like wifi,bluetooth,phone screen,gps – IMRAN May 06 '16 at 10:17
  • @lakshithamadusanka for wifi 'WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); wifiManager.setWifiEnabled(true); wifiManager.setWifiEnabled(false);' you have to add permission in manifast ** ** – IMRAN May 06 '16 at 10:19
  • @lakshithamadusanka for bluetooth ******************** public static boolean setBluetooth(boolean enable) { BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); boolean isEnabled = bluetoothAdapter.isEnabled(); if (enable && !isEnabled) { return bluetoothAdapter.enable(); } else if(!enable && isEnabled) { return bluetoothAdapter.disable(); } return true; }***********************permissions are – IMRAN May 06 '16 at 10:24
0

Try using below code

public void turnGPSOn()
{
     Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
     intent.putExtra("enabled", true);
     this.ctx.sendBroadcast(intent);

    String provider = Settings.Secure.getString(ctx.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")); 
        this.ctx.sendBroadcast(poke);


    }
}
// automatic turn off the gps
public void turnGPSOff()
{
    String provider = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if(provider.contains("gps")){ //if gps is enabled
        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")); 
        this.ctx.sendBroadcast(poke);
    }
}

You can turn the gps off by LocationSensor.Enabled = False

You may want to read the LocationSensor tutorial here: http://appinventor.mit.edu/explore/ai2/location-sensor.html

Krishna
  • 706
  • 5
  • 22