There's a class that iextends BroadcastReceiver.I have the action "isNumberStored (Context context, String number)" which returns "true" if a phone number (string of numbers) exists in the contacts. When a incoming or outgoing call starts I want to get the phone number and than check if the number is stored in my contacts or not. How can I apply this?
my class :
public class CallReceiver extends BroadcastReceiver {
@Override
// ^ detecting incoming and outgoing calls ^
public void onReceive(Context context , Intent intent) {
if (intent.getStringExtra( TelephonyManager.EXTRA_STATE ).equals( TelephonyManager.EXTRA_STATE_OFFHOOK )) {
//Call started
} else if (intent.getStringExtra( TelephonyManager.EXTRA_STATE ).equals( TelephonyManager.EXTRA_STATE_IDLE )) {
//Call ended
} else if (intent.getStringExtra( TelephonyManager.EXTRA_STATE ).equals( TelephonyManager.EXTRA_STATE_RINGING )) {
//Incoming call
}
}
//Check if number is stored in Contacts list (Truth = exists).
public boolean isNumberStored(Context context, String number) {
Uri lookupUri = Uri.withAppendedPath(
ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
String[] mPhoneNumberProjection = { ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.NUMBER, ContactsContract.PhoneLookup.DISPLAY_NAME };
Cursor cur = context.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null);
try {
if (cur.moveToFirst()) {
cur.close();
return true;
}
} finally {
if (cur != null)
cur.close();
}
return false;
}
}