App 经常会用到地理位置授权, 下面介绍下 Android 端的 gps 和悬浮窗的封装
这里只展示部分代码, 交互类的创建 传送门
效果图
这里有个问题, 由于 Android 各大厂商都不一样, 所以当用户拒绝地理位置授权的时候, 不能准确的调到 App 的对应权限管理, 所以这里只是跳转了应用信息页面
应用详情页
- @ReactMethod // 检查浮动窗是否开启
- public void checkFloatWindowOpen() {
- final Activity activity = getCurrentActivity() ;
- if(Build.VERSION.SDK_INT>= 23){
- if (!Settings.canDrawOverlays(mReactContent)) {
- AlertDialog.Builder builder=new AlertDialog.Builder(activity);
- builder.setTitle("App 未开启悬浮窗权限");
- builder.setMessage("请允许 App 开启悬浮窗权限");
- builder.setPositiveButton("去设置", new DialogInterface.OnClickListener(){
- @Override
- public void onClick(DialogInterface dialog, int which)
- {
- // 第一个参数 dialog 是点击的确定按钮所属的 Dialog 对象, 第二个参数 which 是按钮的标示值
- // 系统定位未打开
- Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
- Uri.parse("package:" + activity.getPackageName()));
- activity.startActivityForResult(intent, 1234);
- }
- });
- builder.show();
- } else {
- //startService(floatWinIntent);
- // activity.startService(floatWinIntent);
- }
- };
- }
- @ReactMethod
- public void checkGpsOpen(){
- final Activity activity = getCurrentActivity() ;
- LocationManager locationManager
- = (LocationManager) mReactContent.getSystemService(Context.LOCATION_SERVICE);
- // 通过 GPS 卫星定位, 定位级别可以精确到街(通过 24 颗卫星定位, 在室外和空旷的地方定位准确, 速度快)
- boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
- // 通过 WLAN 或移动网络 (3G/2G) 确定的位置 (也称作 AGPS, 辅助 GPS 定位. 主要用于在室内或遮盖物(建筑群或茂密的深林等) 密集的地方定位)
- // boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
- if (gps ) { // 打开了定位服务
- // 判断应用是否授权允许 定位
- int checkResult = this.checkOp(this.getReactApplicationContext(),2,AppOpsManager.OPSTR_FINE_LOCATION);
- if (AppOpsManagerCompat.MODE_IGNORED == checkResult ) {
- AlertDialog.Builder builder=new AlertDialog.Builder(activity);
- builder.setTitle("手机已关闭位置权限");
- builder.setMessage("请在 设置 - 应用权限 (将位置权限打开");
- builder.setPositiveButton("去设置", new DialogInterface.OnClickListener(){
- @Override
- public void onClick(DialogInterface dialog, int which)
- {
- // 第一个参数 dialog 是点击的确定按钮所属的 Dialog 对象, 第二个参数 which 是按钮的标示值
- // 系统定位未打开
- // Intent localIntent = new Intent(Settings.ACTION_SETTINGS);
- Intent localIntent = new Intent();
- 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", activity.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", activity.getPackageName());
- }
- activity.startActivity(localIntent);
- }
- });
- builder.show();
- }
- }else{
- // 创建 AlertDialog 的构造器的对象
- AlertDialog.Builder builder=new AlertDialog.Builder(activity);
- builder.setTitle("手机未开启位置服务");
- builder.setMessage("请在 设置 - 位置信息 (将位置服务打开))");
- builder.setPositiveButton("去设置", new DialogInterface.OnClickListener(){
- @Override
- public void onClick(DialogInterface dialog, int which)
- {
- // 第一个参数 dialog 是点击的确定按钮所属的 Dialog 对象, 第二个参数 which 是按钮的标示值
- // 系统定位未打开
- Intent intent = new Intent();
- intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
- activity.startActivityForResult(intent, 1315);
- }
- });
- builder.show();
- }
- }
- public 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;
- }
关于 Android 的弹出框 AlertDialog 的使用, 最后不要忘记使用 show()把其显示.
来源: http://www.jianshu.com/p/1bb6fdf0ead0