当前位置:   article > 正文

一个封装Android Activity Permission操作的Activity: AppCompatActivityEx_android 一个activity 管理权限

android 一个activity 管理权限

每个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();
            }
        }
    }
}

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/559130
推荐阅读
相关标签
  

闽ICP备14008679号