查看Android应用所需权限(uses-permission)

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 {

	@Override
	protected 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:

该示例本身是不需要任何权限的

你可能感兴趣的:(查看Android应用所需权限(uses-permission))