Reputation: 4866
I'm currently writing an app in Android that works with the GPS. At the moment I'm able to work out whether the GPS is enabled. My problem is that I want to enable the GPS on app startup if it is disabled. How can I do this programmaticaly?
Upvotes: 28
Views: 55539
Reputation: 3879
First check whether the location service is already on or not ??
Checking location service is enabled or not
public boolean isLocationServiceEnabled(){
LocationManager locationManager = null;
boolean gps_enabled= false,network_enabled = false;
if(locationManager ==null)
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
try{
gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}catch(Exception ex){
//do nothing...
}
try{
network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}catch(Exception ex){
//do nothing...
}
return gps_enabled || network_enabled;
}
Then finally to open if location service in turned off previously
if (isLocationServiceEnabled())) {
//DO what you need...
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Seems Like location service is off, Enable this to show map")
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
}).setNegativeButton("NO THANKS", null).create().show();
}
Upvotes: 1
Reputation: 15379
You should use the Location Settings Dialog in Play Services that prompts the user to enable location services (if necessary) with just one click.
Upvotes: 0
Reputation: 3
if your question is at the user level of android these properties are located in: "Settings -> Location -> Use wireless networks" -> "Settings -> Location -> Use GPS satellites"
.
But at the developer can use the class "android.provider.Settings.Secure"
with the appropriate permissions.
Upvotes: -5
Reputation: 8186
This method code can be help for you
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();
}
}
Upvotes: 6
Reputation: 5242
You might use the following:
try {
Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, true);
} catch (Exception e) {
logger.log(Log.ERROR, e, e.getMessage());
}
but it will only work if you have system signature protection level. So you need to cook your own Image to actually use it :/
Upvotes: 3
Reputation: 1234
if(!LocationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER ))
{
Intent myIntent = new Intent( Settings.ACTION_SECURITY_SETTINGS );
startActivity(myIntent);
}
Upvotes: 15
Reputation: 1006664
You can't, starting with Android 1.5. The most you can do is pop open the activity to allow the user to toggle it on/off. Use the action held in android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS
to craft an Intent to open this activity.
Upvotes: 51