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

时间:2012-01-09 07:29:25

标签: android android-contacts

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

How to call Android contacts list?

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

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

-

  1. 我想在每个联系人处添加一个复选框,以便用户可以选择 多个联系人,点击完成按钮,他应该可以 得到所有的        他选择的联系人

  2. 此外,我想获得联系人姓名和电话        联系人数量,请参阅我的代码:

  3.     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.
                        }
                    }
    

    在log cat上显示输出为:

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

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

    编辑:

    如果我在代码中使用此(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
    

5 个答案:

答案 0 :(得分:9)

通过查看答案,我认为您可以获得如何获取联系人的答案,现在您想要获取活动中的选定联系人。

  

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

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,然后从{{1}中删除该联系人}}

4. 当用户按完成后,使用您在contactArrayList中添加的所选联系人列表设置结果确定如下:

contactArrayList

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

5. 在您的通话活动覆盖:

finish()

注意:以上代码经过2.3.3测试。

答案 1 :(得分:4)

请尝试此代码

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

答案 2 :(得分:2)

在这里你有光标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));

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

答案 3 :(得分:2)

只需拥有一个联系人数据对象,每当您选择列表中的任何项目时,请将该联系人的值设置为此字段。

AdapterView.onItemClickListener()
{
   public void onItemClick(AdapterView list, View view, int position, long id)
   {
      Contact contact=listContacts.get(position);
      selectedContact=contact;//If Done Button on List
      //if activity is to be finished on selection
      { 
         Intent intent=new Intent();
         intent.putExtra(KEY_NAME, contact.Name);
         ....
         setResult(RESULT_OK, intent);
         finish();
      }
    }
}

答案 4 :(得分:1)

尝试以下代码:

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(cursor.getColumnIndex(Phone.NUMBER));    
    }while (cursor.moveToNext() );     
}