在android应用程序中获取联系人

我正在关注这些链接以获取我的应用程序中的联系人

如何调用Android联系人列表?

http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/

我可以在手机上显示联系人列表,但是

-

  • 我想在每个联系人中添加一个复选框,以便用户可以选择多个联系人,并通过单击完成按钮,他应该能够获取他选择的所有联系人

  • 另外我想获取联系人的姓名以及联系人的电话号码,请参阅我的代码:

  •     if (resultCode == Activity.RESULT_OK) {
                        Uri contactData = data.getData();
                        Cursor c = managedQuery(contactData, null, null, null, null);
                        if (c.moveToFirst()) {
                            String name = c.getString(c
                                    .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                            String number = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                            Log.v("name", name +" "+number);
                            // TODO Whatever you want to do with the selected contact
                            // name.
                        }
                    }
    

    在日志猫它显示输出为:

    01-09 12:46:41.688: V/name(699): Xyz 1
    

    这是联系人的名称是xyz,它至少有一个与之相关的电话号码。请帮助我如何获取与该联系人关联的号码。

    编辑:

    如果我使用this( String number = c.getString(c.getColumnIndexOrThrow(People.NUMBER)); )在代码中,我得到以下异常:

    01-09 13:33:23.008: E/AndroidRuntime(786): FATAL EXCEPTION: main
    01-09 13:33:23.008: E/AndroidRuntime(786): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0r1-2C2E30/1 (has extras) }} to activity {sra.com/sra.com.ContactsDemo}: java.lang.IllegalArgumentException: column 'number' does not exist
    01-09 13:33:23.008: E/AndroidRuntime(786):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at android.app.ActivityThread.access$2800(ActivityThread.java:125)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at android.os.Handler.dispatchMessage(Handler.java:99)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at android.os.Looper.loop(Looper.java:123)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at android.app.ActivityThread.main(ActivityThread.java:4627)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at java.lang.reflect.Method.invokeNative(Native Method)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at java.lang.reflect.Method.invoke(Method.java:521)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at dalvik.system.NativeStart.main(Native Method)
    01-09 13:33:23.008: E/AndroidRuntime(786): Caused by: java.lang.IllegalArgumentException: column 'number' does not exist
    01-09 13:33:23.008: E/AndroidRuntime(786):  at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:99)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at sra.com.ContactsDemo.onActivityResult(ContactsDemo.java:49)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at android.app.Activity.dispatchActivityResult(Activity.java:3890)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
    01-09 13:33:23.008: E/AndroidRuntime(786):  ... 11 more
    

    通过查看答案,我认为你已经获得了如何获取联系人的答案,并且现在您希望获得所选联系人的活动。

    要获取特定于联系人姓名的联系人号码:

    ContentResolver contactResolver = getContentResolver(); 
    Cursor cursor = contactResolver.query(Phone.CONTENT_URI, null, Phone.DISPLAY_NAME + "=?", new String[]{contactName}, null);
    
    if(cursor.getCount() > 0){
        cursor.moveToFirst();
        do {
           String number = cursor.getString(mCursor.getColumnIndex(Phone.NUMBER));
        }while (cursor.moveToNext() ); 
    }
    

    注意:这里contactName是你想要联系号码的联系人姓名。

    我假设你已经在ListView中显示了复选框中的联系人,这里是你的解决方案来获取用户选择的联系人列表到你的活动中:

    1.使用startActivityForResult()开始您的联系活动。

    2.初始化联系活动中的ArrayList变量,将其contactArrayList

    3.当用户checks the checkbox ,在您添加此联系人contactArrayList并不断增加,当unchecks然后从接触contactArrayList

    4.当用户按下完成后,将结果设置为ok,并将选中的联系人列表添加到contactArrayList如下所示:

    Intent intent = new Intent();
    Bundle bundle = new Bundle();
    bundle.putStringArrayList("contacts", contactArrayList);
    intent.putExtras(bundle);
    setResult(RESULT_OK, intent);
    

    finish()该活动。

    5.在您的调用活动覆盖:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
            if(resultCode == RESULT_OK && data != null ){
                 Bundle bundle = new Bundle();
                 bundle =  data.getExtras();
                 ArrayList list = bundle.getStringArrayList("contacts");
        }
    }
    

    注意:上面的代码在2.3.3以上进行了测试。


    请试试这个代码

    Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        String name, number = "";
        String id;
        c.moveToFirst();
        for (int i = 0; i < c.getCount(); i++) {
            name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
    
            if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id },
                        null);
                while (pCur.moveToNext()) {
                    number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }
            }
            Log.i("name ", name + " ");
            Log.i("number ", number + " ");
            c.moveToNext();
    

    在这里你得到了光标c

    Cursor c = getContentResolver().query(Phones.CONTENT_URI, null, null,null, null);
    

    获取名称

    String name = c.getString(c.getColumnIndex(People.NAME));
    

    得到号码

    String number = c.getString(c.getColumnIndex(People.NUMBER));
    

    您可以遍历游标并将它们保存到列表中。 创建一个复选框列表视图并与该列表绑定。

    链接地址: http://www.djcxy.com/p/31981.html

    上一篇: Fetch Contacts in android application

    下一篇: Get first and last name of a contact rather than single display name?