1

i am using GPS in My application and i want to turn GPS on and off Programmatically to save power how can i do it :(

this is for turn off i need the turn on please

private void turnGPSOnOff(){
  String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
  if(!provider.contains("gps")){
    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);
    //Toast.makeText(this, "Your GPS is Enabled",Toast.LENGTH_SHORT).show();
  }
}
Poison Sniper
  • 148
  • 2
  • 15

3 Answers3

1
    try
                {
                dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
              } 
              catch (SecurityException e1) 
              {
                // TODO Auto-generated catch block
                e1.printStackTrace();
              } 
              catch (NoSuchMethodException e1) 
              {
                // TODO Auto-generated catch block
                e1.printStackTrace();
              }

                dataMtd.setAccessible(true); 
               try {

                dataMtd.invoke(conm,true);


                //gprDisable();
              } 
               catch (IllegalArgumentException e) 
               {
                // TODO Auto-generated catch block
                e.printStackTrace();
              } 
               catch (IllegalAccessException e) 
               {
                // TODO Auto-generated catch block
                e.printStackTrace();
              } 
               catch (InvocationTargetException e) 
               {
                // TODO Auto-generated catch block
                e.printStackTrace();
              }

and if you are making the options to false, you can disable the GPS. Hope this may help you.

thampi joseph
  • 700
  • 1
  • 8
  • 20
1
private void turnGPSOnOn(){
  String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
  if(provider.contains("gps")){ // for turn on
    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);
    Toast.makeText(this, "Your GPS is Enabled",Toast.LENGTH_SHORT).show();
  }
}
Pratik Popat
  • 2,891
  • 20
  • 31
-2

You can turn on the gps using

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new CTLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1.0f, locationListener);

and turn it off using

locationManager.removeUpdates(locationListener);

Or you might also find this another thread on gps of use: How can I enable or disable the GPS programmatically on Android?

Community
  • 1
  • 1
codemaster
  • 572
  • 1
  • 6
  • 16
  • This does not turn off the Gps, It requests that YOUR app recieve gps updates, and it then requests that YOUR app recieve updates no more. – FutureSci Oct 26 '13 at 11:53