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);
}
}
清单文件
<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>