Sunteți pe pagina 1din 3

158

CHAPTER 4 Telephony

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- put this here so that even if the app is not running,
your app can be woken up if there is a change in phone
state -->
<receiver android:name=".PhoneStateReceiver">
<intent-filter>
<action
android:name=
"android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>

Thats all there is to it. There is no need for you to register the listener in your activity. Once the
application is installed on the device, the BroadcastReceiver will automatically be fired whenever
there is a change in phone state.

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.

Recipe 4.4 Blocking Outgoing Calls

159

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
import
import
import

android.content.BroadcastReceiver;
android.content.Context;
android.content.Intent;
android.widget.Toast;

public class OutgoingCallsReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
String outgoingNumber =
intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER).toString();
if (outgoingNumber.contentEquals("1234567")) {
setResultData(null);
Toast.makeText(context, "This call is not allowed!",
Toast.LENGTH_LONG).show();
}
}
}

In order for the BroadcastReceiver class to be fired when an outgoing call is made, you
need to add the PROCESS_OUTGOING_CALLS permission, as well as declare the receiver in the
AndroidManifest.xml file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.learn2develop.blockoutgoingcall"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

160

CHAPTER 4 Telephony

<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".OutgoingCallsReceiver" >
<intent-filter android:priority="0">
<action android:name=
"android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application>
</manifest>

Thats it! If the user tries to call a number that is defined in your BroadcastReceiver class, the call
will not be successful.

Recipe 4.5 Auto-Answering an Incoming Call


Android Versions
Level 1 and above

Permissions
android.permission.READ_PHONE_STATE

Source Code Download from Wrox.com


AutoCalls.zip
Recipe 4.3 showed you how to monitor the state of the phone to detect whether it is ringing, off
hook, or idle. Another useful behavior is answering a call automatically when the phone rings. For
example, you might want to write an application that allows the user to automatically answer a
call (only for some specific phone numbers) when the user is busy (such as when he is driving). This
recipe demonstrates how to do that.

Solution
To programmatically answer an incoming call, your application simply needs to emulate the device
with a Bluetooth headset attached. When an incoming call is detected, you fire an Intent object to
simulate the user pressing the button on the Bluetooth headset.

S-ar putea să vă placă și