【发布时间】:2012-02-17 12:27:25
【问题描述】:
如果已关闭,是否可以通过编程方式打开设备上的 LocationProviders(GPS 提供商/网络提供商)?
【问题讨论】:
如果已关闭,是否可以通过编程方式打开设备上的 LocationProviders(GPS 提供商/网络提供商)?
【问题讨论】:
不,不是, 但您可以打开定位服务设置窗口:
context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
【讨论】:
【讨论】:
private void turnGPSOn(){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.contains("gps")){ //if gps is disabled
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);
}
}
但此代码仅支持 APK 8。
【讨论】:
是的,但您必须是超级用户 (sudo),您的设备必须是 root。
【讨论】:
在最近的 Marshmallow 更新中,即使启用了位置设置,您的应用也需要明确请求许可。推荐的方法是显示应用程序的权限部分,用户可以在其中根据需要切换权限。执行此操作的代码 sn-p 如下:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Location Permission");
builder.setMessage("The app needs location permissions. Please grant this permission to continue using the features of the app.");
builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
}
});
builder.setNegativeButton(android.R.string.no, null);
builder.show();
}
} else {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean isGpsProviderEnabled, isNetworkProviderEnabled;
isGpsProviderEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkProviderEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isGpsProviderEnabled && !isNetworkProviderEnabled) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Location Permission");
builder.setMessage("The app needs location permissions. Please grant this permission to continue using the features of the app.");
builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
builder.setNegativeButton(android.R.string.no, null);
builder.show();
}
}
并重写onRequestPermissionsResult方法如下:
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_COARSE_LOCATION: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "coarse location permission granted");
} else {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
}
}
}
另一种方法是您也可以使用SettingsApi 来查询启用了哪些位置提供程序。如果没有启用,您可以提示一个对话框从应用程序内更改设置。
【讨论】:
使用以下代码启动 GPS
locationManager = ( LocationManager ) getSystemService ( Context.LOCATION_SERVICE );
locationListener = new LocationListener();
locationManager.requestLocationUpdates ( LocationManager.GPS_PROVIDER, 0, 0, locationListener );
和下面的代码来停止 GPS
locationManager.removeUpdates( locationListener );
更多详情请参阅documentations for LocationManager 和for LocationListener。
【讨论】: