Android黑名单电话挂断(AIDL)

来源:互联网 发布:淘宝零食店 知乎 编辑:程序博客网 时间:2024/05/07 08:03

1.先应该Android电话监听的AIDL加进来
建一个aidl文件夹,把以下资源放进去
小编准备一个资源路径:http://download.csdn.net/detail/vampire2777/9752907

这里写图片描述

2.MyPhoneStateReceived.java

public class MyPhoneStateReceived extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        if("android.intent.action.PHONE_STATE".equals(intent.getAction())){            //d得到电话管理者            TelephonyManager telephonyManager= (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);            //得到电话的状态            int state=telephonyManager.getCallState();            switch (state) {                case TelephonyManager.CALL_STATE_RINGING:                    //得到电话号码                    String number=intent.getStringExtra("incoming_number");                    Log.i("test","来电了"+number);                    //得到电话管理者类对象                    Class<TelephonyManager> clazz=TelephonyManager.class;                    //得到方法                    try {                        Method method=clazz.getDeclaredMethod("getITelephony",null);                        //设置可访问                        method.setAccessible(true);                        //执行方法                        ITelephony iTelephony= (ITelephony) method.invoke(telephonyManager,null);                        //判断                        if("18163843884".equals(number)){                            iTelephony.endCall();                        }                    } catch (NoSuchMethodException e) {                        e.printStackTrace();                    } catch (InvocationTargetException e) {                        e.printStackTrace();                    } catch (IllegalAccessException e) {                        e.printStackTrace();                    } catch (RemoteException e) {                        e.printStackTrace();                    }                    break;                case TelephonyManager.CALL_STATE_OFFHOOK:                    Log.i("test","通话中...录音中");                    break;                case TelephonyManager.CALL_STATE_IDLE:                    Log.i("test","挂了");                    break;            }        }    }}

3.AndroidManifest.xml

 <!--读取电话状态的权限-->    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>    <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>   <receiver android:name=".MyPhoneStateReceived">            <intent-filter>                <action android:name="android.intent.action.PHONE_STATE"></action>            </intent-filter>        </receiver>
0 0