我正在尝试启用/禁用GPS。我试过这段代码 - :
.edmx
它提供安全权限错误。我也尝试打开设置 - :
//Enable GPS
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
context.sendBroadcast(intent);
//Disable GPS
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
context.sendBroadcast(intent);
它不适用于每台设备。是他们的任何解决方案来处理所有版本的android。
答案 0 :(得分:3)
这是deprecated
。您无法以编程方式打开/关闭位置。我建议您使用最新版本的GooglePlayService
。使用PlayService
,您可以随时查看是否启用了location
。如果未启用,则AlertDialog
启用它。即使不离开您的应用程序,您也可以启用GPS。如果未启用,则可以执行要执行的操作。您可以在此post
基本上,您的Activity
应该实施ResultCallback<LocationSettingsResult>
如下所示注册PendingIntent
,
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(
mGoogleApiClient,
mLocationSettingsRequest
);
result.setResultCallback(this);
现在你有一个onResult
回调,你可以执行你想做的事情
@Override
public void onResult(LocationSettingsResult locationSettingsResult) {
final Status status = locationSettingsResult.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
Log.i(TAG, "All location settings are satisfied.");
startLocationUpdates();
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to" +
"upgrade location settings ");
try {
// Show the dialog by calling startResolutionForResult(), and check the result
// in onActivityResult().
status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
Log.i(TAG, "PendingIntent unable to execute request.");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog " +
"not created.");
break;
}
}
// This code is from https://github.com/googlesamples/android-play-location/blob/master/LocationSettings/app/src/main/java/com/google/android/gms/location/sample/locationsettings/MainActivity.java
请在Github
中找到示例Google项目