我找到了一种方法来实现这个…发布它为“下一个人”.
简而言之,手机状态在三种状态之间移动:
>’空闲’ – 您没有使用手机
>’铃声’ – 来电正在进行中
>’OFF_HOOK’ – 电话没电了
当广播“RINGING”状态时,紧接着是IDLE或OFF_HOOK,以将状态恢复为预呼入状态.
把它们放在一起,你最终得到这个:
package com.telephony;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
public class PhoneStateChangedReciever extends BroadcastReceiver {
private static String lastKnownPhoneState = null;
@Override
public void onReceive(Context context, Intent intent) {
if(intent != null && intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
//State has changed
String newPhoneState = intent.hasExtra(TelephonyManager.EXTRA_STATE) ? intent.getStringExtra(TelephonyManager.EXTRA_STATE) : null;
//See if the new state is 'ringing'
if(newPhoneState != null && newPhoneState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
//If the last known state was 'OFF_HOOK', the phone was in use and 'Line 2' is ringing
if(lastKnownPhoneState != null && lastKnownPhoneState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
...
}
//If it was anything else, the phone is free and 'Line 1' is ringing
else {
...
}
}
lastKnownPhoneState = newPhoneState;
}
}
}