在 Android 3.0 上检索电话联系人

Posted

技术标签:

【中文标题】在 Android 3.0 上检索电话联系人【英文标题】:Retrieving phone contacts on Android 3.0 【发布时间】:2011-08-24 18:40:40 【问题描述】:

您好,我使用以下代码检索设备的电话联系人。

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts;
import android.provider.Contacts.People;

public class TestContacts extends Activity 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(People.CONTENT_URI, 
            null, null, null, null);
        if (cur.getCount() > 0) 
         while (cur.moveToNext()) 
             String id = cur.getString(cur.getColumnIndex(People._ID));
             String name = cur.getString(cur.getColumnIndex(People.DISPLAY_NAME));
         

        
        if (Integer.parseInt(cur.getString(
                cur.getColumnIndex(People.PRIMARY_PHONE_ID))) > 0) ---Error at this line---
 
            Cursor pCur = cr.query(
                    Contacts.Phones.CONTENT_URI, 
                    null, 
                    Contacts.Phones.PERSON_ID +" = ?", 
                    new String[]id, null);
            int i=0;
            int pCount = pCur.getCount();
            String[] phoneNum = new String[pCount];
            String[] phoneType = new String[pCount];
            while (pCur.moveToNext()) 
                phoneNum[i] = pCur.getString(
                                   pCur.getColumnIndex(Contacts.Phones.NUMBER));
                phoneType[i] = pCur.getString(
                                   pCur.getColumnIndex(Contacts.Phones.TYPE));
                i++;
             
        
    


但我在粗线处得到 CursorIndexOutOfBound 异常。异常如下

05-13 14:27:26.910: ERROR/AndroidRuntime(2432): Uncaught handler: thread main exiting due to uncaught exception
05-13 14:27:26.910: ERROR/AndroidRuntime(2432): **android.database.CursorIndexOutOfBoundsException: Index 13 requested, with a size of 13**
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:172)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.database.CursorWrapper.getString(CursorWrapper.java:135)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at com.fsp.mus.UTell_Sms$2.onClick(UTell_Sms.java:101)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.view.View.performClick(View.java:2347)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.view.View.onTouchEvent(View.java:4185)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.view.View.dispatchTouchEvent(View.java:3717)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.app.Activity.dispatchTouchEvent(Activity.java:1987)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1659)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.os.Looper.loop(Looper.java:123)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.app.ActivityThread.main(ActivityThread.java:4203)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at java.lang.reflect.Method.invokeNative(Native Method)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at java.lang.reflect.Method.invoke(Method.java:521)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

【参考方案1】:

您指的是已弃用的类联系人、电话

运行下面的代码,你的问题就解决了……

package com.test;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;

public class TestContacts extends Activity 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        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 (("1")
                        .equals(cur
                                .getString(cur
                                        .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))) 
                    Cursor pCur = cr.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                    + " = ?", new String[]  id , null);
                    int i = 0;
                    int pCount = pCur.getCount();
                    String[] phoneNum = new String[pCount];
                    String[] phoneType = new String[pCount];
                    while (pCur.moveToNext()) 
                        phoneNum[i] = pCur
                                .getString(pCur
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        phoneType[i] = pCur
                                .getString(pCur
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

                        i++;
                    
                

            

        

    

【讨论】:

谢谢.. 我试过你的代码它显示错误,无论在哪里使用 ContactsContract。错误是 ContactsContract 无法解析为变量。 您使用的是哪个 API 版本?导入应该是“import android.provider.ContactsContract;”请查看导入中的代码,因为我的帖子显示了“if”语句中的代码。 +1,即使在开发人员指南中也提到了已弃用的类。去developer.android.com/guide/topics/providers/…看看吧。 你忘了关闭游标

以上是关于在 Android 3.0 上检索电话联系人的主要内容,如果未能解决你的问题,请参考以下文章

Android 3.0 - 如何通过 ContactsContract 检索所有联系人

在 Android 上检索联系人图像

从 Android 中的 URI 检索联系人电话号码

仅从 android 中检索电话号码包含 123 的联系人

从android中的电话簿中检索whatsapp联系人

从 Android 联系人中获取电子邮件和电话