6.0以上获取位置信息,需要动态申请,这里讨论当位置权限申请同意了,但是位置信息关闭了(其实就是6.0以前的gps开关)高精度定位也会获取失败!(网上有的检测方法在某些手机上会检测失败,下列的方式亲测有效)
![位置信息开关](https://upload-images.jianshu.io/upload_images/5439590-d468261b30c7632c.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| package com.guoshikeji.xiaoxiangDriver.utils;
import android.content.Context; import android.content.Intent; import android.os.Build; import android.provider.Settings; import android.text.TextUtils; /** * Created by tyl * 2019/5/22/022 * Describe: */ public class GpsUtil { /** * 判断GPS是否开启,GPS或者AGPS开启一个就认为是开启的 * @param context * @return true 表示开启 */ public static final boolean isOPen(final Context context) { if (context==null){ return true; } int locationMode = 0; String locationProviders; 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(); return false; } return locationMode != Settings.Secure.LOCATION_MODE_OFF; } else { locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); return !TextUtils.isEmpty(locationProviders); } }
/** * 跳转设置 打开GPS * @param context */ public static final void openGPS(Context context) { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); context.startActivity(intent); } }
|