如何检索联系人姓名和电话号码的Andr​​oid电话号码、姓名、联系人、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 follo...

我是新的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);
                  }
             }
       }
    }
}
阅读全文

相关推荐

最新文章