MainActivity如下:
package cc.testusespermission;import android.app.Activity;import android.content.pm.PackageInfo;import android.content.pm.PackageManager;import android.content.pm.PermissionGroupInfo;import android.content.pm.PermissionInfo;import android.os.Bundle;/** * Demo描述: * 查看Android應用要求的權限(uses-permission) * * 參考資料: * 1 http://blog.csdn.net/bage1988320/article/details/6740292 * 2 http://blog.csdn.net/hjd_love_zzt/article/details/12238301 * Thank you very much * * 備忘說明: * 在Demo中的代碼注釋是以android.permission.INTERNET許可權 * 為例子對各屬性進行了詳細說明 * */public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);getUsesPermission("cc.testusespermission");} private void getUsesPermission(String packageName){try {PackageManager packageManager=this.getPackageManager();PackageInfo packageInfo=packageManager.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);String [] usesPermissionsArray=packageInfo.requestedPermissions;for (int i = 0; i < usesPermissionsArray.length; i++) { //得到每個許可權的名字,如:android.permission.INTERNET String usesPermissionName=usesPermissionsArray[i]; System.out.println("usesPermissionName="+usesPermissionName); //通過usesPermissionName擷取該許可權的詳細資料 PermissionInfo permissionInfo=packageManager.getPermissionInfo(usesPermissionName, 0); //獲得該許可權屬於哪個許可權組,如:網路通訊 PermissionGroupInfo permissionGroupInfo = packageManager.getPermissionGroupInfo(permissionInfo.group, 0); System.out.println("permissionGroup=" + permissionGroupInfo.loadLabel(packageManager).toString()); //擷取該許可權的標籤資訊,比如:完全的網路存取權限 String permissionLabel=permissionInfo.loadLabel(packageManager).toString(); System.out.println("permissionLabel="+permissionLabel); //擷取該許可權的詳細描述資訊,比如:允許該應用建立網路通訊端和使用自訂網路通訊協定 //瀏覽器和其他某些應用提供了向互連網發送資料的途徑,因此應用無需該許可權即可向互連網發送資料. String permissionDescription=permissionInfo.loadDescription(packageManager).toString(); System.out.println("permissionDescription="+permissionDescription); System.out.println("===========================================");}} catch (Exception e) {// TODO: handle exception}}}
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查看Android應用要求的權限(uses-permission)" android:layout_centerInParent="true" /></RelativeLayout>
PS:
該樣本本身是不需要任何許可權的