Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 進階開發

Android 通話狀態監聽的正確實作方式

編輯:進階開發

開發應用程式時,常需要監聽來電以執行動作,例如來電時暫停音樂播放,通話結束後自動恢復。在 Android 平台上,這件事可以透過 TelephonyManager 與 PhoneStateListener 完成。

取得 TelephonyManager 實例

TelephonyManager 作為一個 Service 介面提供給開發者查詢電話相關資訊,例如 IMEI、行號等:

TelephonyManager mTelephonyMgr = (TelephonyManager) this
        .getSystemService(Context.TELEPHONY_SERVICE);

PhoneStateListener 的監聽項目

PhoneStateListener 是相當有用的監聽器,可用來追蹤電話狀態與服務狀態,提供的方法包含:

public void onCallForwardingIndicatorChanged(boolean cfi)
public void onCallStateChanged(int state, String incomingNumber)
public void onCellLocationChanged(CellLocation location)
public void onDataActivity(int direction)
public void onDataConnectionStateChanged(int state)
public void onMessageWaitingIndicatorChanged(boolean mwi)
public void onServiceStateChanged(ServiceState serviceState)
public void onSignalStrengthChanged(int asu)

只需要監聽呼叫狀態的話,覆蓋 onCallStateChanged() 即可。TelephonyManager 中定義了三種狀態:振鈴(RINGING)、摘機(OFFHOOK)與空閒(IDLE),依 state 的值就能判斷目前的通話情況。

註冊監聽

取得 TelephonyManager 介面後,呼叫 listen() 並傳入監聽器與想接收的事件遮罩:

mTelephonyMgr.listen(new TeleListener(), PhoneStateListener.LISTEN_CALL_STATE);

完整測試範例

以下範例只是把呼叫狀態即時附加到 TextView 上,方便驗證:

package com.J2MEdev;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.TextVIEw;

public class Telephony extends Activity {
    private static final String TAG = "Telephony";
    TextVIEw vIEw = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TelephonyManager mTelephonyMgr = (TelephonyManager) this
                .getSystemService(Context.TELEPHONY_SERVICE);
        mTelephonyMgr.listen(new TeleListener(),
                PhoneStateListener.LISTEN_CALL_STATE);

        vIEw = new TextVIEw(this);
        vIEw.setText("listen the state of phone\n");
        setContentView(vIEw);
    }

    class TeleListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:
                    Log.e(TAG, "CALL_STATE_IDLE");
                    vIEw.append("CALL_STATE_IDLE" + "\n");
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    Log.e(TAG, "CALL_STATE_OFFHOOK");
                    vIEw.append("CALL_STATE_OFFHOOK" + "\n");
                    break;
                case TelephonyManager.CALL_STATE_RINGING:
                    Log.e(TAG, "CALL_STATE_RINGING");
                    vIEw.append("CALL_STATE_RINGING" + "\n");
                    break;
                default:
                    break;
            }
        }
    }
}

別忘了權限

在 AndroidManifest.xml 中加上:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

實務提醒

三點值得注意。第一,新版 Android 已將 PhoneStateListener 標示為不建議使用,官方建議改用 TelephonyCallback,寫法相近但更安全。第二,incomingNumber 在部分版本或情境下可能為 null,取值前應做判斷。第三,Activity 銷毀時應呼叫 listen(listener, PhoneStateListener.LISTEN_NONE) 取消註冊,否則會造成記憶體洩漏。此外,監聽通話狀態屬於敏感行為,上架時需在說明中清楚告知用途。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved