我正在开发一个应用程序在Android操作系统。我不知道如何检查位置服务是否启用。
我需要一个方法,如果它们被启用,返回“true”,如果没有“false”(所以在最后一种情况下,我可以显示一个对话框来启用它们)。
我正在开发一个应用程序在Android操作系统。我不知道如何检查位置服务是否启用。
我需要一个方法,如果它们被启用,返回“true”,如果没有“false”(所以在最后一种情况下,我可以显示一个对话框来启用它们)。
当前回答
在我看来,这个if子句很容易检查位置服务是否可用:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
//All location services are disabled
}
其他回答
根据上面的答案,在API 23中,你需要添加“危险”权限检查以及检查系统本身:
public static boolean isLocationServicesAvailable(Context context) {
int locationMode = 0;
String locationProviders;
boolean isAvailable = false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
isAvailable = (locationMode != Settings.Secure.LOCATION_MODE_OFF);
} else {
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
isAvailable = !TextUtils.isEmpty(locationProviders);
}
boolean coarsePermissionCheck = (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED);
boolean finePermissionCheck = (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED);
return isAvailable && (coarsePermissionCheck || finePermissionCheck);
}
能用最简单的方法做吗
private boolean isLocationEnabled(Context context){
int mode =Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE,
Settings.Secure.LOCATION_MODE_OFF);
final boolean enabled = (mode != android.provider.Settings.Secure.LOCATION_MODE_OFF);
return enabled;
}
迁移到AndroidX并使用
implementation 'androidx.appcompat:appcompat:1.3.0'
并使用LocationManagerCompat
在Java中
private boolean isLocationEnabled(Context context) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return LocationManagerCompat.isLocationEnabled(locationManager);
}
在Kotlin
private fun isLocationEnabled(context: Context): Boolean {
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
return LocationManagerCompat.isLocationEnabled(locationManager)
}
要在android谷歌地图中获取当前地理位置,您应该打开设备位置选项。要检查位置是否打开,您可以从onCreate()方法简单地调用此方法。
private void checkGPSStatus() {
LocationManager locationManager = null;
boolean gps_enabled = false;
boolean network_enabled = false;
if ( locationManager == null ) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
try {
gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex){}
try {
network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex){}
if ( !gps_enabled && !network_enabled ){
AlertDialog.Builder dialog = new AlertDialog.Builder(MyActivity.this);
dialog.setMessage("GPS not enabled");
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//this will navigate user to the device location settings screen
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
AlertDialog alert = dialog.create();
alert.show();
}
}
在我看来,这个if子句很容易检查位置服务是否可用:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
//All location services are disabled
}