Click here to Skip to main content
11,721,053 members (83,090 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: Android Calls
import java.lang.reflect.Method;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class PhoneCallStateListener extends PhoneStateListener {	
	private Context context;
	public PhoneCallStateListener(Context context){
		this.context = context;
	public void onCallStateChanged(int state, String incomingNumber) {	
	SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(context);
	switch (state) {
		case TelephonyManager.CALL_STATE_RINGING:		
		  String block_number = prefs.getString("block_number", null);
				AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);	
				//Turn ON the mute
		audioManager.setStreamMute(AudioManager.STREAM_RING, true);					
				TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		try {
		Toast.makeText(context, "in"+block_number, Toast.LENGTH_LONG).show();
		Class clazz = Class.forName(telephonyManager.getClass().getName());
		Method method = clazz.getDeclaredMethod("getITelephony");
		ITelephony telephonyService = (ITelephony) method.invoke(telephonyManager);		
		//Checking incoming call number
		System.out.println("Call "+block_number);
		if (incomingNumber.equalsIgnoreCase("+91"+block_number)) {
		//telephonyService.silenceRinger();//Security exception problem
		 telephonyService = (ITelephony) method.invoke(telephonyManager);
		System.out.println(" in  "+block_number);
		} catch (Exception e) {
		Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
			//Turn OFF the mute		
		audioManager.setStreamMute(AudioManager.STREAM_RING, false);
		case PhoneStateListener.LISTEN_CALL_STATE:
		super.onCallStateChanged(state, incomingNumber);
Though this code i am able to block incoming calls but problems it rings for fraction of seconds before disconnection of time how can i directly disconnect phone without single ringing
Posted 20-Feb-12 20:16pm
cute_friend7077 at 8-Mar-13 0:07am
code is working on emulator but not on real device having os ICS 4.0.4
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

public class myActivity extends BroadcastReceiver
public void onReceive(Context context, Intent intent)

String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
setResultData(null); //It will terminate the call
Toast.makeText(context, "Call Terminated" + number, 5000).show();


Hope it helps
Sahil Mahajan(eMM jAY)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 500
1 OriginalGriff 334
2 Mika Wendelius 160
3 F-ES Sitecore 140
4 Maciej Los 80
0 Sergey Alexandrovich Kryukov 2,199
1 OriginalGriff 959
2 F-ES Sitecore 640
3 Maciej Los 609
4 Richard MacCutchan 540

Advertise | Privacy | Mobile
Web02 | 2.8.150901.1 | Last Updated 25 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100