I am trying to make an application on android that takes the contact name as a string input and returns his phone number if that contact exists in the phone book…
I tried searching around but there is no clear tutorial as to how to do exactly that
input:contact name
outputs:the phone number
please help
Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if(name.equalsIgnoreCase(token3)) {
try{ ContentResolver cr = context.getContentResolver();
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { ContactsContract.CommonDataKinds.Phone._ID}, null);
String lname = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(context, "number is "+lname, Toast.LENGTH_LONG).show();
}catch (Exception e) {
// TODO: handle exception
}
}
}
it’s what I have so far. the piece of code in the try catch block always crashes.
I wrote this method eventually to solve my problem
it may not be very efficient but hey it works