電話狀態監視

1 篇文章 / 0 new
author
電話狀態監視
當有電話撥入或要撥出等等動作, 需要對程式進行某些處置時(如暫停程式,停止撥放音樂..), 其監視方法如下
PhoneStateListener phoneStateListener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        if (state == TelephonyManager.CALL_STATE_RINGING) {
            //電話撥入中
        } else if(state == TelephonyManager.CALL_STATE_IDLE) {
            //閒置中
        } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
            //A call is dialing, active or on hold
        }
        super.onCallStateChanged(state, incomingNumber);
    }
};//end PhoneStateListener
//註冊監聽程序
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr != null) {
    mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
Free Web Hosting