How do I implelment Android 6.0 Runtime Permissions On Existing App

假装没事ソ 提交于 2019-11-28 10:21:31

The runtime permission model for Android 6.0 is mainly divided into part

1. Checking Permission

2. Requesting Permission

you can create two method for this thing in your activity, As follow

Check Permission

private boolean checkPermission(){
    int result = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_SMS);
    if (result == PackageManager.PERMISSION_GRANTED){

        return true;

    } else {

        return false;

    }
}

Request Permission

private void requestPermission(){

    if (ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.READ_SMS)){

        Toast.makeText(context,"Read Sms Allowed.",Toast.LENGTH_LONG).show();

    } else {

        ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.READ_SMS},PERMISSION_REQUEST_CODE);
    }
}

Last but not least you need to override the onRequestPermissionsResult method

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_CODE:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                Snackbar.make(view,"Permission Granted, Now you can access SMS.",Snackbar.LENGTH_LONG).show();

            } else {

                Snackbar.make(view,"Permission Denied, You cannot access SMS.",Snackbar.LENGTH_LONG).show();

            }
            break;
    }
}

as you asked do i need to run this in thread .. answer is No Just do this in main thread

If you want to do less code then, please use Dexter is an Android library that simplifies the process of requesting permissions at runtime.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!