栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android判断定位功能是否可用的方法

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android判断定位功能是否可用的方法

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

判断定位服务:


  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;
  }

判断定位权限:


  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)) {//检测是否开启定位服务
      if (netErrorDialog == null || !netErrorDialog.isShowing()) {
 locErrorDialog = DialogUtil.showLocErrorDialog(activity, 0);
      }
    } 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) {
 if (netErrorDialog == null || !netErrorDialog.isShowing()) {
   locErrorDialog = DialogUtil.showLocErrorDialog(activity, 1);
 }
      }
    }
  }

如果不能使用,弹出对话框,根据1或2,判断跳转页面:


  public static Dialog showLocErrorDialog(Activity activity, int state) {
    Dialog locErrorDialog = new Dialog(activity, R.style.MyDialog);
    View contentView = View.inflate(activity, R.layout.dialog_tip_error_loc, null);
    locErrorDialog.setContentView(contentView);
    locErrorDialog.setCanceledonTouchOutside(true);
    locErrorDialog.show();
    TextView checkNetCancel = contentView.findViewById(R.id.tv_submit_no);
    TextView checkNet = contentView.findViewById(R.id.tv_submit_yes);
    checkNetCancel.setonClickListener(view -> {
      locErrorDialog.dismiss();
    });
    checkNet.setonClickListener(view -> {
      locErrorDialog.dismiss();
      Intent intent = new Intent();
      if (state == 0) {
 //定位服务页面
 intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
      } else {
 //应用详情页面
 intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
 intent.setData(Uri.parse("package:" + activity.getPackageName()));
      }
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      try {
 activity.startActivity(intent);
      } catch (ActivityNotFoundException ex) {
 //如果页面无法打开,进入设置页面
 intent.setAction(Settings.ACTION_SETTINGS);
 try {
   activity.startActivity(intent);
 } catch (Exception e) {
   e.printStackTrace();
 }
      }
    });
    return locErrorDialog;
  }

Dialog样式:


    @null
    @android:color/transparent
    true
    @color/transparent
  

以上这篇Android判断定位功能是否可用的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/155705.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号