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;
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)) {
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.
android
Chinmay sarupria
source share