从 Android 联系人中获取多个号码
我一直在搜索和搜索如何从联系人处获取多个电话号码(即家庭、手机、工作),但我被难住了。我将在下面添加我正在使用的代码。我希望有人可以提供帮助。谢谢 杰夫
case CONTACT_PICKER_RESULT:
Log.w("+DEBUG_TAG+","Got the Info");
//handle contact results
Cursor cursor = null;
String number = "";
String number2 = "";
try{
Uri result = data.getData();
//get the content id
String id = result.getLastPathSegment();
//ask for the phone number
cursor = getContentResolver().query(Phone.CONTENT_URI,
null, Phone.CONTACT_ID + "=?", new String[] {id},
null);
int phoneIdx = cursor.getColumnIndex(Phone.DATA);
//take the phone number
if(cursor.moveToFirst()){
number = cursor.getString(phoneIdx);
Log.v("+DEBUG_TAG+","Got number " + number);
}else if(cursor.moveToNext()){
number2 = cursor.getString(phoneIdx);
Log.v("+DEBUG_TAG+","GOT NumbEr2 "+ number2);
}
else{
Log.e("+DEBUG_TAG","FAILED TO GET NUMBER!");
}
}
finally{
if(cursor != null){
cursor.close();
}
EditText phNumberEditText = (EditText) findViewById(R.id.number1);
phNumberEditText.setText(number);
if (number.length() == 0){
Toast.makeText(this, "No Phone Number For This Contact",
Toast.LENGTH_LONG).show();
}
I've been searching and searching on how to grab multiple phone numbers (ie Home, Cell, Work) from a contact but I am Stumped. I'll add the code I'm using below. I hope some one can help. Thanks
Jeff
case CONTACT_PICKER_RESULT:
Log.w("+DEBUG_TAG+","Got the Info");
//handle contact results
Cursor cursor = null;
String number = "";
String number2 = "";
try{
Uri result = data.getData();
//get the content id
String id = result.getLastPathSegment();
//ask for the phone number
cursor = getContentResolver().query(Phone.CONTENT_URI,
null, Phone.CONTACT_ID + "=?", new String[] {id},
null);
int phoneIdx = cursor.getColumnIndex(Phone.DATA);
//take the phone number
if(cursor.moveToFirst()){
number = cursor.getString(phoneIdx);
Log.v("+DEBUG_TAG+","Got number " + number);
}else if(cursor.moveToNext()){
number2 = cursor.getString(phoneIdx);
Log.v("+DEBUG_TAG+","GOT NumbEr2 "+ number2);
}
else{
Log.e("+DEBUG_TAG","FAILED TO GET NUMBER!");
}
}
finally{
if(cursor != null){
cursor.close();
}
EditText phNumberEditText = (EditText) findViewById(R.id.number1);
phNumberEditText.setText(number);
if (number.length() == 0){
Toast.makeText(this, "No Phone Number For This Contact",
Toast.LENGTH_LONG).show();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用这种代码:
Use this kind of code: