关于Android6.0没有弹出运行时权限弹窗的问题

问题描述

如题,在Android6.0的手机上如果需要使用到权限的时候,是会弹出框让用户授权,如果拒绝则会造成APP某些功能不能使用或者直接Crash。

而且有的时候,并不会弹窗,使用某功能的时候直接的就crash了,如果不了解运行时权限的话,查看日志发现没有权限,但是自己明明在manifest是声明过权限,造成分析问题困难。原因就在于此,运行时权限没有弹出来。

我遇到的问题是要去在手机上新建目录不成功。

解决思路

来直接上代码

public class MainActivity extends AppCompatActivity {
    private static final int REQUEST_RUNTIME_PERMISSION = 123;
    private static final String DATA_PATH = Environment.getExternalStorageDirectory().toString() + "/Test/";
    private static final String TESTDATA = "testdata"
   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //android6.0手机上权限问题不能创建目录导致崩溃
        if (CheckPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            // you have permission go ahead
            prepareDirectory(DATA_PATH + TESTDATA);
        } else {
            // you do not have permission go request runtime permissions
            RequestPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE, REQUEST_RUNTIME_PERMISSION);
        }
    }
    @Override
    public void onRequestPermissionsResult(int permsRequestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (permsRequestCode) {

            case REQUEST_RUNTIME_PERMISSION: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // you have permission go ahead
                    prepareDirectory(DATA_PATH + TESTDATA);
                } else {
                    // you do not have permission show toast.
                }
                return;
            }
        }
    }
    public void RequestPermission(Activity thisActivity, String Permission, int Code) {
        if (ContextCompat.checkSelfPermission(thisActivity,
                Permission)
                != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                    Permission)) {
            } else {
                ActivityCompat.requestPermissions(thisActivity,
                        new String[]{Permission},
                        Code);
            }
        }
    }

    public boolean CheckPermission(Context context, String Permission) {
        if (ContextCompat.checkSelfPermission(context,
                Permission) == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            return false;
        }
    }

    private void prepareDirectory(String path) {

        File dir = new File(path);
        if (!dir.exists()) {
            if (!dir.mkdirs()) {
                Log.e(TAG, "ERROR: Creation of directory " + path + " failed, check does Android Manifest have permission to write to external storage.");
            }
        } else {
            Log.i(TAG, "Created directory " + path);
        }
    }
}

相信代码很容易看懂,如果小伙伴有更好的方法,可以告诉我。

推荐阅读更多精彩内容