Recipe 4.4 Blocking Outgoing Calls

Android Versions
Level 1 and above
Permissions
android.permission.PROCESS_OUTGOING_CALLS
Source Code Download from Wrox.com
BlockOutgoingCall.zip

If you are writing a parental control application, you might want to block the user from making calls to certain numbers. For example, you might not want your children to call specific numbers, such as premium-rate phone numbers offered by service providers for online dating, online quizzes, and so on.

In this recipe, you will learn how to block a specific outgoing call.

Solution

To block an outgoing call, create a BroadcastReceiver class and override its onReceive() method. When an outgoing call is made, the onReceive() method is called. To prevent the phone from calling, you simply need to call the setResultData() method by passing it a null argument. This will prevent the device from making the call. Of course, you may want to block only certain numbers, and hence in this method you can retrieve the phone number that the user is trying to call. Using this, you can selectively block outgoing calls. The following code snippet shows the BroadcastReceiver class:

package net.learn2develop.blockoutgoingcall; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class OutgoingCallsReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String outgoingNumber ...

Get Android Application Development Cookbook: 93 Recipes for Building Winning Apps now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.