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);
}
}
}
try adding the @Override
before the onCallStateChange
method
I just wanted to add that Vairavan mentioned in this answer, that there was an internal change in how the PhoneStateListener is referenced. It is now held in a weak reference, which means that once the method returns, the PhoneStateListener instance is elegable for garbage collection. Therefore you should hold the reference in a class which will be persisted for the lifetime that you expect to listen for callbacks, such as an Activity
, Service
, or Application
classes.
See: https://github.com/aosp-mirror/platform_frameworks_base/commit/f5d7c587e86c64e657a15a12ad70e75d47c48d99#diff-5af2ac899de823cf60597e554bf67fe0.
精彩评论