为了检测时传出呼叫接通后,我尝试创建一个PhoneStateListener和监听TelephonyManager的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; } }现在,当我在设备中打出电话时,CALL_STATE_RINGING 将永远不会被调用。当另一行开始振铃时,我总是只将“ IDLE”的打印输出显示为“ OFFHOOK”,当呼叫被应答时,我什么也没有,而当通话结束时,我的打印输出再次显示为“ IDLE”。如何可靠地检测出何时在Android中应答了拨出电话,甚至有可能吗?
3 回答
- 3 回答
- 0 关注
- 695 浏览
添加回答
举报
0/150
提交
取消