你可以在manifest文件中申明你需要的权限。权限可以分成normal 和 dangerous。app只有在直接操作的时候需要权限,如果是通过其他有权限的application来完成操作,则不需要权限。
可以通过如下uses-permission在manifest文件要求权限,
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.snazzyapp">
<uses-permission android:name="android.permission.SEND_SMS"/>
<application ...>
...
</application>
</manifest>
可以调用ContextCompatible.checkSelfPermission()来检查你是否有权限.
// Assume thisActivity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.WRITE_CALENDAR);
permissionCheck ==PackageManager.PERMISSION_GRANTED 表示有权限,PackageManager.PERMISSION_DENIED 表示没权限.
当user 拒绝赋予权限是,可以调用shouldShowRequestPermissionRationale来想向user解释,为什么要获取这个权限。如下所示:
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
//用toast解释为什么要这个权限
/ Toast.makeText(this, "I really need it, dude!", Toast.LENGTH_SHORT).show();
} 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.
}
}
public boolean shouldShowRequestPermissionRationale(String permissionName,
3809 String packageName, int userId) {
3810 if (UserHandle.getCallingUserId() != userId) {
3811 mContext.enforceCallingPermission(
3812 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
3813 "canShowRequestPermissionRationale for user " + userId);
3814 }
3815
3816 final int uid = getPackageUid(packageName, userId);
3817 if (UserHandle.getAppId(getCallingUid()) != UserHandle.getAppId(uid)) {
3818 return false;
3819 }
3820
3821 if (checkPermission(permissionName, packageName, userId)
3822 == PackageManager.PERMISSION_GRANTED) {
3823 return false;
3824 }
3825
3826 final int flags;
3827
3828 final long identity = Binder.clearCallingIdentity();
3829 try {
3830 flags = getPermissionFlags(permissionName,
3831 packageName, userId);
3832 } finally {
3833 Binder.restoreCallingIdentity(identity);
3834 }
3835
3836 final int fixedFlags = PackageManager.FLAG_PERMISSION_SYSTEM_FIXED
3837 | PackageManager.FLAG_PERMISSION_POLICY_FIXED
3838 | PackageManager.FLAG_PERMISSION_USER_FIXED;
3839
3840 if ((flags & fixedFlags) != 0) {
3841 return false;
3842 }
3843
3844 return (flags & PackageManager.FLAG_PERMISSION_USER_SET) != 0;
3845 }
当app向系统要求权限是,系统会弹出一个对话框给用户选择是同意赋予权限还是不同意。你需要在你的code中重写onRequestPermission,以获取用用户是同意还是拒绝。如下所示:
@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
}
}
可以通过adb tool在command line上关系permission
列出权限
$ adb shell pm list permissions -d -g
同意或者取消一个或多个权限
$ adb shell pm [grant|revoke] <permission-name> ...