【发布时间】:2016-08-16 23:24:55
【问题描述】:
我尝试从给定特定电话号码的电话联系人中打印联系人姓名(例如“Jose”)和联系人电话号码类型值(例如“Home”、“Work”等)(例如“9600515852” ")
为此,我使用了以下代码。但我只能获取联系人姓名。当我尝试获取电话号码类型时,应用程序崩溃了。
请指导我。
String number = getContactName(this, "9600515852");
Toast.makeText(getApplicationContext(),number,Toast.LENGTH_LONG).show();
private String getContactName(Context context, String number) {
String TAG ="" ;
String name = null, label=null;
int type=0;
// define the columns I want the query to return
String[] projection = new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME,ContactsContract.PhoneLookup._ID };
// encode the phone number and build the filter URI
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
// query time
Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null);
if(cursor != null) {
if (cursor.moveToFirst()) {
name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
// type = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
} else {
Log.v(TAG, "Contact Not Found @ " + number);
}
cursor.close();
}
return name+" - "+number;
}
【问题讨论】:
标签: android android-contacts phone-number