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

用 Android 廣播接收器實作 IP 撥號:攔截 ACTION_NEW_OUTGOING_CALL

編輯:關於 Android 程式設計

企業電話網常需要在撥號號碼前自動加上 IP 卡的前綴號碼。Android 提供 ACTION_NEW_OUTGOING_CALL 這個系統廣播,正是為此設計:當使用者按下撥號鍵、通話尚未真正建立時,系統會發出這個廣播,我們可以在裡面改寫要撥出的號碼。

一、版面配置

UI 很簡單:一個輸入框與一顆儲存按鈕,用來設定前綴號碼。

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="請輸入 IP 前綴號碼" />

    <EditText
        android:id="@+id/et_number"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="phone" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="save"
        android:text="儲存 IP 號碼" />

</LinearLayout>

二、MainActivity:把前綴存起來

這裡用 SharedPreferences 持久化,確保重開 App 或行程被回收後設定仍在。

package com.wzw.ipdial;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    private EditText etNumber;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etNumber = (EditText) findViewById(R.id.et_number);
        sp = getSharedPreferences("config", MODE_PRIVATE);

        // 把已存的值回填到輸入框,方便使用者確認目前設定
        etNumber.setText(sp.getString("ipnumber", ""));
    }

    public void save(View v) {
        String ipnumber = etNumber.getText().toString().trim();
        if (TextUtils.isEmpty(ipnumber)) {
            Toast.makeText(this, "已清除 IP 號碼", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "已設定 IP 號碼", Toast.LENGTH_SHORT).show();
        }
        Editor editor = sp.edit();
        editor.putString("ipnumber", ipnumber);
        editor.apply();
    }
}

三、廣播接收器:改寫撥出號碼

關鍵是 getResultData() 取得原始號碼,再用 setResultData() 寫回加上前綴後的新號碼。

package com.wzw.ipdial;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

/** 攔截撥號廣播,在前面加上 IP 前綴 */
public class Ipdial extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String number = getResultData();     // 使用者原本要撥的號碼
        if (number == null) return;

        SharedPreferences sp = context.getSharedPreferences("config", context.MODE_PRIVATE);
        String ipnumber = sp.getString("ipnumber", "");

        // 前綴為空時保持原號碼不變
        setResultData(ipnumber + number);
    }
}

四、清單檔:權限與註冊

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.wzw.ipdial"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />

    <!-- 攔截撥號的必要權限 -->
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- 註冊廣播接收器,指定攔截撥號動作 -->
        <receiver android:name="com.wzw.ipdial.Ipdial">
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
        </receiver>

    </application>
</manifest>

五、實務上的注意事項

  • 權限是敏感權限:PROCESS_OUTGOING_CALLS 屬於危險等級,Android 6.0 以上必須在執行期動態向使用者申請,否則 onReceive 根本不會被觸發。
  • 只能改寫、不能攔截:回傳 null 給 setResultData() 的行為在各版本上不一致,不要依賴它來「取消通話」。
  • 前綴格式:若使用 IP 卡,記得讓使用者能輸入逗號或分號來控制暫停,例如 17951,。
  • API 演進:Android 10 起這個廣播的可用性受到限制,CallRedirectionService(綁定 android.telecom.action.CALL_REDIRECTION_SERVICE)才是官方建議的替代方案,新專案請直接採用後者。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved