views:

791

answers:

2

I'd like to have some code be notified of any incoming and outgoing voice calls. I need to grab all the following:

  • If it was an incoming or outgoing call
  • Number dialed, or incoming caller-id phone number
  • Duration of call, or if it was a missed call

It would also be really slick if I could get more contact info, specifically the contact name, if the phone number matches up to someone in the phone's contacts list, but that part is optional.

I need my code to get this information pretty much as soon as a call has ended. Maybe I'm just not searching for the correct terms but I'm having a real hard time finding any examples of how I would accomplish this.

If someone could provide a code example that accomplishes this, along with the permissions and any sort of broadcast receiver type of setup I would need in the manifest file, that would be great.

+1  A: 

A PhoneStateListener and the CallLog content provider will give you most, if not all, of what you need.

CommonsWare
+2  A: 

You should be able to directly watch outgoing calls by listening for android.intent.action.NEW_OUTGOING_CALL broadcast events, by setting the android.permission.PROCESS_OUTGOING_CALLS permission in your manifest.

I imagine this should work for other devices; at least on my HTC Hero this gives me an Intent full of extras including the phone number being called, the contact's name plus URIs for the contact and individual number being called.

As for getting full information on incoming or missed calls (and indeed outgoing calls), I believe the solution as Mark mentions is to watch the CallLog content provider for changes, or poll after the relevant PhoneStateListener events have been fired.

Christopher