Android黑名单电话挂断(AIDL)

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

Android黑名单电话挂断(AIDL)_第1张图片

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 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>

你可能感兴趣的:(Android四大基本主键)