public class MyService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //监听来电。
        TelephonyManager manager = (TelephonyManager) getSystemService(Service.TELEPHONY_SERVICE);
        PhoneStateListener listener = new MyPhoneStateListener();
        manager.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);//监听电话状态。
    }
    class MyPhoneStateListener extends PhoneStateListener{
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            Log.i("Main",incomingNumber+"");
            if("5556".equals(incomingNumber)){
                switch (state){//判断状态
                    case TelephonyManager.CALL_STATE_IDLE://0
                                //挂断
                        Log.i("Main",TelephonyManager.CALL_STATE_IDLE+"");
                            break;
                    case TelephonyManager.CALL_STATE_RINGING://1
                        //响铃状态。
                        Log.i("Main", TelephonyManager.CALL_STATE_RINGING + "");
                        try {
                            //forName:要反射的包名.类名。----
                            Class myclass =   Class.forName("android.os.ServiceManager");
                            //得到方法。
                            /*
                            1;参数:方法的名称
                            2:参数:方法参数的类型的字节码文件。
                             */
                            Method method = myclass.getMethod("getService", String.class);
                            //调用方法。
                            /*
                            1:表示是否用对象来调用这个方法。如果不用对象则用null,
                            2:表示实参
                             */
                           ITelephony binder = (ITelephony) method.invoke(null, Service.TELEPHONY_SERVICE);
                            binder.endCall();//挂断电话
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        } 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://2
                        //通话状态
                        Log.i("Main",TelephonyManager.CALL_STATE_OFFHOOK+"");
                        //
                        break;
                }
            }
        }
    }
}
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent(this,MyService.class);
        startService(intent);
    }
}