Android定位服务关闭和定位(悬浮)等权限拒绝的判断

摘要:
publicvoidcheckLocationPermission(){if(!PermissionHelper.isLocServiceEnable(this)){//检查位置服务DlgUtils.showLocServiceDialog(thiselse{//检查用户是否拒绝当前应用程序的定位权限intcheckResult=PermissionHelper.checkOp(thi
  public voidcheckLocationPermission() {
        if (!PermissionHelper.isLocServiceEnable(this)) {//检测是否开启定位服务
            DlgUtils.showLocServiceDialog(this);
        } else{//检测用户是否将当前应用的定位权限拒绝
            int checkResult = PermissionHelper.checkOp(this, 2, AppOpsManager.OPSTR_FINE_LOCATION);//其中2代表AppOpsManager.OP_GPS,如果要判断悬浮框权限,第二个参数需换成24即AppOpsManager。OP_SYSTEM_ALERT_WINDOW及,第三个参数需要换成AppOpsManager.OPSTR_SYSTEM_ALERT_WINDOW
            int checkResult2 = PermissionHelper.checkOp(this, 1, AppOpsManager.OPSTR_FINE_LOCATION);
            if (AppOpsManagerCompat.MODE_IGNORED == checkResult || AppOpsManagerCompat.MODE_IGNORED ==checkResult2) {
                DlgUtils.showLocIgnoredDialog(this);
            }
        }
    }
DlgUtils.java代码如下
/*** 显示定位服务未开启确认对话框
     */
    public static void showLocServiceDialog(finalContext context) {
        new AlertDialog.Builder(context).setTitle("手机未开启位置服务")
                .setMessage("请在 设置-位置信息 (将位置服务打开))")
                .setNegativeButton("取消", null)
                .setPositiveButton("去设置", newDialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, intwhich) {
                        Intent intent = newIntent();
                        intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        try{
                            context.startActivity(intent);
                        } catch(ActivityNotFoundException ex) {
                            intent.setAction(Settings.ACTION_SETTINGS);
                            try{
                                context.startActivity(intent);
                            } catch(Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                })
                .show();
    }

    /*** 显示定位权限被拒绝对话框
     */
    public static void showLocIgnoredDialog(finalContext context) {
        AlertDialog.Builder builder = newAlertDialog.Builder(context);
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setTitle("手机已关闭位置权限");
        builder.setMessage("请在 设置-应用权限 (将位置权限打开))");

        //监听下方button点击事件
        builder.setPositiveButton("去设置", newDialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, inti) {
                Intent localIntent = newIntent();
                localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                if (Build.VERSION.SDK_INT >= 9) {
                    localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                    localIntent.setData(Uri.fromParts("package", getPackageName(), null));
                } else if (Build.VERSION.SDK_INT <= 8) {
                    localIntent.setAction(Intent.ACTION_VIEW);
                    localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
                    localIntent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
                }
                context.startActivity(localIntent);
            }
        });
        builder.setNegativeButton("取消", newDialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, inti) {
                dialogInterface.dismiss();
            }
        });

        //设置对话框是可取消的
        builder.setCancelable(true);
        final AlertDialog dialog =builder.create();
        dialog.show();
    }
PermissionHelper.java类如下:
/*** 手机是否开启位置服务,如果没有开启那么所有app将不能使用定位功能
     */
    public static booleanisLocServiceEnable(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;
    }

    /*** 检查权限列表
     *
     * @paramcontext
     * @paramop
     * 这个值被hide了,去AppOpsManager类源码找,如位置权限  AppOpsManager.OP_GPS==2
     * @paramopString
     *   如判断定位权限 AppOpsManager.OPSTR_FINE_LOCATION
     * @return  @see如果返回值 AppOpsManagerCompat.MODE_IGNORED 表示被禁用了

     */
    public static int checkOp(Context context, intop, 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) {
                    returnAppOpsManagerCompat.noteOp(context, opString,context.getApplicationInfo().uid,
                            context.getPackageName());
                }

            }
        }
        return -1;
    }
附上一些AppOpsManage值
/**@hide No operation specified. */
    public static final int OP_NONE = -1;
    /**@hide Access to coarse location information. */
    public static final int OP_COARSE_LOCATION = 0;
    /**@hide Access to fine location information. */
    public static final int OP_FINE_LOCATION = 1;
    /**@hide Causing GPS to run. */
    public static final int OP_GPS = 2;
    /**@hide */
    public static final int OP_VIBRATE = 3;
    /**@hide */
    public static final int OP_READ_CONTACTS = 4;
    /**@hide */
    public static final int OP_WRITE_CONTACTS = 5;
    /**@hide */
    public static final int OP_READ_CALL_LOG = 6;
    /**@hide */
    public static final int OP_WRITE_CALL_LOG = 7;
    /**@hide */
    public static final int OP_READ_CALENDAR = 8;
    /**@hide */
    public static final int OP_WRITE_CALENDAR = 9;
    /**@hide */
    public static final int OP_WIFI_SCAN = 10;
    /**@hide */
    public static final int OP_POST_NOTIFICATION = 11;
    /**@hide */
    public static final int OP_NEIGHBORING_CELLS = 12;
    /**@hide */
    public static final int OP_CALL_PHONE = 13;
    /**@hide */
    public static final int OP_READ_SMS = 14;
    /**@hide */
    public static final int OP_WRITE_SMS = 15;
    /**@hide */
    public static final int OP_RECEIVE_SMS = 16;
    /**@hide */
    public static final int OP_RECEIVE_EMERGECY_SMS = 17;
    /**@hide */
    public static final int OP_RECEIVE_MMS = 18;
    /**@hide */
    public static final int OP_RECEIVE_WAP_PUSH = 19;
    /**@hide */
    public static final int OP_SEND_SMS = 20;
    /**@hide */
    public static final int OP_READ_ICC_SMS = 21;
    /**@hide */
    public static final int OP_WRITE_ICC_SMS = 22;
    /**@hide */
    public static final int OP_WRITE_SETTINGS = 23;
    /**@hide */
    public static final int OP_SYSTEM_ALERT_WINDOW = 24;

 public static final String OPSTR_FINE_LOCATION =
            "android:fine_location";
 /**Read previously received cell broadcast messages. */
    public static finalString OPSTR_READ_CELL_BROADCASTS
            = "android:read_cell_broadcasts";
    /**Inject mock location into the system. */
    public static finalString OPSTR_MOCK_LOCATION
            = "android:mock_location";
    /**Read external storage. */
    public static finalString OPSTR_READ_EXTERNAL_STORAGE
            = "android:read_external_storage";
    /**Write external storage. */
    public static finalString OPSTR_WRITE_EXTERNAL_STORAGE
            = "android:write_external_storage";
    /**Required to draw on top of other apps. */
    public static finalString OPSTR_SYSTEM_ALERT_WINDOW
            = "android:system_alert_window";

免责声明:文章转载自《Android定位服务关闭和定位(悬浮)等权限拒绝的判断》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇用友U8凭证导入工具的使用方法高版本chrome不再支持window.showmodaldialog 的临时替换方案【用window.open】下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Spring框架针对dao层的jdbcTemplate操作crud之query查询数据操作

查询目标是完成3个功能: (1)查询表,返回某一个值。例如查询表中记录的条数,返回一个int类型数据 (2)查询表,返回结果为某一个对象。 (3)查询表,返回结果为某一个泛型的list集合。 一、查询表中记录的条数,返回一个int类型数据的操作方法 使用jdbcTemplate 原理是把加载驱动Class.forName("com.mysql.jdbc.D...

Spring MVC与Dubbo的整合一

一、Dubbo是什么 一款分布式服务框架 高性能和透明化的RPC远程服务调用方案 SOA服务治理方案 每天为2千多个服务提供大于30亿次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点以及别的公司的业务中。 具体dubbo的背景和简介以及框架等基础知识参考这位大神的博客 二、提供者的Dubbo配置 首先我们先配置服务的提供者 1.给作为提供者的Spr...

Android TimePickerDialog样式配置与TimePicker模式选择

习惯性的,把要说的内容先总结一下: TimePicker有两种模式:spinner 和clock,可通过如下方式配置: <TimePicker android:timePickerMode = "spinner" android:layout_width="match_parent" andr...

Canvas:绘制路径

Canvas:绘制路径 绘制路径   图形的基本元素是路径。路径是[通过不同颜色和宽度的线段或曲线相连形成的不同形状的]点的集合。一个路径,甚至一个子路径,都是闭合的。   使用路径绘制图形需要一些额外的步骤。 首先,你需要创建路径起始点。 然后你使用画图命令去绘制路径。 之后把路径进行封闭。 一旦路径生成,你就能通过描边或填充路径区域来渲染图形。 函...

LTE信令流程之专用承载流程介绍

https://www.mscbsc.com/viewnews-102220.html  感谢作者! 【LTE基础知识】LTE信令流程之专用承载流程介绍 发布: 2014-08-11 16:40 | 作者: MSCBSC | 来源: 移动通信网 | 字体:  小  中  大  相关专题:基础知识无线   专用承载建立流程    专用承载建立流程说...

Android中WebView使用总结

1.在应用的配置文件中添加网络权限 1 <!-- 添加使用WebView的需要的internet权限 --> 2 <uses-permission android:name="android.permission.INTERNET"/> 2.支持JavaScript 1 //开启Javascript支...