Android的API - 获得联系人的手机号码 [英] Android Api - get mobile number from contacts

查看:113
本文介绍了Android的API - 获得联系人的手机号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过这么多的教程,并在这里读了很多的SO,但我不能解决我的问题:

I tried so much tutorials, and read a lot here at SO, but i cant solve my problem:

当点击一个按钮,用户可以选择一个联系人的手机号码。实际我可以选择联系人的姓名,但我不能找到一种方法,让/选择了手机号码。

When a button is clicked, the user can choose the mobile number of a contact. Actual I can get the name of the selected contact, but i can't find a way, to get/chose the mobile number..

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    /** Layouting */
    this.mGetMobileNumberButton = (Button)findViewById(R.id.getMobileNumberButton);
    this.mNameTextView = (TextView)findViewById(R.id.nameTextView);
    this.mMobileNumberTextView = (TextView)findViewById(R.id.mobileNumberTextView);


    /** onClick getContactInfos*/
   this.mGetMobileNumberButton.setOnClickListener(new OnClickListener() {
      public void onClick(View v){ 
          Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
          startActivityForResult(intent, 1);        
      } 
    });
}

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);
    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));
            mNameTextView.setText(name);
        }
    }
}

希望有人可以帮助:)

Hope anyone can help :)

推荐答案

这将让光标保持接触的基础数据,并通过电话号码联系人有循环,可以有多个。

This will get the cursor holding base contact data, and will loop through the phone numbers the contact has, can have multiple.

    Uri uri = data.getData();
Cursor cursor=ctx.getContentResolver().query(uri, null, null, null, null);

   while (cursor.moveToNext()) { 
   String contactId = cursor.getString(cursor.getColumnIndex( 
     ContactsContract.Contacts._ID)); 
   String hasPhone = cursor.getString(cursor.getColumnIndex( 
     ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
   if (Boolean.parseBoolean(hasPhone)) { 
                // You know have the number so now query it like this
Cursor phones = getContentResolver().query( 
  ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
  null, 
  ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, 
      null, null); 
    while (phones.moveToNext()) { 
     String phoneNumber = phones.getString( 
       phones.getColumnIndex( 
         ContactsContract.CommonDataKinds.Phone.NUMBER));                 
    } 
    phones.close(); 
   } 
 }

这篇关于Android的API - 获得联系人的手机号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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