在android应用程序中获取联系人 [英] Fetch Contacts in android application

查看:22
本文介绍了在android应用程序中获取联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

如何调用 Android 联系人列表?

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

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

-

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

  2. 我还想知道联系人的姓名和电话联系方式,见我的代码:

<前>如果(结果代码 == Activity.RESULT_OK){URI contactData = data.getData();光标 c = managedQuery(contactData, null, null, null, null);如果(c.moveToFirst()){字符串名称 = 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 无论您想对所选联系人做什么//姓名.}}

在 log cat 上,它显示输出为:

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

那是联系人的名字是 xyz 并且它至少有 1 个关联的电话号码.请帮助我如何获取与该联系人关联的号码.

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

01-09 13:33:23.008: E/AndroidRuntime(786): 致命异常: main01-09 13:33:23.008: E/AndroidRuntime(786): java.lang.RuntimeException: 未能传递结果 ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0r1-2C2E30/1(有额外的)}} 到活动 {sra.com/sra.com.ContactsDemo}:java.lang.IllegalArgumentException:列编号"不存在01-09 13:33:23.008: E/AndroidRuntime(786): 在 android.app.ActivityThread.deliverResults(ActivityThread.java:3515)01-09 13:33:23.008: E/AndroidRuntime(786): 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)01-09 13:33:23.008:E/AndroidRuntime(786):在 android.app.ActivityThread.access$2800(ActivityThread.java:125)01-09 13:33:23.008: E/AndroidRuntime(786): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)01-09 13:33:23.008: E/AndroidRuntime(786): 在 android.os.Handler.dispatchMessage(Handler.java:99)01-09 13:33:23.008: E/AndroidRuntime(786): 在 android.os.Looper.loop(Looper.java:123)01-09 13:33:23.008: E/AndroidRuntime(786): 在 android.app.ActivityThread.main(ActivityThread.java:4627)01-09 13:33:23.008: E/AndroidRuntime(786): 在 java.lang.reflect.Method.invokeNative(Native Method)01-09 13:33:23.008: E/AndroidRuntime(786): 在 java.lang.reflect.Method.invoke(Method.java:521)01-09 13:33:23.008: E/AndroidRuntime(786): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)01-09 13:33:23.008: E/AndroidRuntime(786): 在 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):引起:java.lang.IllegalArgumentException:列编号"不存在01-09 13:33:23.008: E/AndroidRuntime(786): 在 android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)01-09 13:33:23.008: E/AndroidRuntime(786): 在 android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:99)01-09 13:33:23.008:E/AndroidRuntime(786):在 sra.com.ContactsDemo.onActivityResult(ContactsDemo.java:49)01-09 13:33:23.008: E/AndroidRuntime(786): 在 android.app.Activity.dispatchActivityResult(Activity.java:3890)01-09 13:33:23.008: E/AndroidRuntime(786): 在 android.app.ActivityThread.deliverResults(ActivityThread.java:3511)01-09 13:33:23.008: E/AndroidRuntime(786): ... 11 更多

解决方案

通过看到这些答案,我想您已经知道如何获取联系人的答案了,现在您想在您的活动中获取所选的联系人.

<块引用>

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

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

注意:这里的contactName是你想确定联系电话的联系人姓名.

<块引用>

我假设您已经显示了带有复选框的联系人ListView,这是您获取联系人列表的解决方案用户到您的活动:

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

2.在联系活动中初始化ArrayList变量说它contactArrayList.

3.当用户选中复选框时,将此联系人添加到您的contactArrayList中并继续添加,当取消选中时code> 然后从 contactArrayList 中删除联系人.

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

Intent intent = new Intent();Bundle bundle = new Bundle();bundle.putStringArrayList("contacts", contactArrayList);意图.putExtras(捆绑);设置结果(RESULT_OK,意图);

finish()这个活动.

5.关于您的通话活动覆盖:

@Overrideprotected 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("联系人");}}

注意:以上代码是在2.3.3以上测试的.

I was following these links to get the contacts in my application

How to call Android contacts list?

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

I can display the list of contacts on phone but

-

  1. I want to add a checkbox at each contact so that user can select multiple contacts and by clicking the done button he should be able to get all the contacts he selected

  2. Also I want to get the name of contact as well as the phone number of contact , see my code :

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

on log cat it shows output as :

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

that is the name of contact is xyz and it has atleast 1 phone number associated with it.Please help me on how can i get the number associated with that contact.

EDIT :

if i use this(String number = c.getString(c.getColumnIndexOrThrow(People.NUMBER));) line in code the I get following exception :

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

解决方案

By seeing the answers, I think you got the answer how to fetch contacts and now you want to get the selected contacts on your activity.

To fetch contact number specific to the contact name:

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

Note: Here contactName is the contact name of which you want to fecth contact numbers.

I am assuming that you have shown the contacts with checkbox in ListView and here is your solution to get the contact list selected by the user to your activity:

1. Start your contact activity with startActivityForResult().

2. Initialize ArrayList variable in contact activity say it contactArrayList.

3. When user checks the checkbox, add this contact in your contactArrayList and keep on adding and when unchecks then remove the contact from the contactArrayList.

4. When user presses done then set the result ok with the selected contact list which you have added in contactArrayList like this:

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

and finish() this activity.

5. On your calling activity override:

@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");
    }
}

Note: The above code was tested over 2.3.3.

这篇关于在android应用程序中获取联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆