Android自動攔截與接聽功能APK黑白名單

自動接聽與掛斷調用關系如下

1. 首先在啟動apk初始界面的activity中,啟動service服務,開始監聽通話狀態(InCallService)

   //啟動通話狀態監聽服務
    private void startCallListen() {
        Intent intent = new Intent(CallAndMessageEntranceActivity.this, InCallService.class);
        startService(intent);
}
 

2. 在通話監聽服務中,監聽通話狀態,當通話狀態為CALL_STATE_RINGING時處理通話。

public class InCallService extends Service {
    private TelecomManager mTelecomManager;
    private TelephonyManager mTelephonymanager;
    MyPhoneStateListener mMyPhoneStateListener = new MyPhoneStateListener();
 
    @Override
    public void onCreate() {
        Log.e("InCallService", "onCreate()");
        super.onCreate();
 
        //獲取電話服務
        mTelecomManager = (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
        mTelephonymanager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
   }
 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (mTelephonymanager == null) {
            mTelephonymanager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        }
        mTelephonymanager.listen(mMyPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
        return super.onStartCommand(intent, flags, startId);
    }
 
    //監聽通過話
    public class MyPhoneStateListener extends PhoneStateListener {
 
        @RequiresApi(api = Build.VERSION_CODES.P)
        @Override
        public void onCallStateChanged(int state, final String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:
                    //通話空閑狀態
                    break;
                case TelephonyManager.CALL_STATE_RINGING:
                    //通話來電狀態
                    try {
                        Log.e("C_M_BlackCallService", "Monitor incoming calls");
                        //攔截來電判斷來電時黑名單or白名單
                        splitWhiteAndBlack(incomingNumber);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    //電話掛起狀態
                    break;
            }
 
        }
    }
    //incomingNumber:來電號碼 依據來電號碼判斷黑白名單
    private void splitWhiteAndBlack(String incomingNumber) {
        //1.黑名單自動掛斷
        mTelecomManager.endCall();
        //2.白名單自動接聽
        mTelecomManager.acceptRingingCall();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
 
}

到此這篇關於Android自動攔截與接聽功能APK黑白名單的文章就介紹到這瞭,更多相關Android攔截與接聽內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: