我正在开发一个应用程序在Android操作系统。我不知道如何检查位置服务是否启用。
我需要一个方法,如果它们被启用,返回“true”,如果没有“false”(所以在最后一种情况下,我可以显示一个对话框来启用它们)。
我正在开发一个应用程序在Android操作系统。我不知道如何检查位置服务是否启用。
我需要一个方法,如果它们被启用,返回“true”,如果没有“false”(所以在最后一种情况下,我可以显示一个对话框来启用它们)。
当前回答
根据上面的答案,在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);
}
其他回答
public class LocationUtil {
private static final String TAG = LocationUtil.class.getSimpleName();
public static LocationManager getLocationManager(final Context context) {
return (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
}
public static boolean isNetworkProviderEnabled(final Context context) {
return getLocationManager(context).isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
public static boolean isGpsProviderEnabled(final Context context) {
return getLocationManager(context).isProviderEnabled(LocationManager.GPS_PROVIDER);
}
// Returns true even if the location services are disabled. Do not use this method to detect location services are enabled.
private static boolean isPassiveProviderEnabled(final Context context) {
return getLocationManager(context).isProviderEnabled(LocationManager.PASSIVE_PROVIDER);
}
public static boolean isLocationModeOn(final Context context) throws Exception {
int locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
}
public static boolean isLocationEnabled(final Context context) {
try {
return isNetworkProviderEnabled(context) || isGpsProviderEnabled(context) || isLocationModeOn(context);
} catch (Exception e) {
Log.e(TAG, "[isLocationEnabled] error:", e);
}
return false;
}
public static void gotoLocationSettings(final Activity activity, final int requestCode) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
activity.startActivityForResult(intent, requestCode);
}
public static String getEnabledProvidersLogMessage(final Context context){
try{
return "[getEnabledProvidersLogMessage] isNetworkProviderEnabled:"+isNetworkProviderEnabled(context) +
", isGpsProviderEnabled:" + isGpsProviderEnabled(context) +
", isLocationModeOn:" + isLocationModeOn(context) +
", isPassiveProviderEnabled(ignored):" + isPassiveProviderEnabled(context);
}catch (Exception e){
Log.e(TAG, "[getEnabledProvidersLogMessage] error:", e);
return "provider error";
}
}
}
使用isLocationEnabled方法检测位置服务是否已启用。
https://github.com/Polidea/RxAndroidBle/issues/327#页面将提供更多信息为什么不使用被动提供程序,而是使用定位模式。
You can also use extension function.
------------------------------------
fun Context.isLocationEnabled(): Boolean{
val locationManager = 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();
}
}
到2020年
最近,最好和最短的方法是
@SuppressWarnings("deprecation")
public static Boolean isLocationEnabled(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// This is a new method provided in API 28
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return lm.isLocationEnabled();
} else {
// This was deprecated in API 28
int mode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE,
Settings.Secure.LOCATION_MODE_OFF);
return (mode != Settings.Secure.LOCATION_MODE_OFF);
}
}
LocationManager lm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
boolean gps_enabled = false;
boolean network_enabled = false;
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch(Exception e){
e.printStackTrace();
}
try {
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch(Exception e){
e.printStackTrace();
}
if(!gps_enabled && !network_enabled) {
// notify user
new AlertDialog.Builder(this)
.setMessage("Please turn on Location to continue")
.setPositiveButton("Open Location Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
}).
setNegativeButton("Cancel",null)
.show();
}