黑名单的大致原理是监听来电,当有来电是检查它的号码,是否存在于黑名单中。如果它是黑名单中的号码,立即挂断电话。

所以主要步骤是四步:一、监听有来电并获取它的号码;二、查看号码是否在黑名单中;三、挂断电话;四、启动监控。

一、监听来电并获取它的号码

1、引入接口文件

文件名和包名必须与之相同。

2、申请权限

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

 

3、描述监控内容

CustomPhoneCallListener cpListener;
    TelephonyManager telephonyManager;
    public class CustomPhoneCallListener extends PhoneStateListener{
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state){
                case TelephonyManager.CALL_STATE_IDLE:
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    break;
                case TelephonyManager.CALL_STATE_RINGING:
                    if(isBlock(incomingNumber)){
                        try{
                            Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
                            IBinder binder = (IBinder) method.invoke(null,new Object[]{TELEPHONY_SERVICE});
                            ITelephony telephony = ITelephony.Stub.asInterface(binder);
                            telephony.endCall();
                        } catch (Exception e){
                            e.printStackTrace();
                        }
                    }
            }
            super.onCallStateChanged(state, incomingNumber);
        }
    }

其中incomingNumber就是来电的号码。

二、查看号码是否在黑名单中

    private boolean isBlock(String number) {
        for (int i = 0; i < black_phone.size(); i++) {
            if(black_phone.get(i).equals(number)){
                return true;
            }
        }
        return false;
    }

三、如果号码存在于黑名单中,挂断电话

                    if(isBlock(incomingNumber)){
                        try{
                            Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
                            IBinder binder = (IBinder) method.invoke(null,new Object[]{TELEPHONY_SERVICE});
                            ITelephony telephony = ITelephony.Stub.asInterface(binder);
                            telephony.endCall();
                        } catch (Exception e){
                            e.printStackTrace();
                        }
                    }

四、启动监控

    CustomPhoneCallListener cpListener;
    TelephonyManager telephonyManager;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.black_number);
        telephonyManager= (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        cpListener = new CustomPhoneCallListener();
        telephonyManager.listen(cpListener,PhoneStateListener.LISTEN_CALL_STATE);
    }

 

posted on 2015-01-17 13:30  Fishbonell  阅读(227)  评论(0编辑  收藏  举报