android中获得某一个应用程序用到的权限

来源:互联网 发布:推广软件哪个好 编辑:程序博客网 时间:2024/05/21 21:01

以下是android中setting部分源代码

Security permissions sectionLinearLayout permsView = (LinearLayout) findViewById(R.id.permissions_section);AppSecurityPermissions asp = new AppSecurityPermissions(this,packageName);if (asp.getPermissionCount() > 0) {permsView.setVisibility(View.VISIBLE);// Make the security sections header visibleLinearLayout securityList = (LinearLayout) permsView.findViewById(R.id.security_settings_list);securityList.removeAllViews();securityList.addView(asp.getPermissionsView());} else {permsView.setVisibility(View.GONE);}

因为android中没有公开AppSecurityPermissions这个类,所以我们得通过反射来获得对应类的实例,然后调用其中的方法。

public class PrivilegeActivity extends Activity {@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);                try {        /**         * 获得类的字节码文件         */Class clazz = getClass().getClassLoader().loadClass("android.widget.AppSecurityPermissions");/** * 获得指定的构造器 */Constructor constructor = clazz.getConstructor(new Class[]{Context.class,String.class});/** * 创建实例 */Object obj = constructor.newInstance(this,"com.android.phone");/** * 获得对应的方法 */Method method = clazz.getDeclaredMethod("getPermissionsView", new Class[]{});/** * 调用方法并获得返回值 */View view = (View) method.invoke(obj, new Class[]{});setContentView(view);        } catch (Exception e) {e.printStackTrace();}            }}
运行效果


0 0
原创粉丝点击