Manipulating Incoming & Outgoing Calls Tutorial For Android

This code simply implement PhoneStateListener. When call state changed onCallStateChanged function call and toast will appear on screen showing status. You can write your own code instead of toast for what you want to do.

You can download complete source code from here.

IncomingCallReciever.java

public class IncomingCallReciever extends BroadcastReceiver {

	private Context mContext;
	private Intent mIntent;

	@Override
	public void onReceive(Context context, Intent intent) {
		mContext = context;
		mIntent = intent;
		TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		int events = PhoneStateListener.LISTEN_CALL_STATE;
		tm.listen(phoneStateListener, events);
	}

	private final PhoneStateListener phoneStateListener = new PhoneStateListener() {
		@Override
		public void onCallStateChanged(int state, String incomingNumber) {
			String callState = "UNKNOWN";
			switch (state) {
			case TelephonyManager.CALL_STATE_IDLE:
				callState = "IDLE";
				break;
			case TelephonyManager.CALL_STATE_RINGING:
				// -- check international call or not.
				if (incomingNumber.startsWith("00")) {
					Toast.makeText(mContext,"International Call- " + incomingNumber,Toast.LENGTH_LONG).show();
					callState = "International - Ringing (" + incomingNumber+ ")";
				} else {
					Toast.makeText(mContext, "Local Call - " + incomingNumber, Toast.LENGTH_LONG).show();
					callState = "Local - Ringing (" + incomingNumber + ")";
				}
				break;
			case TelephonyManager.CALL_STATE_OFFHOOK:
				String dialingNumber = mIntent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
				if (dialingNumber.startsWith("00")) {
					Toast.makeText(mContext,"International - " + dialingNumber,Toast.LENGTH_LONG).show();
					callState = "International - Dialing (" + dialingNumber+ ")";
				} else {
					Toast.makeText(mContext, "Local Call - " + dialingNumber,Toast.LENGTH_LONG).show();
					callState = "Local - Dialing (" + dialingNumber + ")";
				}
				break;
			}
			Log.i(">>>Broadcast", "onCallStateChanged " + callState);
			super.onCallStateChanged(state, incomingNumber);
		}
	};

}

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="com.geniteam.broadcast" android:versionCode="1"
	android:versionName="1.0">
	<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
	<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
	<application android:icon="@drawable/icon" android:label="@string/app_name"
		android:debuggable="true">
		<receiver android:name=".IncomingCallReciever"
			android:enabled="true">
			<intent-filter>
				<action android:name="android.intent.action.PHONE_STATE"></action>
				<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
			</intent-filter>
		</receiver>
	</application>
	<uses-sdk android:minSdkVersion="4" />
</manifest>

Other Links:

http://www.krvarma.com/posts/android/detecting-incoming-and-outgoing-calls-in-android/

http://www.krvarma.com/posts/android/android-network-connectivity-broadcastreceiver/

free counters

Advertisements
This entry was posted in Android, Tutorials and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s