0

I have an app in which I have placed a switch toggle button. The purpose of my app is that whenever the switch button is set to ON, all incoming calls should be blocked. I am able to block all calls but I am unable to block when the switch is set to ON.

I have created a class called MyClassReceiver that extends Broadcast Receiver, all the call blocking is done in this class. I have added receiver code in Manifest file that works perfectly.

This is the code I am using:-

MainActivity.java

package com.chinmay.smsender;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;

public class MainActivity extends Activity {

public Switch mySwitch;
//private LinearLayout bgElement;
private AudioManager mAudio;
boolean switchChecked;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mySwitch = (Switch) findViewById(R.id.switch1);
    //bgElement = (LinearLayout) findViewById(R.id.container);
    mAudio = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);

    mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener(){

        @Override
        public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
            if(isChecked) {
                mAudio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                broadcastIntent(true);
                //bgElement.setBackgroundColor(Color.GREEN);
            } else {
                mAudio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                broadcastIntent(false);
                //bgElement.setBackgroundColor(Color.RED);
            } 
        }

    });
}

public void broadcastIntent(boolean isChecked) {
    Intent intent = new Intent();
    intent.setAction("com.chinmay.CUSTOM_INTENT");
    intent.putExtra("switchChecked", isChecked);
    sendBroadcast(intent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

MyClassReceiver.java

package com.chinmay.smsender;

import java.lang.reflect.Method;

import com.android.internal.telephony.ITelephony;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;


public class MyClassReceiver extends BroadcastReceiver {
private ITelephony telephonyService;
Context context = null;
boolean isChecked;

@Override
public void onReceive(Context context, Intent intent) {

    TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    isChecked = intent.getExtras().getBoolean("switchChecked");

    if(isChecked) {
        Toast.makeText(context, "Blocking calls", Toast.LENGTH_LONG).show();

        if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            //String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            //Toast.makeText(context, "Call from: "+incomingNumber, Toast.LENGTH_LONG).show();

            try {
                   Class<?> c = Class.forName(telephony.getClass().getName());
                   Method m = c.getDeclaredMethod("getITelephony");
                   m.setAccessible(true);
                   telephonyService = (ITelephony) m.invoke(telephony);
                   //telephonyService.silenceRinger();
                   telephonyService.endCall();
            } catch (Exception e) {
                   e.printStackTrace();
            }

        }
    } else if(isChecked == false) {
        Toast.makeText(context, "Blocking calls off", Toast.LENGTH_LONG).show();
    }

    /* else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE) || intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
        Toast.makeText(context, "Detected Call Hangup Event.", Toast.LENGTH_LONG).show();
    }*/
}

}

Android Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chinmay.smsender"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.chinmay.smsender.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>

    <receiver android:name="com.chinmay.smsender.MyClassReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
            <action android:name="com.chinmay.CUSTOM_INTENT" />
        </intent-filter>
    </receiver>    
</application>

</manifest>

This is the error I get if I run this code

10-31 13:03:26.283: E/AndroidRuntime(759): FATAL EXCEPTION: main
10-31 13:03:26.283: E/AndroidRuntime(759): Process: com.chinmay.smsender, PID: 759
10-31 13:03:26.283: E/AndroidRuntime(759): java.lang.RuntimeException: Unable to start receiver com.chinmay.smsender.MyClassReceiver: java.lang.NullPointerException
10-31 13:03:26.283: E/AndroidRuntime(759):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2441)
10-31 13:03:26.283: E/AndroidRuntime(759):  at android.app.ActivityThread.access$1700(ActivityThread.java:139)
10-31 13:03:26.283: E/AndroidRuntime(759):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
10-31 13:03:26.283: E/AndroidRuntime(759):  at android.os.Handler.dispatchMessage(Handler.java:102)
10-31 13:03:26.283: E/AndroidRuntime(759):  at android.os.Looper.loop(Looper.java:136)
10-31 13:03:26.283: E/AndroidRuntime(759):  at android.app.ActivityThread.main(ActivityThread.java:5086)
10-31 13:03:26.283: E/AndroidRuntime(759):  at java.lang.reflect.Method.invokeNative(Native Method)
10-31 13:03:26.283: E/AndroidRuntime(759):  at java.lang.reflect.Method.invoke(Method.java:515)
10-31 13:03:26.283: E/AndroidRuntime(759):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-31 13:03:26.283: E/AndroidRuntime(759):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-31 13:03:26.283: E/AndroidRuntime(759):  at dalvik.system.NativeStart.main(Native Method)
10-31 13:03:26.283: E/AndroidRuntime(759): Caused by: java.lang.NullPointerException
10-31 13:03:26.283: E/AndroidRuntime(759):  at com.chinmay.smsender.MyClassReceiver.onReceive(MyClassReceiver.java:49)
10-31 13:03:26.283: E/AndroidRuntime(759):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2434)
10-31 13:03:26.283: E/AndroidRuntime(759):  ... 10 more

I think that because I am sending 2 intents to MyClassReceiver, I am getting this error.

Please tell how can I get to know the state of switch button in MyClassReceiver itself and block every call if switch is on.

gegobyte
  • 4,945
  • 10
  • 46
  • 76
  • you are initializing context = null in class. just declare it in class and initilize in onreceive method it may work.. – M S Gadag Oct 31 '14 at 11:49
  • @MSGadag I have already tried it, it doesn't works. – gegobyte Oct 31 '14 at 12:08
  • insted contex, use classname.this in Toast... – M S Gadag Oct 31 '14 at 12:10
  • @MSGadag I have no problem with Toast, I just want to get the boolean state of switch button in Broadcast Receiver class. – gegobyte Oct 31 '14 at 13:01
  • I'm confused, why are you using broadcast receiver for this? You are within the same project, from one activity to a receiver. Does not appear to be necessary. Unless this was just sample you made to get point across. I would say just make your TelephoneHelper class with a method that handles it on switch changed. – Sam Feb 15 '18 at 17:49

1 Answers1

0

The boolean state of the switch widget can be accessed with the help of Shared Preferences. See the answer here:

How to get switch value in Android?

After getting the boolean state, we just have to check whether its true or not and if it is, then block call.

Community
  • 1
  • 1
gegobyte
  • 4,945
  • 10
  • 46
  • 76