我通过扩展广播接收器实现了电话接收器。
public abstract class PhonecallReceiver extends BroadcastReceiver {
private static final String TAG = PhonecallReceiver.class.getSimpleName();
//The receiver will be recreated whenever android feels like it. We need a static variable to remember data between instantiations
private static int lastState = TelephonyManager.CALL_STATE_IDLE;
private static Date callStartTime;
private static boolean isIncoming;
protected static String savedNumber; //because the passed incoming is only valid in ringing
@Override
public void onReceive(Context context, Intent intent) {
MyApp.printLog(TAG, " ...intent.getAction()= " + intent.getAction());
//We listen to two intents. The new outgoing call only tells us of an outgoing call. We use it to get the number.
if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
MyApp.printLog(TAG, "NEW_OUTGOING_CALL.number = " + savedNumber);
} else {//TODO: PUT INCOMING CALL HERE..OTHERWISE, TROUBLE
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
int state = 0;
if (stateStr != null && stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
state = TelephonyManager.CALL_STATE_IDLE;
} else if (stateStr != null && stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
state = TelephonyManager.CALL_STATE_OFFHOOK;
} else if (stateStr != null && stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
state = TelephonyManager.CALL_STATE_RINGING;
}
//following fix for outgoing calls because above if block can never be executed when called programmatically
savedNumber = intent.getExtras().getString("PHONE_NUMBER");
if (savedNumber == null) {
//savedNumber = intent.getStringExtra("PHONE_NUMBER");
savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
}
MyApp.printLog(TAG, "else non-NEW_OUTGOING_CALL.number = " + number + " intent.getAction()= " + intent.getAction() + " stateStr = " + stateStr + " savedNumber = " + savedNumber);
onCallStateChanged(context, state, number);
savedNumber = null;
}
}
//Derived classes should override these to respond to specific events of interest
protected void onIncomingCallStarted(Context ctx,int state, String number, Date start) {
}
protected void onOutgoingCallStarted(Context ctx,int state, String number, Date start) {
}
protected void onIncomingCallEnded(Context ctx,int state, String number, Date start, Date end) {
}
protected void onOutgoingCallEnded(Context ctx,int state, String number, Date start, Date end) {
}
protected void onMissedCall(Context ctx,int state, String number, Date start) {
}
//Deals with actual events
//Incoming call- goes from IDLE to RINGING when it rings, to OFFHOOK when it's answered, to IDLE when its hung up
//Outgoing call- goes from IDLE to OFFHOOK when it dials out, to IDLE when hung up
public void onCallStateChanged(Context context, int state, String number) {
if (lastState == state) {
//No change, debounce extras
return;
}
MyApp.printLog(TAG, " onCallStateChanged.state =" + state + " number =" + number);
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
isIncoming = true;
callStartTime = new Date();
savedNumber = number;
onIncomingCallStarted(context, state,number, callStartTime);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//Transition of ringing->offhook are pickups of incoming calls. Nothing done on them
if (lastState != TelephonyManager.CALL_STATE_RINGING) {
isIncoming = false;
callStartTime = new Date();
//following fixes for outgoing calls because above if block in onrecieve can never be executed when called programmatically
if (savedNumber == null)
savedNumber = number;
if (savedNumber == null) {//still null, then
/* TODO
if(SmartMonitorService.listener != null) {
savedNumber = SmartMonitorService.listener.getOutgoingNumber();
SmartMonitorService.listener.setOutgoingNumber(null);
}
*/
}
onOutgoingCallStarted(context,state, savedNumber, callStartTime);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
//Went to idle- this is the end of a call. What type depends on previous state(s)
if (lastState == TelephonyManager.CALL_STATE_RINGING) {
//Ring but no pickup- a miss
if (savedNumber == null)
savedNumber = number;
onMissedCall(context,state, savedNumber, new Date());
} else if (isIncoming) {
if (savedNumber == null)
savedNumber = number;
onIncomingCallEnded(context,state, savedNumber, callStartTime, new Date());
} else {
//following fix for outgoing calls because above if block can never be executed when called programmatically
if (savedNumber == null)
savedNumber = number;
onOutgoingCallEnded(context, state,savedNumber, callStartTime, new Date());
}
break;
}
lastState = state;
}
}
现在,此接收器仅在Android Oreo,Nougat上的拨出呼叫期间触发。在棉花糖测试中,br触发了来电,未接来电和外拨电话。
奥利奥(Oreo)对隐式意图https://developer.android.com/guide/components/broadcast-exceptions应用了限制。无需更改手机状态。
您能帮我了解一下Android Oreo中与此相关的内容吗?
谢谢。