2015-11-05 48 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(); 
     } 
    } 
}