在获取Android应用程序与我们联系 [英] Fetch Contacts in android application

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

问题描述

我在以下链接获得联系人在我的应用程序

如何调用Android的联系人列表?

<一个href="http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/">http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/

我可以显示联系人的电话列表中,但

-

  1. 我想补充一个复选框,在每个联系人,以便用户可以选择 多个联系人,并通过点击完成按钮,他应该能 让所有的        他选定的联系人

  2. 此外,我希望得到联系人的姓名和电话        联系号码,请参阅我的code:

    如果(结果code == Activity.RESULT_OK){
                    乌里联系数据= data.getData();
                    光标C = managedQuery(联络人资料,NULL,NULL,NULL,NULL);
                    如果(c.moveToFirst()){
                        字符串名称= c.getString(C
                                .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                        串号= c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                        Log.v(名,名称++号);
                        不管你想要做选定联系人// TODO
                        // 名称。
                    }
                }

在日志猫它显示为输出:

  12月1号至九日:46:41.688:V /名(699):XYZ 1
 

这是联系人的姓名是XYZ,它有ATLEAST 1手机与it.Please相关的一些帮助我我怎样才能与该联系人相关的数字。

编辑:

如果我用这个(串号= c.getString(c.getColumnIndexOrThrow(People.NUMBER)); )线在code中的我得到以下异常

  01-09 13:33:23.008:E / AndroidRuntime(786):致命异常:主要
01-09 13:33:23.008:E / AndroidRuntime(786):java.lang.RuntimeException的:不提供结果ResultInfo {谁= NULL,请求= 1,结果= -1,数据=意向{DAT =内容:// com.android.contacts /联系人/查询/ 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(本机方法)
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):在dalvik.system.NativeStart.main(本机方法)
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();
光标光标= contactResolver.query(Phone.CONTENT_URI,空,Phone.DISPLAY_NAME +=?,新的String [] {}联系人姓名,NULL);

如果(cursor.getCount()大于0){
    cursor.moveToFirst();
    做 {
       串号= cursor.getString(mCursor.getColumnIndex(Phone.NUMBER));
    }而(cursor.moveToNext());
}
 

注意:在这里联系人姓名是您要fecth联系人号码的联系人姓名

  

我假设你已经如数复选框中的联系人   ListView和这里是您的解决方案来获得所选择的联系人列表   用户到你的活动:

1 startActivityForResult启动您的联系活动()

2 初始化的ArrayList 接触活动变量说它 contactArrayList

3。:当用户检查复选框,在添加此联系人的 contactArrayList 并继续增加,当取消选中然后删除联系人的 contactArrayList

4。:当完成用户presses然后将结果确定与您添加在 contactArrayList 是这样选择的联系人列表

 意向意图=新的意图();
束束=新包();
bundle.putStringArrayList(联系人,contactArrayList);
intent.putExtras(包);
的setResult(RESULT_OK,意图);
 

完成()本次活动。

在您的通话活动覆盖 5

  @覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
    super.onActivityResult(要求code,因此code,数据);
        如果(结果code == RESULT_OK和放大器;&放大器;数据!= NULL){
             束束=新包();
             捆绑= data.getExtras();
             ArrayList的名单= bundle.getStringArrayList(接触);
    }
}
 

注意:上面的code进行了测试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天全站免登陆