packageInfo.requestedPermissions返回不正确的权限

时间:2019-01-30 09:06:01

标签: android

这是我的AndroidManifest.xml,此文件有8个权限。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.jiyuzhai.xxxx">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.READ_SETTINGS"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.android.vending.BILLING"/>
    <application
        android:name=".main.MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="HardcodedDebugMode"
        tools:replace="android:allowBackup">
        <activity
            android:name="com.jiyuzhai.xxx.main.MainActivity"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustResize"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.jiyuzhai.xxx.settings.SetttingsActivity"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustResize"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
        </activity>
        <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-2905597280622539~7328266239" />
    </application>
</manifest>

然后,我使用以下代码获取文件AndroidManifest.xml中的权限,

try {
    PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS);
    String[] permissions = packageInfo.requestedPermissions;
    if (permissions == null || permissions.length == 0) {
        throw new IllegalStateException("You did not register any permissions in the manifest.xml.");
    }
    return Collections.unmodifiableList(Arrays.asList(permissions));
} catch (PackageManager.NameNotFoundException e) {
    throw new AssertionError("Package name cannot be found.");
}

但是结果不正确,我获得了以下权限,android.permission.WRITE_EXTERNAL_STORAGE丢失了!!,为什么?

enter image description here

[编辑]添加android sdk版本和手机状态。

ext {
    compileSdkVersion = 28
    minSdkVersion = 17
    targetSdkVersion = 28
    buildToolsVersion = '28.0.3'
}

enter image description here

1 个答案:

答案 0 :(得分:-1)

我认为这个问题已经在这篇文章中得到解答:

Storage permission error in Marshmallow

public  boolean isStoragePermissionGranted() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v(TAG,"Permission is granted");
            return true;
        } else {

            Log.v(TAG,"Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            return false;
        }
    }
    else { //permission is automatically granted on sdk<23 upon installation
        Log.v(TAG,"Permission is granted");
        return true;
    }
}

权限结果回调:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
        Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
        //resume tasks needing this permission
    }
}