为了检测拨出电话何时得到应答,我尝试从这个问题PhoneStateListener
创建一个并监听TelephonyManager
's CALL_STATE_RINGING
,CALL_STATE_OFFHOOK
和, 但它似乎不起作用,如下所述。CALL_STATE_IDLE
首先,我在清单中注册了以下权限:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
然后,每当进行拨出呼叫时都会捕获事件的被BroadcastReceiver
调用OutCallLogger
:NEW_OUTGOING_CALL
<receiver android:name=".listener.OutCallLogger">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
接下来,我的OutCallLogger
. 我设置了一个布尔值noCallListenerYet
,以避免在调用时附加一个新PhoneStateListener
的。TelephonyManager
onReceive()
public class OutCallLogger extends BroadcastReceiver {
private static boolean noCallListenerYet = true;
@Override
public void onReceive(final Context context, Intent intent) {
number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
if (noCallListenerYet) {
final TelephonyManager tm = (TelephonyManager) context.getSystemService(
Context.TELEPHONY_SERVICE);
tm.listen(new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Log.d(This.LOG_TAG, "RINGING");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(This.LOG_TAG, "OFFHOOK");
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.d(This.LOG_TAG, "IDLE");
break;
default:
Log.d(This.LOG_TAG, "Default: " + state);
break;
}
}
}, PhoneStateListener.LISTEN_CALL_STATE);
noCallListenerYet = false;
}
}
}
现在,当我在我的设备中拨打电话时,CALL_STATE_RINGING
永远不会调用。当另一条线路开始响铃时,我总是只打印“IDLE”到“OFFHOOK”,接听电话时什么也没有,当通话结束时再次打印“IDLE”。
我怎样才能可靠地检测到在 Android 中何时接听了拨出电话,或者这是否可能?