一个封装Android Activity Permission操作的Activity: AppCompatActivityEx

每个android app都要写一推权限设置的代码有点繁琐,于是就基于 AppCompatActivity 写了一个扩展类,封装permissions操作:

用起来比原来方便多了,只需在你的 MainActivity中修改两处:

public class MainActivity extends AppCompatActivityEx {

    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        requestPermissions(
                new String[]{
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE},
                new AppCompatActivityEx.PermissionCallback() {
                    @Override
                    public void onPermissionGranted() {
                        LogEx.trace();
                    }

                    @Override
                    public void onPermissionDenied() {
                        LogEx.trace();
                    }
                }
        );

 

即可省去权限申请的一堆代码。

下面是该类的实现,欢迎使用和提宝贵意见~

AppCompatActivityEx.java

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@SuppressLint("Registered")
public class AppCompatActivityEx extends AppCompatActivity {

    private static final int REQUEST_CODE_PERMISSIONS = 2297;

    private PermissionCallback mPermissionCallback;

    public interface PermissionCallback {
        void onPermissionGranted();
        void onPermissionDenied();
    }

    public void requestPermissions(String[] permissions, PermissionCallback callback) {
        boolean allPermissionsGranted = true;
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                allPermissionsGranted = false;
                break;
            }
        }
        if (allPermissionsGranted) {
            if (callback != null) {
                callback.onPermissionGranted();
            }
        }
        else {
            mPermissionCallback = callback;
            ActivityCompat.requestPermissions(this, permissions,
                    REQUEST_CODE_PERMISSIONS);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        boolean grantedAll = true;
        if (requestCode == REQUEST_CODE_PERMISSIONS && grantResults.length == permissions.length) {
            for (int grantResult : grantResults) {
                if (grantResult != PackageManager.PERMISSION_GRANTED) {
                    grantedAll = false;
                }
            }
        }
        if (grantedAll) {
            if (mPermissionCallback != null) {
                mPermissionCallback.onPermissionGranted();
            }
        }
        else {
            Toast.makeText(this, "Request Permissions Failed", Toast.LENGTH_LONG).show();
            if (mPermissionCallback != null) {
                mPermissionCallback.onPermissionDenied();
            }
        }
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

你可能感兴趣的:(实用小工具,软件开发)