Android电话设置黑名单,来电自动挂断

来源:互联网 发布:软件测试项目描述 编辑:程序博客网 时间:2024/04/30 10:56
首先导入两个外包
public class PhoneActivity extends BroadcastReceiver {    private TelephonyManager tm;    @Override    public void onReceive(Context context, Intent intent) {        //判断是否为手机行为状态      if("android.intent.action.PHONE_STATE".equals(intent.getAction())){            //获取电脑管理者            tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);            int state=tm.getCallState();           //获取电话号码           String phone=intent.getStringExtra("incoming_number");            switch (state){                case TelephonyManager.CALL_STATE_RINGING:                    Log.i("test",phone+"来电");                    //得到电话管理者类对象                    Class<TelephonyManager> clazz=TelephonyManager.class;                    //得到方法                    try {                        Method method=clazz.getDeclaredMethod("getITelephony",null);                        //设置可访问                        method.setAccessible(true);                        //执行方法                        try {                            ITelephony iTelephony= (ITelephony) method.invoke(tm,null);                            //判断                            if("18873870231".equals(phone)){                                try {                                    iTelephony.endCall();                                } catch (RemoteException e) {                                    e.printStackTrace();                                }                            }                        } catch (IllegalAccessException e) {                            e.printStackTrace();                        } catch (InvocationTargetException e) {                            e.printStackTrace();                        }                    } catch (NoSuchMethodException e) {                        e.printStackTrace();                    }                    break;
    <!--读取电话状态的权限--><uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>    <!--打定话的权限--><uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>


原创粉丝点击