Is there a way to check for manifest permission from code?

前端 未结 2 1421
一向
一向 2020-12-29 06:22

How do I check for a specific permission in the manifest.xml from code? I want to throw some exception if some permissions that are necessay for my application are missing.

2条回答
  •  时光说笑
    2020-12-29 07:03

    You can read the available tags at runtime using the following. Tested on older Android versions AND Android 6 and 7

    PackageManager pm = getPackageManager();
    try
    {
        PackageInfo packageInfo = pm.getPackageInfo(getPackageName(), PackageManager.GET_PERMISSIONS);
        String[] requestedPermissions = null;
        if (packageInfo != null) {
            requestedPermissions = packageInfo.requestedPermissions;
        }
    
        if (requestedPermissions != null && requestedPermissions.length > 0)
        {
            List requestedPermissionsList = Arrays.asList(requestedPermissions);
            ArrayList requestedPermissionsArrayList = new ArrayList();
            requestedPermissionsArrayList.addAll(requestedPermissionsList);
    
            Log.i(ExConsts.TAG, ""+requestedPermissionsArrayList);
        }
    }
    catch (PackageManager.NameNotFoundException e)
    {
        e.printStackTrace();
    }
    

提交回复
热议问题