views:

40

answers:

1

I'm trying to get an Android background service to be "notified" when the user chooses a phone number (he wishes to call) from the phone's contacts list. My goal with this is to prevent the system from placing the call and present a user with a choice dialog, then go from there... Need help.

+1  A: 

I found a way to do what I asked yesterday, so I'm sharing it here for anyone interested. The trick was, to catch the NEW_OUTGOING_CALL broadcasted intent in a BroadcastReceiver and not any event related to the choosing of contacts, as I was thinking.

So... follow the steps.

In the application element of the AndroidManifest.xml file add a receiver element... The android:name attribute is the class that will extend from BroadcastReceiver (explained below).

<receiver android:name=".OutgoingCallDetection">

<action android:name="android.intent.action.NEW_OUTGOING_CALL"
  android:priority="0" />

ahh! And you'll need a special permission...

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />

The onReceive method in the class that extends BroadcastReceiver (OutgoingCallDetection in this example). Note that setResultData(null) is what prevents the call from being made by the system itself.

public void onReceive(Context arg0, Intent arg1) {

    setResultData(null);

    // Start an activity and then show a dialog, or something...

}

Here it is, and working as needed.

André Maricato