android 權限封裝(思路來至於RxPermissions)

思路很簡單,就是我們自定義一個Fragment,每次請求權限,都是用這個fragment去請求所需要的權限,那麼回調的地方就固定了(就是會掉這個fragment對應的onRequestPermissionsResult(xxxx)函數中),具體代碼如下:

PermissionFragment 類:

package com.example.androidfram.utils;
import android.app.Fragment;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import java.util.ArrayList;
import java.util.List;

public class PermissionFragment extends Fragment{
private static final int PERMISSIONS_REQUEST_BACK =142 ;
private PermissionUtil.PermissionsListener permissionsListener;

    @RequiresApi(api = Build.VERSION_CODES.M)
    public void fetchPermisson(String[] permissonList, final PermissionUtil.PermissionsListener permissionsListener){
        this.permissionsListener=permissionsListener;
        List<String> mPermissionList = new ArrayList<>();
        for (int i=0;i<permissonList.length;i++){
            if ((ContextCompat.checkSelfPermission(getContext(), permissonList[i]) != PackageManager.PERMISSION_GRANTED)
                    || shouldShowRequestPermissionRationale(permissonList[i])){
                mPermissionList.add(permissonList[i]);
            }
        }

        if (mPermissionList.size()>0) {
            requestPermissions(permissonList, PERMISSIONS_REQUEST_BACK);
        }else {
            permissionsListener.permissonApplied();
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        permissionsListener.accept(requestCode, permissions, grantResults);
    }
}

PermissionUtil 類:

package com.example.androidfram.utils;

import android.app.Activity;
import android.app.FragmentManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;

import com.example.androidfram.fragments.Fragment;

public class PermissionUtil {
     private PermissionFragment permissionFragment;
     private final String TAG = "permissionFragment";
	
     public PermissionUtil(Activity activity){
     	permissionFragment=getRxPermissionsFragment(activity);
    }

    public PermissionUtil(Fragment fragment){
        permissionFragment=getRxPermissionsFragment(fragment.getActivity());
    }


    //一次性請求多個
    @RequiresApi(api = Build.VERSION_CODES.M)
    public void fetchPermisson(String[] permissonList, final PermissionsListener permissionsListener){
        permissionFragment.fetchPermisson(permissonList,permissionsListener);
    }


    private PermissionFragment getRxPermissionsFragment(Activity activity) {
        PermissionFragment rxPermissionsFragment = findRxPermissionsFragment(activity);
        boolean isNewInstance = rxPermissionsFragment == null;
        if (isNewInstance) {
            rxPermissionsFragment = new PermissionFragment();
            FragmentManager fragmentManager = activity.getFragmentManager();
            fragmentManager
                    .beginTransaction()
                    .add(rxPermissionsFragment, TAG)
                    .commitAllowingStateLoss();
            fragmentManager.executePendingTransactions();
        }
        return rxPermissionsFragment;
    }

    private PermissionFragment findRxPermissionsFragment(Activity activity) {
        return (PermissionFragment) activity.getFragmentManager().findFragmentByTag(TAG);
    }

    public interface PermissionsListener{
        //申請權限回調接口
        void accept(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults);

        //權限申請全部通過後的回調接口
        void permissonApplied();
    }
}

最後就是,那怎麼調用呢?

//獲取權限
private void initPermission() {
	String[] permissonList = new String[]{
        Manifest.permission.INTERNET,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.READ_PHONE_STATE,
        Manifest.permission.RECORD_AUDIO,
        Manifest.permission.ACCESS_COARSE_LOCATION,
        Manifest.permission.CALL_PHONE,
        Manifest.permission.CAMERA

};
    if (Build.VERSION.SDK_INT >= 23) {
        //權限請求
        new PermissionUtil(this).fetchPermisson(permissonList, new PermissionUtil.PermissionsListener() {
            @Override
            public void accept(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
           }

            @Override
            public void permissonApplied() {
              
            }
        });
    } else {
    
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章