我正在尝试从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); //[...] 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)
[…]`
我一定是错过了一些东西,但是我无法弄明白什么.