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(); 
  } 
 }
} 

33 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
  13. Hi,
    can you pls send me complete project? iiec@seznam.cz

    Thx

    ReplyDelete
  14. Can you please send me the entire code to seguharshitha@gmail.com

    ReplyDelete
  15. Can you please send me the source code for the project to ranaamin15544@gmail.com

    ReplyDelete
  16. ITelephony cannot resolve .Please send me source code sandun.jaya@gmail.com

    ReplyDelete
  17. Microsoft, the leading software developer has given mobile phone users a lot of free Windows phone apps, one of them is the Sudoku app. For centuries, Sudoku was only printed in traditional newspapers and people couldn't wait for the next day's paper to fill in the game at the last pages of the newspapers. But in this era of advanced technology, you can play Sudoku at any time you want and frequently as you desire. Free Windows phone Sudoku app is now available to you from Windows phone free app market Check This Out

    ReplyDelete
  18. ITelephony cannot resolve .Please send me source code murugammalvalli10@gmail.com

    ReplyDelete
  19. I real glad to find this web site on bing, just what I was searching for : D likewise saved to bookmarks . 토토사이트

    ReplyDelete
  20. It is especially decent, though look into the tips during this home address. One Shot Keto is fundraising for Pips Hope & Support

    ReplyDelete
  21. I like this site its a master peace ! Glad I detected this on google . buy hydrocodone online

    ReplyDelete
  22. Just wanna comment that you have a very nice site, I the style and design it actually stands out. GLD Partners

    ReplyDelete
  23. Spot lets start work on this write-up, I truly feel this fabulous website requirements a great deal more consideration. I’ll likely to end up once more to study additional, many thanks for that info. facebook login

    ReplyDelete
  24. If you are thinking about purchasing a security camera, it's important to cover all the different types of cameras. This article covers the benefits and drawbacks of c-mount security cameras. how to build a mini spy camera

    ReplyDelete
  25. I’ve read a few just right stuff here. Definitely worth bookmarking for revisiting. I wonder how a lot effort you put to create this type of excellent informative site. buy steroids

    ReplyDelete
  26. dog crates made from ABS Plastic can withstand those aggressive dogs” workout clothes

    ReplyDelete
  27. I just done mine and that i was looking for several design suggestions and you afflicted me with a few. rapid application development methodology

    ReplyDelete
  28. I was suggested this blog by way of my cousin. I’m no longer sure whether or not this put up is written by him as nobody else realize such detailed about my trouble. You’re wonderful! Thanks! buy steroids online

    ReplyDelete
  29. The the next occasion Someone said a blog, I really hope who’s doesnt disappoint me about this blog. After all, I know it was my choice to read, but I just thought youd have some thing intriguing to talk about. All I hear is really a number of whining about something you could fix in case you werent too busy seeking attention. buy hgh

    ReplyDelete
  30. well of course, everyone loves to get rich but not everyone would love to do hard work” buy hgh

    ReplyDelete
  31. i like war movies and inglourious basterds is one of the movies that i really love,, wavob.com

    ReplyDelete