WEB开发网
开发学院手机开发Android 开发 查看Android应用所需权限(uses-permission) 阅读

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

 2013-10-04 13:45:48 来源:WEB开发网   
核心提示:MainActivity如下:package cc.testusespermission;import android.app.Activity;import android.content.pm.PackageInfo;import android.content.pm.PackageManager;import a

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
		}
		
		
	}

}

1 2  下一页

Tags:查看 Android 应用

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接