Android

How to Detect Call State

September 3rd 2009 | Posted by msino

Some applications need to do clever things whenever there’s an incoming or outgoing call. There’s a great example of this in the source code for ‘five’ (an app providing remote access to your PC’s music collection) where the music PlaylistService listens for incoming calls so it can temporarily pause playback…

TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

and…

private PhoneStateListener mPhoneListener = new PhoneStateListener()
{
        public void onCallStateChanged(int state, String incomingNumber)
        {
                try {
                        switch (state)
                        {
                        case TelephonyManager.CALL_STATE_RINGING:
...
                                break;
                        case TelephonyManager.CALL_STATE_OFFHOOK:
...
                                break;
                        case TelephonyManager.CALL_STATE_IDLE:
...
                                break;
                        default:
                                Log.d(TAG, "Unknown phone state=" + state);
                        }
                } catch (RemoteException e) {}
        }
};

In many applications, the next stage is to do something based on an incoming telephone number. The number is given by the String incomingNumber parameter shown above.

Also don’t forget the following in your manifest…

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

2 Responses to “How to Detect Call State”

Познавательно

Get a FREE Apple iPhone or iPod…

The myTouch 3G is now available in T- Mobile stores across the US, joining the G1 as the second Android handset for the carrier. More importantly, it\’s only the second Android phone to be released by a carrier in the country. We\’re not counting t…

Leave a Reply:

You must be logged in to post a comment.