Noman
Noman

Reputation: 4109

Switch off GPS programmatically

What i have: Currently my app is giving location through gps.

what i want: Gps to turn off automatically after i exit from the application. Because it keeps on telling me the location time and again that looks odd and also gps consume a lot battery.

Upvotes: 5

Views: 7733

Answers (4)

Pir Fahim Shah
Pir Fahim Shah

Reputation: 10633

If you want to enable the GPS programmaticaly then copy and paste this code in your project, its working fine.

      final Intent i = new Intent();
      i.setClassName("com.android.settings","com.android.settings.widget.SettingsAppWidgetProvider"); 
      i.addCategory(Intent.CATEGORY_ALTERNATIVE);
      i.setData(Uri.parse("3")); 
       sendBroadcast(i); //if you are in broad cast receiver activity then use context.sendBroadcast(i)

Upvotes: 2

Kartik Domadiya
Kartik Domadiya

Reputation: 29968

Looking at above comment thread it seems its possible to turn OFF GPS programatically (But seeing only 12 Upvotes)

But if you switch OFF GPS from your application programatically, what if other applications use GPS Service ?

The solution would be like

Open Settings of android and then tell user to turn OFF GPS when he exits from the application...

For this you can do like :

Intent i = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);

OR

You can try like

locationManager.removeUpdates(myLocationListener); 
locationManager = null;

This shutdown gps for this app, but its still available for use by other apps.

Upvotes: 10

Joachim Rohde
Joachim Rohde

Reputation: 6045

Even though the link Rasel posted contains some code which might work you should keep in mind that the code is exploiting a security flaw which has been fixed already (http://code.google.com/p/android/issues/detail?id=7890) and therefore shouldn't work in the near future anymore.

Upvotes: 2

Rasel
Rasel

Reputation: 15477

This code can alter your gps.But it is not documented

 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);

Upvotes: 2

Related Questions