2017-01-23 85 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");
相關問題