2017-01-23 83 views
-1

当弹出权限对话框时,会询问我是否允许或拒绝某个权限。事情是,当我允许或否认时,行动不执行。我如何知道他是否允许,以便我可以在接受后执行操作?如何知道用户是否允许提示权限

我想:

int hasWriteExternalStoragePermission = ctx.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE); 
    if (hasWriteExternalStoragePermission == PackageManager.PERMISSION_GRANTED) { 
     // my code 
    } 

但它不执行,因为它是“亡羊补牢”

+0

入住这http://stackoverflow.com/a/31925748/2435238 – sJy

+0

您应该遵循关于[运行时权限](https://developer.android.com/training/permissions/requesting.html)的指南。这通常是Android基础知识的良好开端 – AxelH

回答

1

您必须覆盖onRequestPermissionsResult功能在那里你会得到回电的permission Dialog pop up

像下面

public void onRequestPermissionsResult(int requestCode, 
     String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case MY_PERMISSIONS_REQUEST_READ_CONTACTS: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
       && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       // permission was granted 
      } else { 

       // permission denied 
       // ask again or ignore 

      } 
      return; 
     } 

     // other 'case' lines to check for other 
     // permissions this app might request 
    } 
} 
1

你需要重写此方法

@Override 
public void onRequestPermissionsResult(int requestCode, 
     String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case MY_PERMISSIONS_REQUEST_READ_CONTACTS: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
       && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       // permission was granted, yay! Do the 
       // contacts-related task you need to do. 

      } else { 

       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 
      } 
      return; 
     } 

     // other 'case' lines to check for other 
     // permissions this app might request 
    } 
} 

link官方文档

0

试试这个方法:

public boolean isStoragePermissionGranted() 
{ 
    if (Build.VERSION.SDK_INT >= 23) 
    { 
     if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) 
       == PackageManager.PERMISSION_GRANTED) 
     { 
      Log.v(TAG,"Permission is granted"); 
      return true; 
     } 
     else 
     { 

      Log.v(TAG,"Permission is revoked"); 
      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); 
      return false; 
     } 
    } 
    else 
    { 
     //permission is automatically granted on sdk<23 upon installation 
     Log.v(TAG,"Permission is granted <23"); 
     return true; 
    } 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) 
{ 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    if(grantResults[0]== PackageManager.PERMISSION_GRANTED) 
    { 
     Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]); 
     //resume tasks needing this permission 
    } 

    else 
    { 
     Log.v(TAG,"Permission denied"); 
    } 
} 

这样,如果权限被授予它会来这里:

  • Log.v(TAG,"Permission is granted");

如果许可尚未批准,它会来这里:

  • Log.v(TAG,"Permission is revoked");并试图获得许可。

如果用户授予在运行时的权限:

  • Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);

如果用户否认:

  • Log.v(TAG,"Permission denied");

如果版本低于Marshamallow,即低于API 23的话,那就来这里:

  • Log.v(TAG,"Permission is granted <23");
相关问题