Saturday, January 31, 2009

Getting the Contact Info when on a number

On android when someone call you or you receive a message, a non formatted number would be shown to you and for you to find out whom that number is, you have to use Contacts.Phones.CONTENT_FILTER_URL on your query. This article is based on Get Phone State When Someone is calling using BroadcastReceiver Example
public class MyPhoneStateListener extends PhoneStateListener {
  private String[] projection = new String[] {
    People._ID, People.NAME, People.NUMBER
  };
  public void onCallStateChanged(int state,String incomingNumber){
    switch(state)
    {
      case TelephonyManager.CALL_STATE_IDLE:
        Log.d("DEBUG", "IDLE");
      break;
      case TelephonyManager.CALL_STATE_OFFHOOK:
        if(!incomingNumber.equals("")){
          handleCall(incomingCall);
        }
      break;
      case TelephonyManager.CALL_STATE_RINGING:
        Log.d("DEBUG", "RINGING");
      break;
    }
  }
  public void handleCall(String incomingCall){
    Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, incomingNumber);
    contactsCursor = context.getContentResolver().query(contactUri, projection, null , null, People.NAME + " ASC");
    if(contactsCursor.moveToFirst()){
      int phoneNameIndex = contactsCursor.getColumnIndex(People.NAME);
      String phoneNameStr = contactsCursor.getString(phoneNameIndex);
      Log.d("DEBUG",phoneNameStr + " is calling");
    }else{
      Log.d("DEBUG","Not a contact");
    }
  }
}



Quick Explanation
Looking on the handleCall function, there is nothing really new here, for the BroadcastReceiver explanation go to this page and for the displaying contact names go to this page. Sorry i'm too lazy to explain here coz i already explained it on the previous post.

Anyway i have to temporarily stop my android adventure and do some stuff for work which is webbased stuff so the next article would be web based article. Thanks

Update History
   Jan 17, 2012 - Visual Update
Post a Comment