java.lang.SecurityException 尝试从 Android 联系人 URI 读取
我尝试从 ContactsContract
URI 读取联系人姓名、电话号码和电子邮件,但在尝试运行该程序时收到 SecurityException
。我已在 AndroidManifest.xml
文件中设置权限:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.smumn.cs394"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
**<uses-permission android:name="android.pemission.READ_CONTACTS"/>**
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ReadPhoneNumbers"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>`
以下是应用程序代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_list);
ContentResolver resolver = getContentResolver();
Cursor c = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
//[...] Work through data here`
我在最后一行 (resolver.query()
) 收到安全异常:
`03-08 07:41:40.812: ERROR/AndroidRuntime(416): FATAL EXCEPTION: main
03-08 07:41:40.812: ERROR/AndroidRuntime(416): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.smumn.cs394/edu.smumn.cs394.ReadPhoneNumbers}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts from pid=416, uid=10037 requires android.permission.READ_CONTACTS
[...]
03-08 07:41:40.812: ERROR/AndroidRuntime(416): Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts from pid=416, uid=10037 requires android.permission.READ_CONTACTS
[...]
03-08 07:41:40.812: ERROR/AndroidRuntime(416): at edu.smumn.cs394.ReadPhoneNumbers.onCreate(ReadPhoneNumbers.java:30)
[...]`
我一定错过了一些东西,但我不知道是什么。
I am trying to read Contact names, phone #'s, and emails from the ContactsContract
URI, and I am getting a SecurityException
when I try to run the program. I have set the permission in the AndroidManifest.xml
file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.smumn.cs394"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
**<uses-permission android:name="android.pemission.READ_CONTACTS"/>**
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ReadPhoneNumbers"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>`
The following is the application code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_list);
ContentResolver resolver = getContentResolver();
Cursor c = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
//[...] Work through data here`
I get a security exception on the last line (resolver.query()
):
`03-08 07:41:40.812: ERROR/AndroidRuntime(416): FATAL EXCEPTION: main
03-08 07:41:40.812: ERROR/AndroidRuntime(416): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.smumn.cs394/edu.smumn.cs394.ReadPhoneNumbers}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts from pid=416, uid=10037 requires android.permission.READ_CONTACTS
[...]
03-08 07:41:40.812: ERROR/AndroidRuntime(416): Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts from pid=416, uid=10037 requires android.permission.READ_CONTACTS
[...]
03-08 07:41:40.812: ERROR/AndroidRuntime(416): at edu.smumn.cs394.ReadPhoneNumbers.onCreate(ReadPhoneNumbers.java:30)
[...]`
I must be missing something, but I can't figure out what.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在运行时请求权限
从 Android 6.0(API 级别 23)开始,用户在应用运行时(而不是安装应用时)向应用授予权限。
如果您需要添加的权限未在正常权限下列出,则您需要处理“运行时权限”。运行时权限是应用程序运行时根据需要请求的权限。这些权限将向用户显示一个对话框,类似于以下对话框:
添加“运行时权限”的第一步是将其添加到 AndroidManifest:
接下来,您需要启动权限请求并处理结果。下面的代码展示了如何在 Activity 的上下文中执行此操作,但这也可以在 Fragment 中执行。
Requesting Permissions at Run Time
Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.
If the permission you need to add isn't listed under the normal permissions, you'll need to deal with "Runtime Permissions". Runtime permissions are permissions that are requested as they are needed while the app is running. These permissions will show a dialog to the user, similar to the following one:
The first step when adding a "Runtime Permission" is to add it to the AndroidManifest:
Next, you'll need to initiate the permission request and handle the result. The following code shows how to do this in the context of an Activity, but this is also possible from within a Fragment.
确保将其添加到应用程序标记之外。在 Ubuntu 上使用 Eclipse 为 2.3.3 目标平台进行开发时,我在日志文件中遇到权限失败,这表明我在处理类似的事情时需要这一行。直到我将 *uses-permission...READ_CONTACTS* 行移到应用程序标记之外,事情才起作用。
Make sure you add it outside of the application tag. While developing for a target platform of 2.3.3 using Eclipse on Ubuntu, I had permission failures in the log file that indicated I needed this exact line while working on something similar. It wasn't until I moved the *uses-permission...READ_CONTACTS* line to outside the application tag that things worked.
你好史蒂文调试日志跟踪告诉你你需要
...需要 android.permission.READ_CONTACTS
所以只需通过编辑 Manifest.xml 尝试一些操作,例如添加另一个权限,看看它是否未正确读取。
并检查这一行没有 **
dan
Hello Steven the debug log trace tells you that you need
... requires android.permission.READ_CONTACTS
so just try something by editing the Manifest.xml like adding another permission, let see if its not correctly readed.
and check this line without **
dan
对于 api 23,权限
不起作用,请将模拟器中的 api 级别更改为 api 22(lollipop) 或更低with the api 23, permission
<uses-permission android:name="android.pemission.READ_CONTACTS"/>
dont work, change the api level in the emulator for api 22(lollipop) or lower如果设备运行的是 Android 6.0 或更高版本,并且应用程序的目标 SDK 为 23 或更高版本:应用程序必须在清单中列出权限,并且必须在应用程序运行时请求所需的每个危险权限。用户可以授予或拒绝每项权限,即使用户拒绝权限请求,应用程序也可以继续以有限的功能运行。
示例:
http://developer.android.com/training/permissions/requesting.html
If the device is running Android 6.0 or higher, and your app's target SDK is 23 or higher: The app has to list the permissions in the manifest, and it must request each dangerous permission it needs while the app is running. The user can grant or deny each permission, and the app can continue to run with limited capabilities even if the user denies a permission request.
EXAMPLE:
http://developer.android.com/training/permissions/requesting.html