Tuesday, July 30, 2013

[android-developers] Re: Get the incoming caller name before phone starts to ring

what the hell? why are you scanning the entire DB and in such a bad fashion? 
You do know that the reason there's a ContentResolver is for you to run queries right?


You'll get the result instantly. 

On Tuesday, July 30, 2013 9:27:49 PM UTC+3, shiva pendem wrote:
HI everyone,

i am making an application to get the name of the contact no saved on the mobile.

i am using the following code

private class CallStateListener extends PhoneStateListener {
private String displayContacts(String incomingNumber) {
    ContentResolver cr = ctx.getContentResolver();
       Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
               null, null, null, null);
       if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
        String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        if (Integer.parseInt(cur.getString(
                       cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    Cursor pCur = cr.query(
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                    new String[]{id}, null);
                    while (pCur.moveToNext()) {
                    String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    // Log.v("Test",phoneNo.substring(phoneNo.length()-10,phoneNo.length())+"   "+incomingNumber.substring(incomingNumber.length()-10,incomingNumber.length()));
                    Toast.makeText(ctx, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
                    Log.v("Test","qualified   "+phoneNo+"   "+name);
                    return ""+name;
       return "";
i am calling the displayContacts() method with the incoming call no as argument
but its taking about 18 to 29 seconds to get the contact name, some times it is taking more than 35 seconds,

can any one suggest me the best way to get the contact names saved in the mobile,
i need to get the contact name in the following manner,
i have a contact with name as service call and no as 9848012345
and if i get a call i should get the name in not more than 5 seconds .

if there are any new mechanisms that are used to implement this mechanism plese send me the links,

Shiva Shankar,

You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group at
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment