連絡先の名前(例:「Jose」)&連絡先の電話番号のタイプ値(「自宅」、「仕事」など)特定の電話番号(たとえば、「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;
}