Sunday, August 19, 2012

Block all calls programmatically



Block all calls
Requirement:
Block incoming and out going calls.

Steps:
Register a broadcast receiver to monitor calls.
In AndroidManifest.xml file write:
<receiver android:name="MyPhoneReceiver">
    <intent-filter android:priority="100">
        <action android:name="android.intent.action.PHONE_STATE"/>
        <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
    </intent-filter>
</receiver>

Create a broadcast Receiver to catch the "call" related broadcast.

public class ProcessCall extends BroadcastReceiver { 
 @Override 
 public void onReceive(Context context, Intent intent) { 
  //Create object of Telephony Manager class. 
  TelephonyManager telephony = (TelephonyManager)  context.getSystemService(Context.TELEPHONY_SERVICE); 
  //Assign a phone state listener. 
  CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener (context); 
  telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); 
 } 
} 
Create a custom phone state listener.
import java.lang.reflect.Method; 
import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import com.CallLogApp.helper.BlockNumberHelper;
import com.CallLogApp.util.UDF;
import com.android.internal.telephony.ITelephony;
 
 
public class CustomPhoneStateListener extends PhoneStateListener {
 Context context; 
  
 public CustomPhoneStateListener(Context context) { 
  super(); 
  this.context = context; 
 } 
  
 @Override 
 public void onCallStateChanged(int state, String callingNumber) 
 { 
  super.onCallStateChanged(state, callingNumber); 
  switch (state) { 
    case TelephonyManager.CALL_STATE_IDLE: 
    break; 
    case TelephonyManager.CALL_STATE_OFFHOOK: 
    //handle out going call
     endCallIfBlocked(callingNumber); 
    break; 
    case TelephonyManager.CALL_STATE_RINGING: 
    //handle in coming call
     endCallIfBlocked(callingNumber); 
    break; 
    default: 
    break; 
   }  
  } 
 
 private void endCallIfBlocked(String callingNumber) { 
  try { 
 
   // Java reflection to gain access to TelephonyManager's 
   // ITelephony getter 
   TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
   c = Class.forName(tm.getClass().getName()); 
   Method m = c.getDeclaredMethod("getITelephony"); 
   m.setAccessible(true); 
    com.android.internal.telephony.ITelephony telephonyService = (ITelephony) m.invoke(tm);
   telephonyService = (ITelephony) m.invoke(tm); 
   //
    telephonyService.silenceRinger(); 
    telephonyService.endCall(); 
  } catch (Exception e) { 
   e.printStackTrace(); 
  } 
 }
} 

13 comments:

  1. Pls send for me completed source code, thanks
    toannd@hqsoft.com.vn

    ReplyDelete
  2. Please send me Actual Code
    Thanks for it :)

    ReplyDelete
  3. Can you please send me the entire source code as zip?
    Thanks,
    michael.hanna@utexas.edu

    ReplyDelete
  4. can you please send complete source code as rar file?
    krunal11400@gmail.com
    thanks

    ReplyDelete
  5. could you plz send source code to sarathi1989@gmail.com

    ReplyDelete
  6. could you plz send source code to sarathi1989@gmail.com

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. actually my code is working but my phone all outgoing call rejected

    ReplyDelete
  9. Can you please send me the entire source code as zip? at neethupbaby95@gmail.com

    ReplyDelete
  10. Can you send me the entire source code please? rauteru@gmail.com

    ReplyDelete
  11. can you please send complete source code? malikneha747@gmail.com

    ReplyDelete
  12. Football today happens to be a whole lot more than what goes on on the field of play. It has definitely become more than just a game, even metamorphosed in to a religion for some but that would not be my concern today, rather I would be particularly more concerned about the politics behind the game. It has become surrounded with so much intrigues that one is left wondering about what may happen next. Dwelling on the politics of the game might even sound a bit ambiguous because we are almost less concerned about the elections or who gets put in what office or not, but the maverick side to this whole free business calls called soccer.

    ReplyDelete