java 尝试访问 Android 中的联系人时的权限拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30293479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:50:09  来源:igfitidea点击:

Permission Denial when trying to access contacts in Android

javaandroidpermissionscontacts

提问by Brady131313

I am trying to access the contacts on the device and later display them in a list, however when I try to access them with a cursor, I get an error that I'm missing permissions for READ_CONTACTS but I have already gave permissions in my AndroidManifest.xml

我正在尝试访问设备上的联系人,然后将它们显示在列表中,但是当我尝试使用光标访问它们时,出现错误,提示我缺少 READ_CONTACTS 的权限,但我已经在我的 AndroidManifest 中授予了权限.xml

Code where I access Contacts:

我访问联系人的代码:

public void setupCursor() {
    Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

    while (cursor.moveToNext()) {
        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        Toast.makeText(this, name + ": " + number, Toast.LENGTH_SHORT).show();
    }
    cursor.close();
}

AndroidManifest.xml

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.github.brady131313.textback" >

<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="21"/>

<uses-permission android:name="ANDROID.PERMISSION.READ_CONTACTS"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".ActivityMainSettings"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".ActivityContactSelect"
        android:label="Select Contacts">
    </activity>

</application>


</manifest>

Error Log

错误日志

05-17 17:12:41.544  20202-20202/io.github.brady131313.textback E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: io.github.brady131313.textback, PID: 20202
    java.lang.RuntimeException: Unable to start activity ComponentInfo{io.github.brady131313.textback/io.github.brady131313.textback.ActivityContactSelect}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{14c069a9 20202:io.github.brady131313.textback/u0a90} (pid=20202, uid=10090) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2661)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
            at android.app.ActivityThread.access0(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5835)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
     Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{14c069a9 20202:io.github.brady131313.textback/u0a90} (pid=20202, uid=10090) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
            at android.os.Parcel.readException(Parcel.java:1540)
            at android.os.Parcel.readException(Parcel.java:1493)
            at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3582)
            at android.app.ActivityThread.acquireProvider(ActivityThread.java:5081)
            at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2926)
            at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1452)
            at android.content.ContentResolver.query(ContentResolver.java:468)
            at android.content.ContentResolver.query(ContentResolver.java:428)
            at io.github.brady131313.textback.ActivityContactSelect.setupCursor(ActivityContactSelect.java:46)
            at io.github.brady131313.textback.ActivityContactSelect.onCreate(ActivityContactSelect.java:32)
            at android.app.Activity.performCreate(Activity.java:6221)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2614)
????????????at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
????????????at android.app.ActivityThread.access0(ActivityThread.java:172)
????????????at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
????????????at android.os.Handler.dispatchMessage(Handler.java:102)
????????????at android.os.Looper.loop(Looper.java:145)
????????????at android.app.ActivityThread.main(ActivityThread.java:5835)
????????????at java.lang.reflect.Method.invoke(Native Method)
????????????at java.lang.reflect.Method.invoke(Method.java:372)
????????????at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
????????????at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)

Any help will be appreciated!

任何帮助将不胜感激!

采纳答案by Lucas Crawford

Add the uses-permission outside the application tag and make the android.permission lowercase. that should fix it as agreed below in comments

在应用程序标签外添加 uses-permission 并使 android.permission 小写​​。这应该按照下面评论中的约定进行修复

回答by ThetNaing Mizo

Since the new runtime permission modelis introduced in Android 6.0,

由于在 Android 6.0 中引入了新的运行时权限模型

Hope this code will help if you want to use runtime permission model. You can also see detail implementation and get source code from here.

如果您想使用运行时权限模型,希望此代码会有所帮助。您还可以从此处查看详细实现并获取源代码。

import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Build;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {
    // The ListView
    private ListView lstNames;

    // Request code for READ_CONTACTS. It can be any number > 0.
    private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Find the list view
        this.lstNames = (ListView) findViewById(R.id.lstNames);

        // Read and show the contacts
        showContacts();
    }

    /**
     * Show the contacts in the ListView.
     */
    private void showContacts() {
        // Check the SDK version and whether the permission is already granted or not.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, PERMISSIONS_REQUEST_READ_CONTACTS);
            //After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method
        } else {
            // Android version is lesser than 6.0 or the permission is already granted.
            List<String> contacts = getContactNames();
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contacts);
            lstNames.setAdapter(adapter);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults) {
        if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission is granted
                showContacts();
            } else {
                Toast.makeText(this, "Until you grant the permission, we canot display the names", Toast.LENGTH_SHORT).show();
            }
        }
    }

    /**
     * Read the name of all the contacts.
     *
     * @return a list of names.
     */
    private List<String> getContactNames() {
        List<String> contacts = new ArrayList<>();
        // Get the ContentResolver
        ContentResolver cr = getContentResolver();
        // Get the Cursor of all the contacts
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

        // Move the cursor to first. Also check whether the cursor is empty or not.
        if (cursor.moveToFirst()) {
            // Iterate through the cursor
            do {
                // Get the contacts name
                String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                contacts.add(name);
            } while (cursor.moveToNext());
        }
        // Close the curosor
        cursor.close();

        return contacts;
    }
}

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by Zhangjun Xu

i had same situation like what you have, basically you only need change "android.permission.READ_CONTACTS" to lowercase for the first half you the permission.

我有和你一样的情况,基本上你只需要在前半部分将“android.permission.READ_CONTACTS”更改为小写即可。