在 Android 中,如何选择联系人并将其显示在我的应用程序上?
我是android的初学者,我正在构建一个应用程序,当用户按下按钮时,会显示存储在手机中的联系人。当他从中选择联系人时,我必须获取所选联系人的姓名和号码。我尝试使用该代码,但仅显示所选联系人的姓名,而不显示电话号码。
public void readcontact(){
try {
Intent intent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts/people"));
startActivityForResult(intent, PICK_CONTACT);
} catch (Exception e) {
e.printStackTrace();
}
}
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);
startManagingCursor(c);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(People.NAME));
String number = c.getString(c.getColumnIndexOrThrow(People.NUMBER));
perrsonname.setText(name);
Toast.makeText(this, name + " has number " + number, Toast.LENGTH_LONG).show();
}
}
break;
}
}
我什至需要所选联系人的附加号码(家庭、办公室等), 谁能帮我解决这个问题。对此的建议是值得重视的。
I am a beginner to android, i am building a application in which when the user presses a button, the contacts which is stored in the mobile are shown. When he selects a contact from that, i have to get the selected contact name and number. I tried using the code but only the name of the selected contact is shown and not the phone number.
public void readcontact(){
try {
Intent intent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts/people"));
startActivityForResult(intent, PICK_CONTACT);
} catch (Exception e) {
e.printStackTrace();
}
}
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);
startManagingCursor(c);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(People.NAME));
String number = c.getString(c.getColumnIndexOrThrow(People.NUMBER));
perrsonname.setText(name);
Toast.makeText(this, name + " has number " + number, Toast.LENGTH_LONG).show();
}
}
break;
}
}
I even need the additional number(home,office etc.,)of the selected contacts,
Can anyone help me out with this. Suggestions on this is appreciable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在这里,我将向您演示如何在点击或焦点事件时从联系人列表中选择联系人号码和姓名,
焦点事件:-
获取联系人姓名和电话号码的函数是:
}
Here i will demonstrate you that how to pick a contact no and name from a contact list on click or focus event,
Focus Event:-
A FUNCTION TO GET THE CONTACT NAME AND PHONE NO IS :
}
这在我的实现中有效。
This works in my implementation.
这可以轻松地提供所选联系人的所有电话号码
启动联系人活动,
startActivityForResult(intent, PICK_CONTACT);
现在使用以下命令获取所有数据,
This gives all the phone numbers from a picked contact in a hassle free manner
Start the Contact Activity,
startActivityForResult(intent, PICK_CONTACT);
Now use the following to get all the data,
我发现选择带有电话号码的联系人并显示显示名称和所选电话号码的最简单方法。另请参阅:完整的可执行示例作为要点。
如果您需要其他字段,例如电话号码和电子邮件,请查看此问题中的答案。
Simplest way I've found to pick a contact with phone number, and show display name and the selected phone number. See also: full, executable example as gist.
If you need other fields, for example both phone number and email, check out answers in this question.
请参阅 Android 源附带的联系人应用程序
Refer Contacts Application which comes with Android Source