2015-11-05 36 views
3

我發展我的項目在SDK版本23,其中的應用權限被新引進的。 在他們使用下面的代碼來讀取手機狀態許可的一些準則被授予或不Android的檢查許可

if (ContextCompat.checkSelfPermission(serviceContext, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) { 
    //Read Phone state 
    }else{ 
} 

但我直接訪問checkSelfPermission像下面

if(serviceContext.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) { 
     //Read Phone state 
    }else{ 
} 

它的正常工作。 我的問題是什麼是上面這些代碼有什麼區別?。其中是檢查授予或不許可的正確方法?

+0

入住這一點 - http://stackoverflow.com/a/7203752/3235048 –

回答

9

我的問題是這些代碼之間的區別是什麼?

無,在API 23(+)設備。

設備上運行Android的舊版本,但是,將通過當您嘗試直接調用context.checkSelfPermission()產生錯誤。此方法直到API 23纔可用。

ContextCompat提供向後兼容方式運行在較舊的API checkSelfPermission()了。如果你看看實現,你會發現它只需將調用checkPermission()委託給應用程序自己的進程參數即可完成。 checkPermission()自首次API發佈以來一直可用,因此可以全面開展工作。

public static int checkSelfPermission(@NonNull Context context, @NonNull String permission) { 
    if (permission == null) { 
     throw new IllegalArgumentException("permission is null"); 
    } 

    return context.checkPermission(permission, android.os.Process.myPid(), Process.myUid()); 
} 

這是檢查權限授予或不正確的方法是什麼?

因此,要回答這個問題:如果你是運行Android「棉花糖」 6.0及更高版本,那麼你可以使用兩種方法配套設備。然而,由於它更可能你也想支持一些較舊的Android版本,使用ContextCompat

+0

謝謝。我會用它:) –

4

official and recent way編碼中的所有設備配套使用下面的代碼片段

請求的權限,你需要

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

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

      // 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(thisActivity, 
        new String[]{Manifest.permission.READ_CONTACTS}, 
        MY_PERMISSIONS_REQUEST_READ_CONTACTS); 

      // 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_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 
    } 
} 
+0

感謝@Anoop。我得到了答案。 –

0

另一種解決方案:

//Requesting permission 
private void requestStoragePermission(){ 

    if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_EXTERNAL_STORAGE)){ 
     //If the user has denied the permission previously your code will come to this block 
     //Here you can explain why you need this permission 
     //Explain here why you need this permission 
    } 

    //And finally ask for the permission 
    ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},STORAGE_PERMISSION_CODE); 
} 

//This method will be called when the user will tap on allow or deny 
@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 

    //Checking the request code of our request 
    if(requestCode == STORAGE_PERMISSION_CODE){ 

     //If permission is granted 
     if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ 

      //Displaying a toast 
      Toast.makeText(this,"Permission granted now you can read the storage",Toast.LENGTH_LONG).show(); 
     }else{ 
      //Displaying another toast if permission is not granted 
      Toast.makeText(this,"Oops you just denied the permission",Toast.LENGTH_LONG).show(); 
     } 
    } 
}