我正在尝试从 ContactsContract URI 读取联系人姓名、电话号码和电子邮件,并且在我尝试运行该程序时收到 SecurityException.我在 AndroidManifest.xml 文件中设置了权限:
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="schemas.android/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()):
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.
推荐答案确保将其添加到应用程序标记之外.在 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.
更多推荐
java.lang.SecurityException 尝试从 Android 联系人 URI 读取
发布评论