您应该创建一个 BroadcastReceiver:在 TelephonyManager.CALL_STATE_OFFHOOK 的情况下:检查先前的状态是 CALL_STATE_RINGING 还是 CALL_STATE_IDLE(例如通过在两种情况下设置不同的标志)。在后一种情况下,继续打开您的应用程序,:::
public class CallReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_RINGING)) {
// Phone number
// Ringing state
// This code will execute when the phone has an incoming call
} else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE)
|| intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_OFFHOOK)) {
// This code will execute when the call is answered or disconnected
}
}
}
您应该注册您的应用程序以在清单中收听这些意图:
<receiver android:name=".CallReciever">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>