Permission.READ_CONTACTS 似乎不起作用
我正在开发一个简单的应用程序,可以浏览用户的联系人。不幸的是,我不断收到以下错误:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
应包含在
元素中。请参阅清单文件的结构。因此,尝试将其放入
或
中是行不通的。绝望之举:尝试在
之前移动另外,如果您可以在没有其他权限的情况下进行测试,请将其删除。
编辑:您能否检查一下这是否是清单文件中的第一行:
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>
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:
以上都没有帮助。解决方案非常简单。
您将需要运行时权限请求。
无论是否在清单中添加许可,您都需要即时向用户请求该许可。
审核通过后您才可以查询联系人的电话/姓名等信息。
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.
only then after the approval you will be able to query the contacts phone number/name etc.
我完全陷入困境,直到我读到 这篇文章介绍了从 SDK 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:
I opened up my
gradle.build
file and changedtargetSdkVersion 23
totargetSdkVersion 22
, and now everything works great! I'll eventually find time to build in the new permissions system and switch back totargetSdkVersion 23
, which is probably the more correct answer. This is a temporary shortcut.在你的 on create 方法中尝试这个
Try this in your on create method
我遇到了同样的问题,我阅读的解决方案都无法帮助解决该问题,直到我添加了 WRITE_EXTERNAL_STORAGE 权限和 READ_CONTACTS 权限。
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 theREAD_CONTACTS
permission.我今天遇到了这个问题,上述修复方法均不适用于我。
这个问题最终与我的 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 thePermissions
tab weren't the same. I had theREAD_CONTACTS
permission in mymanifest.xml
, butREAD_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!
使用运行时权限为我工作来访问联系人
Worked for me using run time permission to access contacts