0

I am new to Android programming , your help is very much needed .

i tried to disable GPS by the code Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, false);

also had set the permissions in AndroidManifest.xml file as

. . .

<uses-permission android:name="android.permission.WRITE_SETTINGS"/> 
 <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/> 
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 <uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

but in when i run the application it says Not granting permission android.permission.WRITE_SECURE_SETTINGS to package .....

and my application crashes saying

ava.lang.SecurityException: Permission denial: writing to secure settings requires android.permission.WRITE_SECURE_SETTINGS

thx for help in advance :)

skaffman
  • 398,947
  • 96
  • 818
  • 769
Kuch
  • 15
  • 4

3 Answers3

0

You can start the GPS by using

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

To stop the gps provider use

 locationManager.removeUpdates(locationListener);
Chirag Patel
  • 11,416
  • 3
  • 26
  • 38
  • Thanks chirag for the information ,I can just use locationManager.removeUpdates(locationListener); whenever i was thinking of disabling GPS. – Kuch Mar 21 '12 at 02:09
  • This does not start or stop the GPS. It just enables location updates (provided the GPS or some other source of location information is available) resp. disables the location updates! – Stefan Mar 21 '12 at 12:48
0

If you're trying to actually disable the GPS chip then my understanding of the android system is that this isn't possible. You have to ask the user to enable or disable the GPS via the Setting menu.

See this question: How can I get the dreaded WRITE_SECURE_SETTINGS permission for my android app?

In the past it was possible and system level apps can use that permission, a normal app cannot take advantage of it.

Community
  • 1
  • 1
DavidBriggs
  • 400
  • 3
  • 15
  • Thanks David, your right , the application will not be able to get this permission , the only way to disable is from the user through setting menu , or build your own custom firmware – Kuch Mar 20 '12 at 21:47
0

I guess your users are complaining that your app can see their location.

For not showing that message when they install the app, you must remove:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

You may also try to use the getLastKnownLocation in order to save battery. It doesn't turn the GPS on and may use the network cell data (COARSE_LOCATION).

public Location getLocation() {
 if (contextReference == null) {
   return null;
 } 

 Context context = contextReference.get();
 if (context == null) {
   return null;
 }

 Location location = null;

 if (context
    .checkCallingOrSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
  LocationManager lm = (LocationManager) context
      .getSystemService(Context.LOCATION_SERVICE);
  location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
} else if (context
    .checkCallingOrSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
  LocationManager lm = (LocationManager) context
      .getSystemService(Context.LOCATION_SERVICE);
  location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
return location;

}

Rafael Sanches
  • 1,823
  • 21
  • 28
  • Thanks Rafael , i want the location periodically (every 30mins) in the app, wanted to return the GPS settting back to its original setting after getting the location to save the power. – Kuch Mar 20 '12 at 21:35