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

Android 如何偵測未接來電並自動後續處理

編輯:關於 Android 程式設計

Android 平台並沒有現成的「未接來電」監聽器,因此想攔下這個事件,必須自己動手:一邊用 PhoneStateListener 追蹤通話狀態的變化,一邊靠系統廣播把電話狀態事件送進 App。判斷的關鍵其實很朴素——如果上一刻還在響鈴,下一刻卻直接回到閒置,那這通電話就沒被接起。

一、撰寫 CallListener:攔截通話狀態切換

繼承 PhoneStateListener,在 onCallStateChanged 中比對「上一次狀態」與「本次狀態」。當 CALL_STATE_RINGING 轉為 CALL_STATE_IDLE 時,即可認定為未接來電,接著交由 sendSmgWhenMissedCall 去做後續動作(發送簡訊、寄送 email、寫入通知等)。

package rbase.app.smshelpmate.call.listener;

import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class CallListener extends PhoneStateListener {

    private static final String TAG = "sms";

    /** 記錄上一次的通話狀態 */
    private static int lastetState = TelephonyManager.CALL_STATE_IDLE;

    private Context context;

    public CallListener(Context context) {
        super();
        this.context = context;
    }

    public void onCallStateChanged(int state, String incomingNumber) {
        Log.v(TAG, "CallListener call state changed : " + incomingNumber);

        // 上一刻還在響鈴,這一刻卻回到閒置,即判定為未接來電
        if (lastetState == TelephonyManager.CALL_STATE_RINGING
                && state == TelephonyManager.CALL_STATE_IDLE) {
            sendSmgWhenMissedCall(incomingNumber);
        }

        // 更新狀態快照,供下次比對
        lastetState = state;
    }

    private void sendSmgWhenMissedCall(String incomingNumber) {
        // 未接來電的處理邏輯:發送簡訊、寄送 email 或其他通知
    }
}

二、撰寫 CallReceiver:註冊來電廣播接收器

在 onReceive 中取得 TelephonyManager,把上一節的監聽器掛上去,同時也能從 Intent 的 extras 裡讀出來電號碼。

package rbase.app.smshelpmate.call.service;

import rbase.app.smshelpmate.call.listener.CallListener;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class CallReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        Log.i("sms", "CallReceiver Start...");

        TelephonyManager telephony =
                (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        CallListener customPhoneListener = new CallListener(context);
        telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

        Bundle bundle = intent.getExtras();
        String phoneNr = bundle.getString("incoming_number");
        Log.i("sms", "CallReceiver Phone Number : " + phoneNr);
    }
}

三、在 AndroidManifest.xml 註冊廣播

把接收器掛在 application 節點下,並監聽 android.intent.action.PHONE_STATE。intent-filter 上的 priority 設為 100,可提高接收時機的優先度。

<manifest ...>
    <application ...>
        <receiver android:name=".call.service.CallReceiver">
            <intent-filter android:priority="100">
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

四、加上讀取電話狀態的權限

少了這行,接收器永遠收不到回呼。

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

四步完成後,一通電話若響了又沒人接,sendSmgWhenMissedCall 便會被觸發,你可以在裡面接上簡訊通知、郵件提醒或任何自訂流程。實務上要留意兩點:一是狀態快照必須是「物件層級」而非每次新建監聽器時重算,否則比對會失效;二是 Android 6.0 以後 READ_PHONE_STATE 屬於執行期權限,記得補上動態授權的請求流程。

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