在android中使用定位服务是很平常的事 一般大家都会使用
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
//All location services are disabled
}
这种模式,但是在高版本的手机中locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)和locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
不管你是否开启 都会返回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);
}

更多相关文章

  1. Android 6.0 动态权限申请实例
  2. android如何设置adb root权限
  3. Android 截图程序实现 需要root权限
  4. Android 常见权限列表
  5. java拷贝文件夹和android设置文件权限
  6. 关于android录音权限被用户禁用的问题解决方案
  7. android动态申请拍照获取照片权限

随机推荐

  1. 【Android(安卓)TextView/EditText 不允
  2. 使用Android内部的DownloadProvider下载
  3. Android(安卓)渗透测试学习手册 第七章
  4. Android(安卓)关于android:name属性问题
  5. 为什么说Android令人沮丧!?
  6. 谷歌正式发布Android(安卓)2.2手机操作平
  7. Android高手进阶教程(二十)之---Android
  8. Android下xml中RelativeLayout布局常见属
  9. Android高手进阶教程(二十)之---Android
  10. android:layout_gravity 和 android:grav