Android Studio:ActivityCompat.requestPermissions不显示对话框

时间:2016-11-13 23:06:06

标签: android android-studio permissions

调用ActivityCompat.requestPermissions不会打开任何对话框。我已经在这里工作了几天,但未能找到任何有关此主题的答案。我听说过一些错误,但没有具体针对这种情况。

手机版Android版:6.0.1

在测试手机的应用程序设置中接受所有存储权限。应用程序已清除数据两次,以及已卸载和重新安装。

它正在进行requestPermissions()调用。

摇篮:

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion '25'

defaultConfig {
    applicationId "APPID"
    minSdkVersion 16
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.facebook.android:facebook-android-sdk:4.+'
compile 'com.android.support:appcompat-v7:23.4.0'
compile('com.affectiva.android:affdexsdk:3.1.2')

}

清单文件包括:

<?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="PACKAGE NAME">

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:replace="android:allowBackup,android:label">
    <activity
        android:name=".LoginActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name"/>
        <!--android:theme="@android:style/Theme.Translucent.NoTitleBar" -->

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id" />

    <activity
        android:name=".ProfileActivity"
        android:screenOrientation="portrait"></activity>
    <activity android:name=".QuestionOne"></activity>
</application>

最初在这里打电话:

    if(loggedIn && friendsNames.size() > 0) {

        try{

            verifyStoragePermissions(this);
        }
        catch(Exception e ){
            Log.e("LoginActv: Save Block", e.getMessage());
            e.getStackTrace();
        }

        Intent intent = new Intent(this, QuestionOne.class);
        intent.putStringArrayListExtra("names",friendsNames);
        intent.putStringArrayListExtra("urls", pictureURL);

        startActivity(intent);

当然还需要权限。请注意,&#39;权限!= 1&#39; vs&#39; permission!= PackageManager.PERMISSION_GRANTED&#39;因为否则,它不会遇到requestPermissions:

public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    Log.e("Permission value", permission + " ");
    if (permission != 1) {
        Log.e("Permission ask", "Asking, permission not granted");
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}

权限变量如下:

// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
};

最后覆盖onRequestPermissionsResult(),它甚至从未被调用过。

@Override
public void onRequestPermissionsResult(int requestCode,  String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);


    if (requestCode == 200) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            Log.e("PERResult", "Granted");
            resultExport file = new resultExport();
            file.setUserName(facebookHandler.getUserName());
            file.setUserID(facebookHandler.getUserID());
            file.addData("friendNames", friendsNames);
            saveObject(file, facebookHandler.getUserName());


        } else {
            Log.e("PERResult", "Denied");
            Toast.makeText(getApplicationContext(), "PERMISSION_DENIED", Toast.LENGTH_SHORT).show();
        }
    }
}

我完全不知道为什么一旦我请求权限就不会弹出对话框。真的很感激任何帮助。

编辑:

两者

int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

int permissionCheck = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

返回0,但尝试保存得到:

open failed: EACCES (Permission denied)

3 个答案:

答案 0 :(得分:1)

显然它不需要对话,因为权限存在。但是,它仅仅因为路径错误而拒绝了权限。

来自

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(Environment.getExternalStorageDirectory().getPath() + name + ".bin")));

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(Environment.getExternalStorageDirectory().getPath()+ "/" + name + ".bin")));

第一个示例抛出权限错误。

答案 1 :(得分:0)

看起来应该是这样的

if (permission != PackageManager.PERMISSION_GRANTED) {...}

因为1代表PackageManager.SIGNATURE_NEITHER_SIGNED

答案 2 :(得分:0)

我遇到了类似的问题,但找到了答案。

您必须检查merged manifest,以确保您的应用或库没有WRITE_EXTERNAL_STORAGE的{​​{1}}权限。

WRITE_EXTERNAL_STORAGE permission with android:maxSdkVersion 如果您发现android:maxSdkVersion, 将WRITE_EXTERNAL_STORAGE权限标签替换为:

android:maxSdkVersion

将解决问题。