react native (GPS,懸浮窗 android)權(quán)限封裝沒有跳轉(zhuǎn)到相應(yīng)設(shè)置

APP 經(jīng)常會用到地理位置授權(quán),下面介紹下Android端的gps 和懸浮窗的封裝
這里只展示部分代碼,交互類的創(chuàng)建 傳送門

效果圖

這里有個問題,由于Android 各大廠商都不一樣,所以當用戶拒絕地理位置授權(quán)的時候,不能準確的調(diào)到app的對應(yīng)權(quán)限管理,所以這里只是跳轉(zhuǎn)了應(yīng)用信息頁面
應(yīng)用詳情頁

 @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未開啟懸浮窗權(quán)限");
                builder.setMessage("請允許APP開啟懸浮窗權(quán)限");
                builder.setPositiveButton("去設(shè)置", new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        //第一個參數(shù)dialog是點擊的確定按鈕所屬的Dialog對象,第二個參數(shù)which是按鈕的標示值
                        //系統(tǒng)定位未打開


                        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衛(wèi)星定位,定位級別可以精確到街(通過24顆衛(wèi)星定位,在室外和空曠的地方定位準確、速度快)
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        // 通過WLAN或移動網(wǎng)絡(luò)(3G/2G)確定的位置(也稱作AGPS,輔助GPS定位。主要用于在室內(nèi)或遮蓋物(建筑群或茂密的深林等)密集的地方定位)
//        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (gps ) {  //打開了定位服務(wù)
          //判斷應(yīng)用是否授權(quán)允許 定位
            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("手機已關(guān)閉位置權(quán)限");
                builder.setMessage("請在 設(shè)置-應(yīng)用權(quán)限 (將位置權(quán)限打開");
                builder.setPositiveButton("去設(shè)置", new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        //第一個參數(shù)dialog是點擊的確定按鈕所屬的Dialog對象,第二個參數(shù)which是按鈕的標示值
                        //系統(tǒng)定位未打開


//                        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{
            //創(chuàng)建AlertDialog的構(gòu)造器的對象
            AlertDialog.Builder builder=new AlertDialog.Builder(activity);
            builder.setTitle("手機未開啟位置服務(wù)");
            builder.setMessage("請在 設(shè)置-位置信息 (將位置服務(wù)打開))");
            builder.setPositiveButton("去設(shè)置", new DialogInterface.OnClickListener(){
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    //第一個參數(shù)dialog是點擊的確定按鈕所屬的Dialog對象,第二個參數(shù)which是按鈕的標示值
                    //系統(tǒng)定位未打開
                    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;
    }

關(guān)于Android的彈出框 AlertDialog的使用,最后不要忘記使用 show()把其顯示。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容