How to block an incoming call when the switch widget is on?

I have an application in which I placed the switch toggle button. The purpose of my application is that whenever the switch button is set to ON, all incoming calls should be blocked. I can block all calls, but I cannot block when the switch is set to ON.

I created a class called MyClassReceiver that extends the broadcast receiver, all call blocking is done in this class. I added the receiver code in the manifest file, which works fine.

This is the code I'm 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 since I send 2 intentions to MyClassReceiver, I get this error.

Tell me how I can find out the state of the switch button in MyClassReceiver and block each call if the switch is on.

0
android
source share
1 answer

The boolean state of the switch widget can be obtained using the general settings. See the answer here:

How to get switch value in Android?

After receiving the logical state, we just need to check if this is true or not, and if so, then block the call.

0
source share

All Articles