如何检索联系人姓名和电话号码的Andr​​oid [英] How to retrieve Contact name and phone number in Android

查看:116
本文介绍了如何检索联系人姓名和电话号码的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Andr​​oid开发,我想要检索联系人列表有姓名和电话号码。我尝试以下code:

I'm new to android development, I'm trying to retrieve contact list with there name and phone numbers. I try following code :

 // Get a cursor over every contact.
    Cursor cursor = getContentResolver().query(People.CONTENT_URI, 
                                               null, null, null, null); 
    // Let the activity manage the cursor lifecycle.
    startManagingCursor(cursor);
    // Use the convenience properties to get the index of the columns
    int nameIdx = cursor.getColumnIndexOrThrow(People.NAME); 

    int phoneIdx = cursor. getColumnIndexOrThrow(People.NUMBER);
    String[] result = new String[cursor.getCount()];
    if (cursor.moveToFirst())
      do { 
        // Extract the name.
        String name = cursor.getString(nameIdx);
        // Extract the phone number.
        String phone = cursor.getString(phoneIdx);
        result[cursor.getPosition()] = name + "-" +" "+  phone;
      } while(cursor.moveToNext());

这code应该返回一个包含有所有联系人的名字和它的电话号码,但接触了这只返回的名字和电话号码返回NULL,

This code should return an array with the all contacts name and its phone number but this only returns name of the contact and returns NULL in phone number,

输出示例:

 John - null

请帮我在这方面,多数民众赞成什么是错在这code。 任何帮助在这方面会非常AP preciated,其迫切预先感谢。

Please help me in this regards thats whats wrong in this code. Any help in this regards will highly appreciated, and its urgent thanks in advance.

推荐答案

在Android清单:

In Android manifest:

    <uses-permission android:name="android.permission.READ_CONTACTS" />

然后在活动:

Then in the activity:

editText.setOnFocusChangeListener(new OnFocusChangeListener(){

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    editText.setText("");   
                     Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                     startActivityForResult(intent, PICK_CONTACT);
                }
            }           
        });

然后,你必须抓住的动作挑接触的结果是:

And then you have to catch the result of the action pick contact:

@Override 
public void onActivityResult(int reqCode, int resultCode, Intent data){ 
    super.onActivityResult(reqCode, resultCode, data);

    switch(reqCode)
    {
       case (PICK_CONTACT):
         if (resultCode == Activity.RESULT_OK)
         {
             Uri contactData = data.getData();
             Cursor c = managedQuery(contactData, null, null, null, null);
              if (c.moveToFirst())
              {
                  String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

                  String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                  if (hasPhone.equalsIgnoreCase("1")) 
                  {
                      Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
                             ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
                      phones.moveToFirst();
                      String cNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                       Toast.makeText(getApplicationContext(), cNumber, Toast.LENGTH_SHORT).show();

                      String nameContact = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));

                      editText.setText(nameContact+ " "+ cNumber);
                  }
             }
       }
    }
}

这篇关于如何检索联系人姓名和电话号码的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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