My situation is that I want to call a particular method to get called from ClassA from inside my ClassB which extends the BroadcastReceiver class. I understand how to do this from inside another class file etc and how to setup the BroadcastReceiver, or so I thought.
Below is the class file that extends the BroadcastReceiver.
public class WatchPhone extends BroadcastReceiver {
private ClassA classA;
@Override
public void onReceive(Context context, Intent BackToMain) {
// TODO Auto-generated method stub
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
int state = telephony.getCallState();
switch(state) {
case TelephonyManager.CALL_STATE_RINGING:
Log.d("TestServiceReceiver", "IDLE");
classA.IwantToDoThis();
break;
}
}
The method that I am trying to call does work great in the application if I manually request it.
In my AndroidManifest.xml I added this
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
and this at the bottom just above the closing application tag.
<receiver android:name=".WatchPhone">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
When the phone rings, the application crashes then closes. Any ideas on what I should do.
I can see one problem immediately: you declared an instance of ClassA but did not instantiate it. That means classA = null and you are probably getting a NullPointerException when you use it.
Try this instead:
Alternately, you can instantiate it near the top of onReceive():