為了檢測(cè)時(shí)傳出呼叫接通后,我嘗試創(chuàng)建一個(gè)PhoneStateListener和監(jiān)聽(tīng)TelephonyManager的CALL_STATE_RINGING,CALL_STATE_OFFHOOK和CALL_STATE_IDLE,從這個(gè)問(wèn)題,但它似乎沒(méi)有工作時(shí),如下所述。首先,我在清單中注冊(cè)了以下權(quán)限:<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />然后,一個(gè)BroadcastReceiver叫OutCallLogger映入NEW_OUTGOING_CALL每當(dāng)呼出由事件:<receiver android:name=".listener.OutCallLogger"> <intent-filter> <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> </intent-filter></receiver>接下來(lái),我的實(shí)現(xiàn)OutCallLogger。我成立了一個(gè)叫布爾值noCallListenerYet,以避免在連接新PhoneStateListener的TelephonyManager時(shí)候onReceive()被調(diào)用。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; } }現(xiàn)在,當(dāng)我在設(shè)備中打出電話時(shí),CALL_STATE_RINGING 將永遠(yuǎn)不會(huì)被調(diào)用。當(dāng)另一行開始振鈴時(shí),我總是只將“ IDLE”的打印輸出顯示為“ OFFHOOK”,當(dāng)呼叫被應(yīng)答時(shí),我什么也沒(méi)有,而當(dāng)通話結(jié)束時(shí),我的打印輸出再次顯示為“ IDLE”。如何可靠地檢測(cè)出何時(shí)在Android中應(yīng)答了撥出電話,甚至有可能嗎?
- 3 回答
- 0 關(guān)注
- 723 瀏覽
添加回答
舉報(bào)
0/150
提交
取消