问题
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?
回答1:
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.
回答2:
if(!LocationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER ))
{
Intent myIntent = new Intent( Settings.ACTION_SECURITY_SETTINGS );
startActivity(myIntent);
}
回答3:
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();
}
}
回答4:
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 :/
回答5:
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();
}
回答6:
You should use the Location Settings Dialog in Play Services that prompts the user to enable location services (if necessary) with just one click.
回答7:
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.
来源:https://stackoverflow.com/questions/1051649/how-to-programmatically-enable-gps-in-android-cupcake