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