2016-03-13 82 views
1

我正在使用rooted M設備並嘗試訪問其他應用的權限設置。 我想知道哪些權限授予或撤銷用戶爲每個應用程序 這可以嗎?檢測權限是否被吊銷

can checkSelfPermission()爲此工作? 另外,如果我下載它不是爲M版開發的應用程序可以檢測操作的工作或沒有,因爲我發現,任何應用程序絲毫目標版本低於23總是會返回PERMISSION_GRANTED

checkSelfPermission returning PERMISSION_GRANTED for revoked permission with targetSdkVersion <= 22

回答

0

喜這裏是爲Android M設置權限的幾個步驟,並記住你應該在清單文件中聲明相同的權限。

步驟1. 聲明全局變量:

public static final int MY_PERMISSIONS_REQUEST_LOCATION = 0; 

第2步 使用此代碼在您的主要活動

private void locationpermission() { 
    // Here, thisActivity is the current activity 
    if (ContextCompat.checkSelfPermission(activity 
      , 
      Manifest.permission.ACCESS_COARSE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 

     // Should we show an explanation? 
     if (ActivityCompat.shouldShowRequestPermissionRationale(activity, 
       Manifest.permission.ACCESS_COARSE_LOCATION)) { 

      // Show an expanation to the user *asynchronously* -- don't block 
      // this thread waiting for the user's response! After the user 
      // sees the explanation, try again to request the permission. 

     } else { 

      // No explanation needed, we can request the permission. 

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

      // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an 
      // app-defined int constant. The callback method gets the 
      // result of the request. 
     } 
    } 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, 
             String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case MY_PERMISSIONS_REQUEST_LOCATION: { 
      // 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 
    } 
} 

步驟3 調用此方法在你onCreate方法,

locationpermission(); 

而且任何人p你可以從這裏打電話給你,每一個結果你都可以在覆蓋方法onRequestPermissionsResult這一個。

三江源

希望這將幫助你(Y)。