is there any way to turn on gps service through code?
Asked
Active
Viewed 1.5k times
2 Answers
17
There's a good example on HelloAndroid
Check for the GPS
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,1.0f, this);
boolean isGPS = locationManager.isProviderEnabled (LocationManager.GPS_PROVIDER);
If isGPS is false open the settings
startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
Don't forget to check the GPS is enabled when the user returns, they might not enable it.

Rob
- 7,039
- 4
- 44
- 75
-
1@Shalini Singh, sorry no, as the other answer from @darioo says, this is not possible in the Android system what so ever, no application is able to start and stop core services like Wifi, GPS and Bluetooth without user confirmation. This is mostly for security reasons. You will have to explain these limiting factors to the client. – Rob Dec 21 '10 at 11:45
7
No.
Turning on GPS is a user's choice. It would be unethical to force it.

darioo
- 46,442
- 10
- 75
- 103
-
2+1 You can provide an alert to the user saying something like "To proceed you GPS needs to be turned on" with a button to open the system preferences. – Rob Dec 21 '10 at 10:10
-
Used to be possible in Android 1.1, but they quickly fixed that in 1.5. The user turns off GPS for a reason. – EboMike Dec 21 '10 at 10:10
-
@Rob: that would be the standard way to do it. The user needs to have choice when dealing with sensitive things like turning on GPS. – darioo Dec 21 '10 at 10:11
-
Yaa i m 100% agree with all of you. but this is the client's requirement if gps service is not start then start it through application – Andy Dec 21 '10 at 10:56
-
@Shalini: then tell your client that this cannot be done directly. It's impossible and your client would have to bug Google for this to be enabled, and you can be pretty sure they won't do that. @Rob's method is probably the best way to do it. – darioo Dec 21 '10 at 10:59