You can block phone call from you activity/app.we can block phone by two
way either block call for specific activity or block call for complete
device.
- Broadcast receiver to handle Incoming calls:
- following code is just sample code, it will block all incoming calls, at line number 19 you will get the cell phone number of incoming call.use that number and apply your logic to block specific calls
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
public class BlockCallReceiver extends BroadcastReceiver { Context context; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Log.d("Call 1","work"); Toast.makeText(context," Receiver start - call1 ",Toast.LENGTH_LONG).show(); Bundle myBundle = intent.getExtras(); if (myBundle != null) { System.out.println("--------Not null-----"); try { if (intent.getAction().equals("android.intent.action.PHONE_STATE")) { String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); System.out.println("--------in state-----"); if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { // Incoming call String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); System.out.println("--------------my number---------" + incomingNumber); // this is main section of the code,. could also be use for particular number. // Get the boring old TelephonyManager. TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); // Get the getITelephony() method Class<?> classTelephony = Class.forName(telephonyManager.getClass().getName()); Method methodGetITelephony = classTelephony.getDeclaredMethod("getITelephony"); // Ignore that the method is supposed to be private methodGetITelephony.setAccessible(true); // Invoke getITelephony() to get the ITelephony interface Object telephonyInterface = methodGetITelephony.invoke(telephonyManager); // Get the endCall method from ITelephony Class<?> telephonyInterfaceClass = Class.forName(telephonyInterface.getClass().getName()); Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("endCall"); // Invoke endCall() methodEndCall.invoke(telephonyInterface); } } } catch (Exception ex) { // Many things can go wrong with reflection calls ex.printStackTrace(); } } } }
- Block Call for Specific activity:
- when you register the call blocking broadcast receiver to specific activity then your receiver only work for that specific activity when that activity is close then broadcast receiver stop the working
- To register broadcast receiver to Activity just refer following lines code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
public class MainActivity extends Activity { IntentFilter filter = new IntentFilter("android.intent.action.PHONE_STATE"); BlockCallReceiver blockCallReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override protected void onResume() { super.onResume(); blockCallReceiver = new BlockCallReceiver(); registerReceiver(blockCallReceiver, filter); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(blockCallReceiver); } }
- Block Call for Application:
- when you register your receiver in manifest for application then your
all call will be blocked even when your application is close .
- In application tag insert the receiver tag to register broadcast receiver
1 2 3 4 5
<receiver android:name=".BlockCallReceiver"> <intent-filter> <action android:name="android.intent.action.PHONE_STATE"></action> </intent-filter> </receiver>
No comments:
Post a Comment