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

所属分类: 软件编程 / Android 阅读数: 72
收藏 0 赞 0 分享

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

判断定位服务:

/**
   * 手机是否开启位置服务,如果没有开启那么所有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)) {//检测是否开启定位服务
      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,判断跳转页面:

/**
   * 无法定位对话框
   *
   * @param activity 上下文
   * @param state  权限状态0,未开启服务 1,未开启权限
   * @return 对话框
   */
  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样式:

<style name="MyDialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:background">@color/transparent</item>
  </style>

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

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多