gpt4 book ai didi

Android Api - 从联系人中获取手机号码

转载 作者:行者123 更新时间:2023-11-29 14:57:13 28 4
gpt4 key购买 nike

我尝试了很多教程,并在 SO 上阅读了很多,但我无法解决我的问题:

单击按钮时,用户可以选择联系人的手机号码。实际上我可以获得所选联系人的姓名,但我找不到获取/选择手机号码的方法..

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

希望有人能帮忙:)

最佳答案

这将获取包含基本联系人数据的光标,并将遍历联系人拥有的电话号码,可以有多个。

    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 - 从联系人中获取手机号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3695566/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com