我正在尝试使用AsyncTask
类获取用户联系人。这是我为此编写的代码,但这不起作用:
protected Void doInBackground(Void... params) {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
System.out.println("name : " + name + ", ID : " + id);
// Result=Result+ "Name: "+name;
// get the phone number
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
String phone = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
publishProgress("Name:" + name + ",Number:" + phone
+ "\n");
}
pCur.close();
}
}
}
return null;
}
这是错误日志:
03-12 08:57:40.281: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:40.291: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:40.511: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:40.511: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:41.201: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:41.201: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:41.221: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:41.281: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:41.391: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:41.401: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:41.411: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:41.541: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:41.541: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:41.551: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:41.561: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:41.561: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:41.571: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:41.611: D/gralloc_goldfish(862): Emulator without GPU emulation detected.
03-12 08:57:41.691: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:41.781: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:42.571: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:42.581: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:42.581: W/dalvikvm(862): threadid=11: thread exiting with uncaught exception (group=0x40a70930)
03-12 08:57:42.631: E/AndroidRuntime(862): FATAL EXCEPTION: AsyncTask #1
03-12 08:57:42.631: E/AndroidRuntime(862): java.lang.RuntimeException: An error occured while executing doInBackground()
03-12 08:57:42.631: E/AndroidRuntime(862): at android.os.AsyncTask$3.done(AsyncTask.java:299)
03-12 08:57:42.631: E/AndroidRuntime(862): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
03-12 08:57:42.631: E/AndroidRuntime(862): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
03-12 08:57:42.631: E/AndroidRuntime(862): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
03-12 08:57:42.631: E/AndroidRuntime(862): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-12 08:57:42.631: E/AndroidRuntime(862): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-12 08:57:42.631: E/AndroidRuntime(862): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-12 08:57:42.631: E/AndroidRuntime(862): at java.lang.Thread.run(Thread.java:856)
03-12 08:57:42.631: E/AndroidRuntime(862): Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts from pid=862, uid=10058 requires android.permission.READ_CONTACTS, or grantUriPermission()
03-12 08:57:42.631: E/AndroidRuntime(862): at android.os.Parcel.readException(Parcel.java:1425)
03-12 08:57:42.631: E/AndroidRuntime(862): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
03-12 08:57:42.631: E/AndroidRuntime(862): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
03-12 08:57:42.631: E/AndroidRuntime(862): at android.content.ContentProviderProxy.query(ContentProviderNative.java:366)
03-12 08:57:42.631: E/AndroidRuntime(862): at android.content.ContentResolver.query(ContentResolver.java:372)
03-12 08:57:42.631: E/AndroidRuntime(862): at android.content.ContentResolver.query(ContentResolver.java:315)
03-12 08:57:42.631: E/AndroidRuntime(862): at in.wptrafficanalyzer.asynctaskdemo.MainActivity$CountDownTask.doInBackground(MainActivity.java:45)
03-12 08:57:42.631: E/AndroidRuntime(862): at in.wptrafficanalyzer.asynctaskdemo.MainActivity$CountDownTask.doInBackground(MainActivity.java:1)
03-12 08:57:42.631: E/AndroidRuntime(862): at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-12 08:57:42.631: E/AndroidRuntime(862): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-12 08:57:42.631: E/AndroidRuntime(862): ... 4 more
03-12 08:57:43.121: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:43.201: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:46.261: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:46.301: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:47.161: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:47.161: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:47.171: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:47.281: W/Trace(862): Unexpected value from nativeGetEnabledTags: 0
03-12 08:57:47.281: I/Choreographer(862): Skipped 72 frames! The application may be doing too much work on its main thread.
03-12 08:57:56.445: I/Process(862): Sending signal. PID: 862 SIG: 9