5

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Noman
  • 4,049
  • 10
  • 38
  • 59

4 Answers4

10

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.

Kartik Domadiya
  • 29,868
  • 19
  • 93
  • 104
2

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)
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81
  • Appears to not be working for me now, but thanks for the advice. – eWizardII Dec 30 '12 at 23:52
  • 1
    **This** solution and the one above by **Rasel** will only work up to Android 2.3.6 - not v3.x.x or v4.x.x - although there are some suggestions that this _vulnerability_ has been re-introduced in Android 4.1.2 and it may work yet again! – ChuongPham May 15 '13 at 15:32
  • @Chuong thanks for informing and putting a comment. its helpful – Pir Fahim Shah May 20 '13 at 11:39
2

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);
Rasel
  • 15,499
  • 6
  • 40
  • 50
2

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.

Joachim Rohde
  • 5,915
  • 2
  • 29
  • 46
  • so u mean i should not use it?? – Noman Aug 03 '11 at 11:49
  • @Noman: Depends what you are trying to do. If it's for personal use, go ahead. If you want to distribute your application I wouldn't use it because it's not guaranteed to work. – Joachim Rohde Aug 03 '11 at 11:58