Vous êtes sur la page 1sur 4

Do something when the phone rings | Android Labs

http://androidlabs.org/short-experiments/broadcast-receivers/do-somethi...

Android Labs A good place to pick up some Android skills.

Home About Lab rules Available Labs Android Lab Primer Lab 1 The Cow Goes BOO Lab 1.1 Setting up the project Lab 1.2 The Splash Activity Lab 1.3 The Main Activity Lab 1.4 More animals and sounds Short Experiments Location and Map experiments Getting Ready for Google Maps Development Draw a location marker on a MapView Broadcast receivers Do something when the phone rings Process outgoing calls Do something when the phone rings Problem You want to act on an incoming phone call and do something with the incoming number. Solution This can be achieved by implementing a Broadcast Receiver and listening for a TelephonyManager.ACTION_PHONE_STATE_CHANGED action. Discussion If you want to do something when the phone rings you have to implement a broadcast receiver which listens for the TelephonyManager.ACTION_PHONE_STATE_CHANGED intent action. This is a broadcast intent action indicating that the call state (cellular) on the device has changed. 1. Create a class IncomingCallInterceptor which extends BroadcastReceiver. 2. Override the onReceive method to handle incoming broadcast messages. 3. The EXTRA_STATE intent extra in this case indicates the new call state. 4. If (and only if) the new state is RINGING, a second intent extra EXTRA_INCOMING_NUMBER provides the incoming phone number as a String. 5. We extract the number information from the EXTRA_INCOMING_NUMBER intent extra. Note: Additionally you can act on a state change to OFFHOOK or IDLE when the user picks up the phone or ends/rejects the phone call respectively. 1 2 3 4 5 6 7 8 package nl.codestone.cookbook.incomingcallinterceptor; import import import import import android.content.BroadcastReceiver; android.content.Context; android.content.Intent; android.telephony.TelephonyManager; android.widget.Toast;
?

1 of 4

10/26/2011 5:00 PM

Do something when the phone rings | Android Labs

http://androidlabs.org/short-experiments/broadcast-receivers/do-somethi...

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

public class IncomingCallInterceptor extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); String msg = "Phone state changed to " + state;

if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) { String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_I msg += ". Incoming number is " + incomingNumber;

// TODO This would be a good place to "Do something when the phone rin } Toast.makeText(context, msg, Toast.LENGTH_LONG).show(); } }

6. We have to register our IncomingCallInterceptor as a <receiver> within the <application> element in the AndroidManifest.xml file. 7. We register an <intent-filter> 8. and an <action value which registers our receiver to listen for TelephonyManager.ACTION_PHONE_STATE_CHANGED broadcast messages. 9. Finally we have to register a <uses-permission> so we are allowed to listen to phone state changes. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="nl.codestone.cookbook.incomingcallinterceptor" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="3" />
?

<application android:icon="@drawable/icon" android:label="Incoming Call Interc <receiver android:name="IncomingCallInterceptor"> <intent-filter> <action android:name="android.intent.action.PHONE_STATE"/> </intent-filter> </receiver> </application> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> </manifest>

If all is well you should see something like this when the phone rings:

2 of 4

10/26/2011 5:00 PM

Do something when the phone rings | Android Labs

http://androidlabs.org/short-experiments/broadcast-receivers/do-somethi...

What happens if two receivers listen for phone state changes? In general, a broadcast message is just that, a message which is sent out to many receivers at the same time. This is the case for normal broadcast messages which is used to send out the ACTION_PHONE_STATE_CHANGED intent as well. All receivers of the broadcast are run in an undefined order, often at the same time and for that reason order is not applicable. In other cases the system sends out ordered broadcast which is described in more detail in the Recipe Process outgoing calls. See Also Process outgoing calls http://developer.android.com/reference/android/content/BroadcastReceiver.html http://developer.android.com/reference/android/telephony /TelephonyManager.html#ACTION_PHONE_STATE_CHANGED Source Download URL: https://github.com/downloads/jpelgrim/androidcookbook/IncomingCallInterceptor.zip

3 Votes Home About Lab rules Available Labs Short Experiments Android Labs. Say something clever here. Or not. You decide.

3 of 4

10/26/2011 5:00 PM

Do something when the phone rings | Android Labs

http://androidlabs.org/short-experiments/broadcast-receivers/do-somethi...

Proudly powered by: WordPress, Nedlinux and

4 of 4

10/26/2011 5:00 PM

Vous aimerez peut-être aussi