Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to block all incoming calls in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 476
    Comment on it

    To block all incomming calls in your android device we have to use a class Broadcast Receiver. Broadcast Receivers simply respond to broadcast events from other applications or from system itself. All the events are notified once the event occured by Android runtime. onReceive() method where each message is received as a Intent object parameter.


    Telephony Manager class is used to access information about the telephony services. Subscriber information as well as telephony services and states are determined by using methods of Telephony service class. Applications can also register a listener to receive notification of telephony state changes.

    public class Receiver extends BroadcastReceiver {
        Context context;
    
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            Bundle myBundle = intent.getExtras();
            if (myBundle != null)
            {
                try
                {
                    if (intent.getAction().equals("android.intent.action.PHONE_STATE"))
                    {
                        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    
                        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
                        {
                            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();
                }
            }
        }
    }
    

    AndroidManifest.xml

    <!--Permission to be used to block the incoming calls-->
    
        <uses-permission android:name="android.permission.CALL_PHONE" />
        <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    
    <!--receiver-->
    <receiver android:name=".Receiver"  >
                <intent-filter android:priority="1000" >
                    <action android:name="android.intent.action.PHONE_STATE" />
                </intent-filter>
            </receiver>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: