1

I am trying to find out whether a incoming call from favourites contacts in Android. So far, my code is:

    public class PhoneCallReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(final Context context, Intent intent) {
            TelephonyManager telephony = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            telephony.listen(new PhoneStateListener() {
                @Override
                public void onCallStateChanged(int state, String incomingNumber) {
                    super.onCallStateChanged(state, incomingNumber);
                    if (state == TelephonyManager.CALL_STATE_RINGING) {
                        if (ContactHelper.fromFavourites(context, incomingNumber)) {
                             //do stuff
                        }
                    }
                };

And my ContactHelper is like this:

public static boolean fromFavourites(Context context, String phoneNumber) {
        final String[] projection = new String[] {ContactsContract.PhoneLookup._ID};
        Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); //use this to look up a phone number
        Cursor cursor = context.getContentResolver().query(lookupUri, projection, "starred=?", new String[] { "1" }, null);
        if (cursor != null && cursor.getCount() != 0) {
            System.out.println("OUTPUT: "+cursor0.getCount());
            return true;
        } else return false;
    }

I have tried this solution but it only gives me all favourites contacts. I am trying to use PhoneLookup because from the Android doc, it says

Columns from the Contacts table are also available through a join.

So I think I can query a join between PhoneLookUp and Contacts table but seems like the Content Providers can't do a join. I intend to write a raw SQLite script for this but I don't know how to join the PhoneLookUp and Contacts table, can't find their foreign key :( Thanks for all the helps

Community
  • 1
  • 1
Duc
  • 511
  • 1
  • 4
  • 18
  • what is your question exactly? do you want to check what your title says?? your question description seems different from the title – stinepike Apr 26 '13 at 15:48
  • sorry if I didn't make it clear, the title is what I am trying to achieve, my question description is what I am having problem with. I have included some code, hope it gets a bit clearer – Duc Apr 26 '13 at 16:13

2 Answers2

0

your first link is okay to get all favorite contacts. now to determine whether the incoming number is from favorite you have to detect a incoming call. So use a PhoneStateListener to detect the inocming call. When there is a incoming call detection then simply check for the favorite

To detect an incoming call

public class CustomPhoneStateListener extends PhoneStateListener {

public void onCallStateChange(int state, String number){
    switch(state){
        case TelephonyManager.CALL_STATE_RINGING:
            //call from number. check whether it is favorite or not
            break;
    }   
}

also use following permission

< uses-permission android:name="android.permission.READ_PHONE_STATE" />
stinepike
  • 54,068
  • 14
  • 92
  • 112
  • thanks for your answer but it is what I have done already and your code comment bit is what I am stuck with – Duc Apr 26 '13 at 16:14
0
public static boolean fromFavourites(Context context, String phoneNumber) {
    final String[] projection = new String[] {ContactsContract.PhoneLookup.STARRED};
    Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); //use this to look up a phone number
    Cursor cursor = context.getContentResolver().query(lookupUri, projection,  
                   ContactsContract.PhoneLookup.NUMBER + "=?", 
                    new String[] { phoneNumber}, null);
    if (cursor.moveToFirst()) {
        while (!cursor.isAfterLast()) {
            if (cursor.getInt(cursor.getColumnIndex(ContactsContract.PhoneLookup.STARRED)) == 1) {
            System.out.println("OUTPUT: " + cursor.getInt(0) );
            return true;
            }
            cursor.moveToNext();
        }   
    } 
    return false;
}
Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
  • sorry but it doesn't work. The starred=? and the argument still returning contact no matter it is starred or not :(. – Duc Apr 26 '13 at 22:57
  • it works, thank you. Just wondering in the doc, when it says "Columns from the Contacts table are also available through a join" does it mean that 2 tables are already joined and we can freely use any of the column? – Duc Apr 27 '13 at 09:26
  • Yes, you can freely use them. For some strange reason the AND does not work so I twitch it like the above. – Hoan Nguyen Apr 27 '13 at 16:57