Permission.READ_CONTACTS 似乎不起作用

发布于 2024-12-15 00:30:17 字数 2155 浏览 3 评论 0原文

我正在开发一个简单的应用程序,可以浏览用户的联系人。不幸的是,我不断收到以下错误:

java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.HtcContactsProvider2 uri content://com.android.contacts/contacts from pid=27455, uid=10171 requires android.permission.READ_CONTACTS

我的清单文件如下所示:

<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.helloMaps"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />   
    <application android:icon="@drawable/icon" 
        android:label="@string/app_name" 
        android:debuggable="true">
        <uses-library android:name="com.google.android.maps" />
        <activity android:name=".MapsActivity"
                  android:label="@string/app_name"
                  android:theme="@android:style/Theme.NoTitleBar">            
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
    </application>
</manifest>

我试图通过以下方式查看我的联系人:

 Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);  

我尝试添加 android:permission="android.permission.READ_CONTACTS",但两者都不起作用。

我没有选择了,有人知道我在这里缺少什么吗?

I'm working on a simple app that browses through the user's contacts. Unfortunately I keep getting the following error:

java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.HtcContactsProvider2 uri content://com.android.contacts/contacts from pid=27455, uid=10171 requires android.permission.READ_CONTACTS

My manifest file looks like this:

<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.helloMaps"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />   
    <application android:icon="@drawable/icon" 
        android:label="@string/app_name" 
        android:debuggable="true">
        <uses-library android:name="com.google.android.maps" />
        <activity android:name=".MapsActivity"
                  android:label="@string/app_name"
                  android:theme="@android:style/Theme.NoTitleBar">            
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
    </application>
</manifest>

I'm trying to look at my contacts by:

 Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);  

I've tried to add <uses-permission android:name="android.permission.READ_CONTACTS" /> or android:permission="android.permission.READ_CONTACTS" to <application> or <activity>, but both didn't work.

I'm running out of options, does anybody know what I'm missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

花落人断肠 2024-12-22 00:30:17

应包含在 元素中。请参阅清单文件的结构。因此,尝试将其放入 中是行不通的。绝望之举:尝试在 之前移动

<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

另外,如果您可以在没有其他权限的情况下进行测试,请将其删除。

编辑:您能否检查一下这是否是清单文件中的第一行:

<?xml version="1.0" encoding="utf-8"?>

the <uses-permission> should be contained in the <manifest> element. See Structure of the Manifest File. So trying putting it into <application> or <activity>, won't work. Desperation move: try to move <uses-sdk> before <application>

<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

Also if you can test without the other permissions, remove them.

EDIT: Can you please check if this is your TOP line in your manifest file:

<?xml version="1.0" encoding="utf-8"?>
染火枫林 2024-12-22 00:30:17

以上都没有帮助。解决方案非常简单。
您将需要运行时权限请求。

无论是否在清单中添加许可,您都需要即时向用户请求该许可。

if( getApplicationContext().checkSelfPermission( Manifest.permission.READ_CONTACTS ) != PackageManager.PERMISSION_GRANTED )
     ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_CONTACTS}, resultValue);

审核通过后您才可以查询联系人的电话/姓名等信息。

None of the above helped. The solution is quite simple.
you'll need a runtime permission request.

with or without placing the permission in your manifest, You will need to request that permission from the User on-the-fly.

if( getApplicationContext().checkSelfPermission( Manifest.permission.READ_CONTACTS ) != PackageManager.PERMISSION_GRANTED )
     ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_CONTACTS}, resultValue);

only then after the approval you will be able to query the contacts phone number/name etc.

把回忆走一遍 2024-12-22 00:30:17

我完全陷入困境,直到我读到 这篇文章介绍了从 SDK 23 开始如何处理权限。关键提示:

如果应用程序的 targetSdkVersion 设置为小于 23。将假定应用程序尚未使用新的权限系统进行测试,并将切换到相同的旧行为:用户必须在安装时接受每个权限,并且他们将一旦安装即可获得所有授权!

我打开了 gradle.build 文件并将 targetSdkVersion 23 更改为 targetSdkVersion 22,现在一切正常!我最终会找到时间构建新的权限系统并切换回 targetSdkVersion 23,这可能是更正确的答案。这是临时的捷径。

I was totally stuck on this until I read this article about how permissions are handled starting with SDK 23. The critical hint:

If the application's targetSdkVersion is set to less than 23. It will be assumed that application is not tested with new permission system yet and will switch to the same old behavior: user has to accept every single permission at install time and they will be all granted once installed !

I opened up my gradle.build file and changed targetSdkVersion 23 to targetSdkVersion 22, and now everything works great! I'll eventually find time to build in the new permissions system and switch back to targetSdkVersion 23, which is probably the more correct answer. This is a temporary shortcut.

等待圉鍢 2024-12-22 00:30:17

在你的 on create 方法中尝试这个

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

Try this in your on create method

ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_CONTACTS},1);
盗梦空间 2024-12-22 00:30:17

我遇到了同样的问题,我阅读的解决方案都无法帮助解决该问题,直到我添加了 WRITE_EXTERNAL_STORAGE 权限和 READ_CONTACTS 权限。

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

I had the same problem and none of the solutions I read helped to resolve it until I added the WRITE_EXTERNAL_STORAGE permission along with the READ_CONTACTS permission.

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
暖伴 2024-12-22 00:30:17

我今天遇到了这个问题,上述修复方法均不适用于我。

这个问题最终与我的 Eclipse 有关。我在 manifest.xml 中编写的内容与 Permissions 选项卡中的内容并不相同。我在 manifest.xml 中拥有 READ_CONTACTS 权限,但 READ_PROFILE 位于我的权限选项卡下。

我知道这是一个旧线程,但希望如果有人遇到与我相同的问题,他们会偶然发现这一点。

谢谢!

I ran into this issue today and none of the above fixes worked for me.

What the issue ended up being had to do with my Eclipse. What I had written in the manifest.xml and what was in the Permissions tab weren't the same. I had the READ_CONTACTS permission in my manifest.xml, but READ_PROFILE was in it's place under my permission's tab.

I know this is an old thread, but hopefully if someone has the same issue I did they'll stumble across this.

Thanks!

狂之美人 2024-12-22 00:30:17

使用运行时权限为我工作来访问联系人

Worked for me using run time permission to access contacts

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文