TelephonyManager 黑名单来电自动挂断

所谓的黑名单就是将一些电话号码添加到一个集合中,当手机接收到这些电话的时候就直接挂断

Android 并没有给我们提供挂断电话的 API,但我们可以通过反射得到相关挂断电话的 API

Method m1 = telephonyManager.getClass().getDeclaredMethod("getITelephony");
if (m1 != null) {
    m1.setAccessible(true);
    Object iTelephony = m1.invoke(telephonyManager);

    if (iTelephony != null) {
        Method m2 = iTelephony.getClass().getDeclaredMethod("silenceRinger");

        if (m2 != null) {
            m2.invoke(iTelephony);
        }

        Method m3 = iTelephony.getClass().getDeclaredMethod("endCall");

        if (m3 != null) {
            m3.invoke(iTelephony);
        }
    }
}

挂断电话用的就是这个

Method m3 = iTelephony.getClass().getDeclaredMethod("endCall");

范例

下面我们就写一个最简单的 demo 演示单个号码的拦截,输入号码,点击屏蔽按钮后,如果此时屏蔽的电话呼入的话; 直接会挂断

  1. 创建一个 空的 Android 项目 cn.twle.android.BlackReject

  2. 修改 activity_main.xml 添加布局

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="要屏蔽的电话号码" />
    
        <EditText
            android:id="@+id/ms_lock_num"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
        <Button
            android:id="@+id/btn_lock" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="屏蔽"    
        />
    
    </LinearLayout>
    
  3. 修改 AndroidManifest.xml 添加电话相关权限

    <!-- 授予该应用控制通话的权限 -->
    <uses-permission android:name="android.permission.CALL_PHONE" />    
    <!-- 授予该应用读取通话状态的权限 -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    
  4. 创建包 cn.twle.android.common 并在包下创建权限管理帮助类 PermHelper.java

    package cn.twle.android.common;
    
    import android.content.Context;
    import android.content.pm.PackageManager;
    import android.support.v4.content.ContextCompat;
    
    public class PermHelper
    {
        private final Context mContext;
    
        public PermHelper(Context context) {
            mContext = context.getApplicationContext();
        }
    
        // 判断权限集合
        public boolean lacksPermissions(String... permissions) {
            for (String permission : permissions) {
                if (lacksPermission(permission)) {
                    return true;
                }
            }
            return false;
        }
    
        // 判断是否缺少权限
        private boolean lacksPermission(String permission) {
            return ContextCompat.checkSelfPermission(mContext, permission) == PackageManager.PERMISSION_DENIED;
        }
    }
    
  5. 添加授权页

    授权页, 首先使用系统默认的授权页, 当用户拒绝时, 指导用户手动设置, 当用户再次操作失败后, 返回继续提示

    用户手动退出授权页时, 给使用页发送授权失败的通知

    我们可以保存这个授权页并将其在多个应用中复用

  6. 创建授权页 PermActivity.java

    package cn.twle.android.blackreject;
    
    import android.app.Activity;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.Settings;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v4.app.ActivityCompat;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    
    import cn.twle.android.common.PermHelper;
    
    public class PermActivity extends AppCompatActivity {
        public static final int PERMISSIONS_GRANTED = 0; // 权限授权
        public static final int PERMISSIONS_DENIED = 1; // 权限拒绝
    
        private static final int PERMISSION_REQUEST_CODE = 0; // 系统权限管理页面的参数
        private static final String EXTRA_PERMISSIONS =
                "cn.twle.android.common.permission.extra_permission"; // 权限参数
        private static final String PACKAGE_URL_SCHEME = "package:"; // 方案
    
        private PermHelper permHelper; // 权限检测器
        private boolean isRequireCheck; // 是否需要系统权限检测, 防止和系统提示框重叠
    
        // 启动当前权限页面的公开接口
        public static void startActivityForResult(Activity activity, int requestCode, String... permissions) {
            Intent intent = new Intent(activity, PermActivity.class);
            intent.putExtra(EXTRA_PERMISSIONS, permissions);
            ActivityCompat.startActivityForResult(activity, intent, requestCode, null);
        }
    
        @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if (getIntent() == null || !getIntent().hasExtra(EXTRA_PERMISSIONS)) {
                throw new RuntimeException("PermissionsActivity需要使用静态startActivityForResult方法启动!");
            }
            //setContentView(R.layout.activity_perm);
    
            permHelper = new PermHelper(this);
            isRequireCheck = true;
        }
    
        @Override protected void onResume() {
            super.onResume();
            if (isRequireCheck) {
                String[] permissions = getPermissions();
                if (permHelper.lacksPermissions(permissions)) {
                    requestPermissions(permissions); // 请求权限
                } else {
                    allPermissionsGranted(); // 全部权限都已获取
                }
            } else {
                isRequireCheck = true;
            }
        }
    
        // 返回传递的权限参数
        private String[] getPermissions() {
            return getIntent().getStringArrayExtra(EXTRA_PERMISSIONS);
        }
    
        // 请求权限兼容低版本
        private void requestPermissions(String... permissions) {
            ActivityCompat.requestPermissions(this, permissions, PERMISSION_REQUEST_CODE);
        }
    
        // 全部权限均已获取
        private void allPermissionsGranted() {
            setResult(PERMISSIONS_GRANTED);
            finish();
        }
    
        /**
         * 用户权限处理,
         * 如果全部获取, 则直接过.
         * 如果权限缺失, 则提示Dialog.
         *
         * @param requestCode  请求码
         * @param permissions  权限
         * @param grantResults 结果
         */
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            if (requestCode == PERMISSION_REQUEST_CODE && hasAllPermissionsGranted(grantResults)) {
                isRequireCheck = true;
                allPermissionsGranted();
            } else {
                isRequireCheck = false;
                showMissingPermissionDialog();
            }
        }
    
        // 含有全部的权限
        private boolean hasAllPermissionsGranted(@NonNull int[] grantResults) {
            for (int grantResult : grantResults) {
                if (grantResult == PackageManager.PERMISSION_DENIED) {
                    return false;
                }
            }
            return true;
        }
    
        // 显示缺失权限提示
        private void showMissingPermissionDialog() {
            AlertDialog.Builder builder = new AlertDialog.Builder(PermActivity.this);
            builder.setTitle("帮助");
            builder.setMessage("当前应用缺少必要权限。\n请点击\"设置\"-\"权限\" 打开所需权限。\n最后点击两次回退按钮,即可返回");
    
            // 拒绝, 退出应用
            builder.setNegativeButton("推出", new DialogInterface.OnClickListener() {
                @Override public void onClick(DialogInterface dialog, int which) {
                    setResult(PERMISSIONS_DENIED);
                    finish();
                }
            });
    
            builder.setPositiveButton("设置", new DialogInterface.OnClickListener() {
                @Override public void onClick(DialogInterface dialog, int which) {
                    startAppSettings();
                }
            });
    
            builder.setCancelable(false);
    
            builder.show();
        }
    
        // 启动应用的设置
        private void startAppSettings() {
            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.setData(Uri.parse(PACKAGE_URL_SCHEME + getPackageName()));
            startActivity(intent);
        }
    }
    
  7. 修改 MainActivity.java

    package cn.twle.android.blackreject;
    
    import android.Manifest;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.telephony.PhoneStateListener;
    import android.telephony.TelephonyManager;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    
    import cn.twle.android.common.PermHelper;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        final private static int REQUEST_CODE = 29; // 请求码
    
        private EditText ms_lock_num;
        private Button btn_lock;
    
        ArrayList<String> blockList = new ArrayList<>();
    
        static final String[] PERMISSIONS = new String[]{
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.CALL_PHONE
        };
    
        private PermHelper permHelper;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            permHelper = new PermHelper(this);
    
            ms_lock_num = ( EditText ) findViewById(R.id.ms_lock_num);
            btn_lock  = (Button)findViewById(R.id.btn_lock);
    
            btn_lock.setOnClickListener(this);
    
            if (permHelper.lacksPermissions(PERMISSIONS)) {
                startPermissionsActivity();
            }
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            if (permHelper.lacksPermissions(PERMISSIONS)) {
                startPermissionsActivity();
            }
        }
    
        private void startPermissionsActivity() {
            PermActivity.startActivityForResult(this, REQUEST_CODE, PERMISSIONS);
        }
    
        public void startListener() {
    
            final TelephonyManager telephonyManager =
                    (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    
            PhoneStateListener listener = new PhoneStateListener() {
                @Override
                public void onCallStateChanged(int state, String incomingNumber) {
    
                    switch (state) {
                        case TelephonyManager.CALL_STATE_RINGING:
                            if (isBlock(incomingNumber)) {
                                try {
    
                                    Method m1 = telephonyManager.getClass().getDeclaredMethod("getITelephony");
                                    if (m1 != null) {
                                        m1.setAccessible(true);
                                        Object iTelephony = m1.invoke(telephonyManager);
    
                                        if (iTelephony != null) {
                                            Method m2 = iTelephony.getClass().getDeclaredMethod("silenceRinger");
    
                                            if (m2 != null) {
                                                m2.invoke(iTelephony);
                                            }
    
                                            Log.d("blacktelegram",incomingNumber);
    
                                            Method m3 = iTelephony.getClass().getDeclaredMethod("endCall");
    
                                            if (m3 != null) {
                                                m3.invoke(iTelephony);
                                            }
                                        }
                                    }
    
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                            break;
                        default:
                            break;
                    }
    
                    super.onCallStateChanged(state, incomingNumber);
                }
            };
    
            telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
    
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            // 拒绝时, 关闭页面, 缺少主要权限, 无法运行
            if (requestCode == REQUEST_CODE && resultCode == PermActivity.PERMISSIONS_DENIED) {
                finish();
            }
        }
    
        @Override
        public void onClick(View view) {
    
            blockList.add(ms_lock_num.getText().toString());
            Toast.makeText(MainActivity.this,"手机号码已经加入屏蔽列表",Toast.LENGTH_SHORT).show();
            startListener();
        }
    
        private boolean isBlock(String phone) {
            for (String s1 : blockList) {
                if (s1.equals(phone)) {
                    return true;
                }
            }
            return false;
        }
    }
    

参考文档

  1. Android TelephonyManager 官方 API

  2. Android电话信息相关 API

Android 基础教程

关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.