0

我正在尝试使用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
4

2 回答 2

1

您需要在 Manifest 中添加以下权限:

<uses-permission android:name="android.permission.READ_CONTACTS" />

这清楚地显示在您的错误日志中......

除此之外,您的代码效率很低。可以通过少得多的查询来获得您想要的信息:

Cursor cursor = getContentResolver ().query (
        ContactsContract.Data.CONTENT_URI, 
        new String [] {
                ContactsContract.CommonDataKinds.Email.DATA,
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME},
                Data.MIMETYPE + " = ?",
                new String [] {ContactsContract.CommonDataKinds.Identity.CONTENT_ITEM_TYPE},
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
            );

if (cursor.isAfterLast()) {
    cursor.close ();
    return null;
}

while (cursor.moveToNext ()) {
    String email = cursor.getString (cursor.getColumnIndex (ContactsContract.Contacts._ID));
    String name = cursor.getString (cursor.getColumnIndex (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
}
于 2013-03-12T09:06:31.763 回答
1

魔术线是:

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()

在您的清单中添加:

<uses-permission android:name="android.permission.READ_CONTACTS" />
于 2013-03-12T09:09:51.953 回答