Android Marshmallow运行时权限无法正常运行

时间:2016-03-14 23:19:03

标签: java android android-6.0-marshmallow

我正在尝试在我的应用中为Android实现运行时权限。但是,Android Studio似乎没有正确编制索引。以下代码来自Google的示例。

 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.database.Cursor;
 import android.os.Bundle;
 import android.provider.MediaStore;
 import android.support.v4.app.ActivityCompat;
 import android.support.v4.content.ContextCompat;
 import android.support.v7.app.AppCompatActivity;

 import java.util.ArrayList;
 import java.util.List;

 /**
  * Created by Taylor on 3/14/16.
  */
 public class TCMediaPicker extends AppCompatActivity {

@Override public void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    Cursor cursor;
    setContentView(R.layout.mediapicker);

    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

    String[] projection = {
            MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Media.DISPLAY_NAME,
            MediaStore.Audio.Media.DURATION
    };

    cursor = this.getContentResolver().query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            projection,
            selection,
            null,
            null);

    List<String> songs = new ArrayList<String>();
    if (cursor!=null)
    while(cursor.moveToNext()) {
        songs.add(cursor.getString(0) + "||"
                + cursor.getString(1) + "||"
                + cursor.getString(2) + "||"
                + cursor.getString(3) + "||"
                + cursor.getString(4) + "||"
                + cursor.getString(5));
    }
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
}

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {

        // Show an expanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
     }
 }

它显示无法解析符号checkSelfPermission和未知类PackageManager.PERMISSION_GRANTED且无法解析符号READ_CONTACTS。

这是build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

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


 dependencies {
compile fileTree(dir: 'lib', include: ['*.jar'])
compile "com.google.android.gms:play-services:8.3.0"
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:support-v4:23.2.1'
}

3 个答案:

答案 0 :(得分:2)

确保您还在AndroidManifest中指定了所有权限。 别忘了导入

import android.content.pm.PackageManager;

在你的活动中。

答案 1 :(得分:1)

首先,您的权限检查不在功能块内。如果修复有效,太棒了! 如果没有,您是否可以检查用优质旧版AppCompatActivity替换Activity是否有效?我最近做了一些非常相似的事情,基于粗略的一瞥,你似乎已经做好了事情。

答案 2 :(得分:1)

使用

android.Manifest.permission.READ_CONTACTS

而不是

Manifest.permission.READ_CONTACTS 

或使用以下代码

getPermissionToReadContacts();



private static final int READ_CONTACTS_PERMISSIONS_REQUEST = 1;
public void getPermissionToReadContacts() {
     if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED){
        requestPermissions(new String[]{ android.Manifest.permission.READ_CONTACTS},READ_CONTACTS_PERMISSIONS_REQUEST);
    }
}

// Callback with the request from calling requestPermissions()
@Override
public void onRequestPermissionsResult(int requestCode,
                                       @NonNull String permissions[],
                                       @NonNull int[] grantResults) {
    // Make sure it's our original READ_CONTACTS request
    if (requestCode == READ_CONTACTS_PERMISSIONS_REQUEST) {
        if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            functionToGetContacts(); //change it to your function
        } else {
           //Read Contacts permission denied
        }
    } else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}