Documente Academic
Documente Profesional
Documente Cultură
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.
Permissions
android.permission.PROCESS_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;
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.
Permissions
android.permission.READ_PHONE_STATE
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.