Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 掛斷來電實作:用 AIDL 打造來電黑名單攔截

編輯:關於 Android 程式設計

一、為什麼要繞道 AIDL

Android 並沒有對外公開「結束通話」的 API。如果要在程式裡掛斷電話,必須透過 AIDL 與電話管理服務(Telephony)溝通,再呼叫服務中隱藏的介面來達成。以下說明完整流程。

二、準備 AIDL 檔

從 Android 原始碼中複製兩個檔案進專案:

  • com.android.internal.telephony 包下的 ITelephony.aidl
  • android.telephony 包下的 NeighboringCellInfo.aidl

注意:必須在專案裡建立與原始碼完全相同名稱的包來存放這兩個檔案,因為 AIDL 產生的 Java 檔會帶有原封不動的 package 宣告,包名對不上就會編譯失敗。開發工具會在 gen/ 目錄下自動產生 ITelephony.java。

三、呼叫 endCall() 掛斷通話

核心是透過反射取得 Binder,再轉成 ITelephony 介面:

Method method = Class.forName("android.os.ServiceManager")
        .getMethod("getService", String.class);
IBinder binder = (IBinder) method.invoke(null, new Object[]{ TELEPHONY_SERVICE });
ITelephony telephony = ITelephony.Stub.asInterface(binder);
telephony.endCall();

記得在 AndroidManifest.xml 加上權限:

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

四、程式碼示例

package cn.itcast.endcall;

import java.lang.reflect.Method;
import com.android.internal.telephony.ITelephony;
import android.app.Activity;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;

public class DemoActivity extends Activity {
    ITelephony iTelephony;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try {
            // 取得系統電話管理服務
            Method method = Class.forName("android.os.ServiceManager")
                    .getMethod("getService", String.class);
            // 以反射取得 binder 物件
            IBinder binder = (IBinder) method.invoke(null,
                    new Object[] { TELEPHONY_SERVICE });
            iTelephony = ITelephony.Stub.asInterface(binder);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void endcall(View view) {
        try {
            // iTelephony.endCall();      // 掛斷通話
            iTelephony.call("15139394270"); // 主動撥號
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}

五、兩個 AIDL 檔內容

NeighboringCellInfo.aidl:

package android.telephony;

parcelable NeighboringCellInfo;

ITelephony.aidl(節錄主要方法):

package com.android.internal.telephony;

import android.os.Bundle;
import java.util.List;
import android.telephony.NeighboringCellInfo;

interface ITelephony {
    /** 開啟撥號盤,可預填號碼,但不直接撥出 */
    void dial(String number);

    /** 直接撥出電話 */
    void call(String number);

    /** 若有通話進行中,顯示通話畫面 */
    boolean showCallScreen();
    boolean showCallScreenWithDialpad(boolean showDialpad);

    /** 掛斷通話 */
    boolean endCall();

    /** 接聽來電(若已有通話則自動保留) */
    void answerRingingCall();

    /** 靜音來電铃声 */
    void silenceRinger();

    /** 是否正在通話中(OFFHOOK) */
    boolean isOffhook();

    /** 是否有來電響鈴(RINGING) */
    boolean isRinging();

    /** 是否閒置(IDLE) */
    boolean isIdle();

    boolean isRadioOn();
    boolean isSimPinEnabled();
    void cancelMissedCallsNotification();
    boolean supplyPin(String pin);
    boolean handlePinMmi(String dialString);
    void toggleRadioOnOff();
    boolean setRadio(boolean turnOn);
    void updateServiceLocation();
    void enableLocationUpdates();
    void disableLocationUpdates();

    int enableApnType(String type);
    int disableApnType(String type);
    boolean enableDataConnectivity();
    boolean disableDataConnectivity();
    boolean isDataConnectivityPossible();

    Bundle getCellLocation();
    List<NeighboringCellInfo> getNeighboringCellInfo();

    int getCallState();
    int getDataActivity();
    int getDataState();
    int getActivePhoneType();
    int getCdmaEriIconIndex();
    int getCdmaEriIconMode();
    String getCdmaEriText();
    boolean getCdmaNeedsProvisioning();
    int getVoiceMessageCount();
    int getNetworkType();
    boolean hasIccCard();
}

六、做成黑名單攔截器

把上面的能力跟來電廣播結合起來,就是一個完整的攔截方案:

  1. 在 AndroidManifest.xml 註冊 android.intent.action.PHONE_STATE 的 Receiver,並加上 READ_PHONE_STATE 權限。
  2. 在 onReceive() 中透過 TelephonyManager.getCallState() 判斷是否為 CALL_STATE_RINGING。
  3. 用 getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER) 取出來電號碼。
  4. 比對本地黑名單,命中就呼叫 iTelephony.endCall() 掛斷。

七、務必知道的相容性風險

這個做法雖然常見,但屬於非官方途徑,實務上有幾個明確限制:

  • com.android.internal.telephony.ITelephony 是隱藏 API,不同 Android 版本、不同廠商 ROM 的方法簽名都可能不同,endCall() 甚至在某些版本需要參數。
  • 從 Android 8.0 起,隱藏 API 呼叫會被系統攔截;Android 9 之後更需搭配 endCall() 的公開替代方案:TelecomManager.endCall() 或 CallScreeningService。
  • 部分中國大陸廠商 ROM 會需要額外白名單或系統簽名才能正常掛斷。

因此,若是新專案,建議直接採用官方 API:Android 10 以上請用 CallScreeningService 在來電階段就拒接,並搭配 ANSWER_PHONE_CALLS 權限;較舊版本則用 TelecomManager.endCall()。上面這套 AIDL 反射的做法,比較適合維護老專案或需要深度客製系統 ROM 的情境。

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