Search code examples
androidmessageblockingphone-call

How to block/unblock all incoming/outgoing calls/text messages in android?


I am developing an android application for teenagers, they can not do/receive any incoming/outgoing calls/text messages during driving and also they can change status either he/she is driving or not. In my application I want to handle both situations Block/Unblock incoming/outgoing calls/text messages. If you have any idea related to this question than please share with me.

Thanks.


Solution

  • After 8 Hours R&D I found my Own Solution for my above question:

    Manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.deactivateimservicedemo"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="19" />
    
        <uses-feature android:name="android.hardware.telephony" />
    
        <uses-permission android:name="android.permission.READ_PHONE_STATE" />
        <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
        <uses-permission android:name="android.permission.CALL_PHONE" />
    
        <!-- For Incoming Text messages -->
    
        <uses-permission android:name="android.permission.RECEIVE_SMS" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.deactivateimservicedemo.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        tools:context="com.example.deactivateimservicedemo.MainActivity$PlaceholderFragment" >
    
        <Button
            android:id="@+id/btnActivate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Activate" />
    
        <Button
            android:id="@+id/btnDeactivate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Deactivate" />
    
    </LinearLayout>
    

    MainActivity.java

    package com.example.deactivateimservicedemo;
    
    import java.lang.reflect.Method;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.telephony.TelephonyManager;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    import com.android.internal.telephony.ITelephony;
    
    public class MainActivity extends ActionBarActivity {
    
        private Button btnActivate, btnDeactivate;
        private BroadcastReceiver receiver;
        private IntentFilter filter;
        private boolean isReceiverRegistered;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            btnActivate = (Button) findViewById(R.id.btnActivate);
            btnActivate.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    /*Register Receiver*/
                    registerReceiver(receiver, filter);
                    isReceiverRegistered = true;
                }
            });
    
            btnDeactivate = (Button) findViewById(R.id.btnDeactivate);
            btnDeactivate.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    /*UnRegister Receiver*/
                    if(isReceiverRegistered){
                        Log.i("Receiver is", "Register");
                        unregisterReceiver(receiver);
                    }else {
                        Log.i("Receiver is", "Not Register");
                    }
                }
            });
    
            filter = new IntentFilter();
            filter.addAction("android.intent.action.PHONE_STATE");
            filter.addAction("android.provider.Telephony.SMS_RECEIVED");
    
            receiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    String action = intent.getAction();
                    Log.i("Intent Action", action);
                    if(intent!=null){
                        if(action.equals("android.intent.action.PHONE_STATE")){
                            try {
                                TelephonyManager tm = (TelephonyManager) context
                                        .getSystemService(Context.TELEPHONY_SERVICE);
                                Class<?> 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.endCall();
                            } catch (Exception e) {
                                Log.e("Exception", e.toString());
                            }
                        }else if(action.equals("android.provider.Telephony.SMS_RECEIVED")){
                            Bundle extras = intent.getExtras();
                            if (extras != null) {
                                abortBroadcast();
                            }
                        }
                    }
                }
            };
        }
    
        @Override
        protected void onDestroy() {
            btnDeactivate.performClick();
            super.onDestroy();
        }
    }
    

    That's IT.