Why run time permissions not working in oreo 8.1.0 or pie version?

▼魔方 西西 提交于 2019-12-25 01:50:25

问题


public void EnableRuntimePermission(){
   if(ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_EXTERNAL_STORAGE)
           != PackageManager.PERMISSION_GRANTED){
       if (ActivityCompat.shouldShowRequestPermissionRationale(
               MainActivity.this,
               Manifest.permission.READ_EXTERNAL_STORAGE))
       {ActivityCompat.requestPermissions(MainActivity.this,new String[]{
                   Manifest.permission.READ_EXTERNAL_STORAGE}, RequestPermissionCode);
       }
   }
}

@Override
public void onRequestPermissionsResult(int RC, String per[], int[] PResult) {
    switch (RC) {
        case RequestPermissionCode:
            if (PResult.length > 0 && PResult[0] == PackageManager.PERMISSION_GRANTED) {
                getNameEmailDetails();


            } else {                 

            }
            break;
    }
}

This code works fine for below oreo versions but not work in 8.1.0 or plus these versions


回答1:


Check this it works for me,

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    { if (checkPermission())
        {
         //do your task
      } else 
       { 
            requestPermission(); 
       }
    } else
    {  
    //do your task
   }

}

protected boolean checkPermission()
{
    int result = ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE);
    if (result == PackageManager.PERMISSION_GRANTED)
    {
        return true;
    } else {
        return false;
    }
}

//file
protected void requestPermission()
{

    if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.READ_EXTERNAL_STORAGE))
    {
        Toast.makeText(MainActivity.this, "Read External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
    } else
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            requestPermissions(new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
        }
    }
}

//file
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
{
    switch (requestCode)
    {
        case 100:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {
                createFile();
                //check here code is needed or not
            } else
            {
                Log.e("value", "Permission Denied, You cannot use local drive .");
                Toast.makeText(MainActivity.this,"Permission Denied",Toast.LENGTH_SHORT).show();
            }
            break;
    }
}


来源:https://stackoverflow.com/questions/55452233/why-run-time-permissions-not-working-in-oreo-8-1-0-or-pie-version

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