本文介绍了Android的联系人列表获得电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好我想在我的应用程序这个code我能得到联系人列表,但是当我美元,联系人姓名p $ PSS我没有得到我的EditText任何事情,我期望能获得联系人电话号码
对不起我的英语不好
公共无效doLaunchContactPicker(查看视图){
意图contactPickerIntent =新意图(Intent.ACTION_PICK,
Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent,CONTACT_PICKER_RESULT);
} @覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
如果(结果code == RESULT_OK){
开关(要求code = RESULT_OK){
案例CONTACT_PICKER_RESULT:
光标光标= NULL;
字符串手机=;
尝试{
捆绑额外= data.getExtras();
SET<串GT;键= extras.keySet();
迭代器<串GT;迭代= keys.iterator();
而(iterate.hasNext()){
字符串键= iterate.next();
Log.v(DEBUG_TAG,键+[+ extras.get(键)+]);
} 乌里结果= data.getData();
Log.v(DEBUG_TAG,有一个接触的结果:
+ result.toString()); //从乌里获得接触ID
字符串ID = result.getLastPathSegment();
光标= getContentResolver()查询(Phone.CONTENT_URI,
空,Phone.CONTACT_ID +=?,新的String [] {ID},
空值); INT PhoneIdx = cursor.getColumnIndex(Phone.DATA);
如果(cursor.moveToFirst()){ 手机= cursor.getString(PhoneIdx); Log.v(DEBUG_TAG,拿到号:+电话); }其他{
Log.w(DEBUG_TAG,无结果);
}
}赶上(例外五){
Log.e(DEBUG_TAG,无法号码,E);
} {最后
如果(指针!= NULL){
cursor.close();
}
的EditText ponenumber =(EditText上)findViewById(R.id.ednum);
ponenumber.setText(电话); 如果(phone.length()== 0){
Toast.makeText(这一点,没有号发现的接触。
Toast.LENGTH_LONG).show();
} } 打破;
} }其他{
Log.w(DEBUG_TAG,警告:活动的结果也不行);
}
}
解决方案
光标手机= getContentResolver()查询(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,NULL,NULL,NULL,NULL );而(phones.moveToNext())
{
字符串名称= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
串号= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));}
hi i am trying this code in my app i can get the contact list but when i press on contact name i didnt get any thing in my edittext which i expected to get the contact phone numbersorry about my poor english
public void doLaunchContactPicker(View view) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode=RESULT_OK) {
case CONTACT_PICKER_RESULT:
Cursor cursor = null;
String phone = "";
try {
Bundle extras = data.getExtras();
Set<String> keys = extras.keySet();
Iterator<String> iterate = keys.iterator();
while (iterate.hasNext()) {
String key = iterate.next();
Log.v(DEBUG_TAG, key + "[" + extras.get(key) + "]");
}
Uri result = data.getData();
Log.v(DEBUG_TAG, "Got a contact result: "
+ result.toString());
// get the contact id from the Uri
String id = result.getLastPathSegment();
cursor = getContentResolver().query(Phone.CONTENT_URI,
null, Phone.CONTACT_ID + "=?", new String[] { id },
null);
int PhoneIdx = cursor.getColumnIndex(Phone.DATA);
if (cursor.moveToFirst()) {
phone = cursor.getString(PhoneIdx);
Log.v(DEBUG_TAG, "Got number: " + phone);
} else {
Log.w(DEBUG_TAG, "No results");
}
} catch (Exception e) {
Log.e(DEBUG_TAG, "Failed to Number", e);
} finally {
if (cursor != null) {
cursor.close();
}
EditText ponenumber = (EditText) findViewById(R.id.ednum);
ponenumber.setText(phone);
if (phone.length() == 0) {
Toast.makeText(this, "No number found for contact.",
Toast.LENGTH_LONG).show();
}
}
break;
}
} else {
Log.w(DEBUG_TAG, "Warning: activity result not ok");
}
}
解决方案
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
String Name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
String Number=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
这篇关于Android的联系人列表获得电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!