1. 程式人生 > 實用技巧 >Android 攔截電話 適用版本4.4

Android 攔截電話 適用版本4.4

Android 攔截電話

1.建立一個名為InterceptCall的應用程式

(1)在activity_main.xml檔案定義一個相對佈局,在該佈局中放置了兩個控制元件(EditText 和 Button)用於輸入和儲存攔截號碼,同時在Button控制元件中新增onClick屬性為按鈕新增單擊事件。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto
" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <EditText android:id="@+id/et_ipnumber" android:layout_width="match_parent" android:layout_height="wrap_content
" android:hint="請輸入攔截號碼"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/et_ipnumber" android:layout_centerHorizontal="true" android:background="#ACD6FF" android:onClick="click
" android:paddingLeft="5dp"w android:paddingRight="5dp" android:text="儲存攔截號碼" android:textSize="16sp" tools:ignore="OnClick" /> </RelativeLayout>

(2)在MainActivity 中編寫介面互動的程式碼,初始化EditText物件和SharedPreferences物件,然後建立click()方法實現單擊事件,當用戶單擊”儲存攔截號碼“按鈕時,就會將要攔截的號碼儲存到SharedPreferences中。

package com.example.interceptcall;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.SharedMemory;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText et_ipnimber;
    private SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_ipnimber=(EditText) findViewById(R.id.et_ipnumber);
        sp = getSharedPreferences("config",MODE_PRIVATE);
    }
        public void click(View view){
            String number = et_ipnimber.getText().toString().trim();
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("number",number);
            editor.commit();
            Toast.makeText(this,"儲存成功",Toast.LENGTH_SHORT).show();
        }
    }

  

(3)建立一個監聽廣播事件

建立OutCallReceiver.java用於接受外撥電話的廣播, 利用getResultDatea()方法可以獲得外撥的電話號碼, 獲取被儲存在SharedPreferences中的攔截號碼,然後判斷是否使用者外撥電話號碼與攔截號碼一致,如果一致澤呼叫setResultData()方法,將其清除,系統自動關閉電話功能,如果不一致則繼續通話。

package com.example.interceptcall;

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

public class OutCallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String outcallnumber =getResultData();
        SharedPreferences sp=context.getSharedPreferences("config",Context.MODE_PRIVATE);
        String number = sp.getString("number","");
        if (outcallnumber.equals(number)){
            setResultData(null);
        }
    }
}

 (4)當輸入完電話號碼撥打電話時Android 系統將傳送一個廣播(android.intent.action.NEW_OUTGOING_CALL)給電話撥號器的廣播接收者,因此想要攔截生效,就需要在AndroidManifest.xml檔案中註冊一個廣播接收者,用來攔截外撥電話的廣播,因為外撥電話涉及到許可權問題,所以我們也要在清單檔案中新增相應的許可權。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.interceptcall">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<!--註冊廣播接收者-->
        <receiver android:name=".OutCallReceiver">
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
            </intent-filter>
        </receiver>
    </application>
<!--設定許可權-->
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
</manifest>

已下為測試的效果截圖

儲存完攔截電話後返回主頁

進入撥號介面 輸入10086

點選撥號如果返回主介面即可成功