Android权限,权限分组官方描述:
permission
<permission android:description="string resource"
android:icon="drawable resource"
android:label="string resource"
android:name="string"
android:permissionGroup="string"
android:protectionLevel=["normal" | "dangerous" |
"signature" | "signatureOrSystem"] />
DESCRIPTION:
Declares a security permission that can be used to limit access to specific components or features of this or other applications.
声明一个安全权限对于 特定的组件 或者 当前或者其他应用程序的特性 的有限的访问权限
permission-group
SYNTAX:
<permission-group android:description="string resource"
android:icon="drawable resource"
android:label="string resource"
android:name="string" />
DESCRIPTION:
Declares a name for a logical grouping of related permissions. Individual permission join the group through the permission Group attribute of the <permission> element. Members of a group are presented together in the user interface.
Note that this element does not declare a permission itself, only a category in which permissions can be placed. See the <permission> element for element for information on declaring permissions and assigning them to groups.
声明一个相关权限的逻辑分组。单独的权限通过权限定义中的权限分组属性,声明响应的分组。权限组的成员是展现在同一用户接口中的。
注意:permission-group并没有生命自己的任何权限,只是一个权限的目录。查看permission元素相关的信息查找一个权限所属的分组。
Android 中权限的分组情况如下表:
!!!!如果获得Permission Group的权限,则分组中所有权限都会获取到。
WRITE_CONTACTS权限被授予以后,应用将会获得READ_CONTACTS和GET_ACCOUNTS的权限。!!!!
在AndroidManifest.xml中声明如下:
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
由上面的权限分组可知:
WRITE_CONTACTS权限和READ_CONTACTS权限同属于CONTACTS组,因此,在程序申请权限的时候。
由于Android6.0中引入的Runtime Permission中,在申请权限的时候只需要申请这两者中的一个,就可以获得统一分组中的其他权限。
我写的测试代码如下:
public class MainActivity extends AppCompatActivity {
final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124;
private Button judgePermission;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
judgePermission = (Button)findViewById(R.id.judge_permission);
judgePermission.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int hasSMSSendPermission = checkSelfPermission(Manifest.permission.WRITE_CONTACTS);
if(hasSMSSendPermission==PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "当前应用已经有写通信录的权限了", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "当前应用并没有写通信录的权限", Toast.LENGTH_SHORT).show();
}
}
});
int hasSMSPermission = checkSelfPermission(Manifest.permission.READ_SMS);
int hasContactPermission = checkSelfPermission(Manifest.permission.READ_CONTACTS);
if(hasSMSPermission!= PackageManager.PERMISSION_GRANTED || hasContactPermission!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_SMS, Manifest.permission.READ_CONTACTS}, REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS:
Map<String, Integer> perms = new HashMap<String, Integer>();
// Initial
perms.put(Manifest.permission.READ_SMS, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_CONTACTS, PackageManager.PERMISSION_GRANTED);
// Fill with results
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for ACCESS_FINE_LOCATION
if (perms.get(Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
// All Permissions Granted
Toast.makeText(MainActivity.this, "All Permission is Allow", Toast.LENGTH_SHORT).show();
} else {
// Permission Denied
Toast.makeText(MainActivity.this, "Some Permission is Denied", Toast.LENGTH_SHORT)
.show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}