While debugging the code below, BroadcastReceiverCustom is being called but PhoneStateListenerCustom is not.
For now I and doing required action in BroadcastReceiverCustom only, but maynot be best place to do it. Any suggestions why PhoneStateListener not being called?? Already spent lot of time for possible reasons, no idea?? Manifest file is all correct with correct permissions. I see no runtime exceptions.
BroadcastReceiverCustom.java
public class BroadcastReceiverCustom extends BroadcastReceiver {
private static final String TAG = "BroadcastReceiverCustom";
@Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListenerCustom phoneStateListenerCustom = new PhoneStateListenerCustom();
telephony.listen(phoneStateListenerCustom, PhoneStateListener.LISTEN_CALL_STATE);
}
}
PhoneStateListenerCustom.java
public class PhoneStateListenerCustom extends PhoneStateListener {
private static final String TAG = "PhoneStateListenerCustom";
public void onCallStateChange(int state, String incomingNumber){
Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.d(TAG, "RINGING");
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
Manifest file
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<receiver android:name=".BroadcastReceiverCustom">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
you should not instantiate the TelephonyManager and PhoneStateListener in the BroadcastReceiver. here is some example code:
in Activity:
EndCallListener callListener = new EndCallListener;
TelephonyManager mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
private class EndCallListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if(TelephonyManager.CALL_STATE_RINGING == state) {
Log.i(LOG_TAG + "Listener", "RINGING, number: " + incomingNumber);
}
if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
Log.i(LOG_TAG + "Listener", "OFFHOOK, number: " + incomingNumber);
}
if(TelephonyManager.CALL_STATE_IDLE == state) {
Log.i(LOG_TAG + "Listener", "IDLE, number: " + incomingNumber);
}
}
}