• 拦截来电


    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);
        }
    }

     

  • 相关阅读:
    POJ3159 Candies —— 差分约束 spfa
    POJ1511 Invitation Cards —— 最短路spfa
    POJ1860 Currency Exchange —— spfa求正环
    POJ3259 Wormholes —— spfa求负环
    POJ3660 Cow Contest —— Floyd 传递闭包
    POJ3268 Silver Cow Party —— 最短路
    POJ1797 Heavy Transportation —— 最短路变形
    POJ2253 Frogger —— 最短路变形
    POJ1759 Garland —— 二分
    POJ3685 Matrix —— 二分
  • 原文地址:https://www.cnblogs.com/anni-qianqian/p/5405514.html
Copyright © 2020-2023  润新知