react native (GPS,懸浮窗 android)許可權封裝沒有跳轉到相應設定
APP 經常會用到地理位置授權,下面介紹下Android端的gps 和懸浮窗的封裝
這裡只展示部分程式碼,互動類的建立 ofollow,noindex">傳送門

效果圖
這裡有個問題,由於Android 各大廠商都不一樣,所以當用戶拒絕地理位置授權的時候,不能準確的調到app的對應許可權管理,所以這裡只是跳轉了應用資訊頁面

應用詳情頁
@ReactMethod//檢查浮動窗是否開啟 publicvoid 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 publicvoid 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(); } } publicint 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()把其顯示。