定位功能是否可用由定位服务和定位权限共同决定:
判断定位服务:

/** * @author:程龙 date; On 2018/8/13 *//**  * 手机是否开启位置服务,如果没有开启那么所有app将不能使用定位功能  */    public static boolean isLocServiceEnable(Context context) {        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);        if (gps || network) {            return true;        }        return false;    }

判断定位权限:

/**     * 检查权限列表     *     * @param context     * @param op       这个值被hide了,去AppOpsManager类源码找,如位置权限  AppOpsManager.OP_GPS==2     * @param opString 如判断定位权限 AppOpsManager.OPSTR_FINE_LOCATION     * @return @see 如果返回值 AppOpsManagerCompat.MODE_IGNORED 表示被禁用了     */    public static int checkOp(Context context, int op, String opString) {        final int version = Build.VERSION.SDK_INT;        if (version >= 19) {            Object object = context.getSystemService(Context.APP_OPS_SERVICE);//            Object object = context.getSystemService("appops");            Class c = object.getClass();            try {                Class[] cArg = new Class[3];                cArg[0] = int.class;                cArg[1] = int.class;                cArg[2] = String.class;                Method lMethod = c.getDeclaredMethod("checkOp", cArg);                return (Integer) lMethod.invoke(object, op, Binder.getCallingUid(), context.getPackageName());            } catch (Exception e) {                e.printStackTrace();                if (Build.VERSION.SDK_INT >= 23) {                    return AppOpsManagerCompat.noteOp(context, opString, context.getApplicationInfo().uid,                            context.getPackageName());                }            }        }        return -1;    }

调用时先检查权限:

/**     * 检查定位服务、权限     */    private void checkLocationPermission() {        if (!AppUtil.isLocServiceEnable(this)) {//检测是否开启定位服务            //未开启定位服务的操作        } else {//检测用户是否将当前应用的定位权限拒绝            int checkResult = AppUtil.checkOp(this, 2, AppOpsManager.OPSTR_FINE_LOCATION);//其中2代表AppOpsManager.OP_GPS,如果要判断悬浮框权限,第二个参数需换成24即AppOpsManager。OP_SYSTEM_ALERT_WINDOW及,第三个参数需要换成AppOpsManager.OPSTR_SYSTEM_ALERT_WINDOW            int checkResult2 = AppUtil.checkOp(this, 1, AppOpsManager.OPSTR_FINE_LOCATION);            if (AppOpsManagerCompat.MODE_IGNORED == checkResult || AppOpsManagerCompat.MODE_IGNORED == checkResult2) {               //未开启定位权限或者被拒绝的操作            }        }    }

根据以上代码进行符合项目需求的操作即可。

更多相关文章

  1. Android中的Environment.getExternalStorageState使用
  2. android framework中添加自定义的permission
  3. Android权限设置android.permission完整列表
  4. 小米,红米手机android 6.0以下 权限管理
  5. 【Android學習專題】實用參考:android权限大全
  6. android 权限大全
  7. Android应用程序获取ROOT权限的方法
  8. Android(安卓)JNI编程内存问题定位方法
  9. [Android基础]四大组件之ContentProvider

随机推荐

  1. android中的数据库操作
  2. Android实现滑动的7种方法
  3. Android如何检测网络的类型为3G、2G、wap
  4. Android视频采集
  5. Android应用程序的生命周期
  6. AsyncTask总结
  7. Android(安卓)View(一)-View坐标以及方法
  8. android输入法02:openwnn源码解析01—输入
  9. Android中实现跨进程通信(IPC)的几种方式
  10. Android实现关机与重启的几种方式(推荐)