zoukankan      html  css  js  c++  java
  • 拦截来电

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

     

  • 相关阅读:
    Linux网卡设置
    Linux虚拟机-----概述(1)
    Redis缓存数据库-----概述(1)
    Spring和Mybatis的集成
    onehot编码解释
    LINUX-CUDA版本所对应的NVIDIA驱动版本号,cuda版本报错的朋友参考一下
    matplotlib画图
    pytorch实现花朵数据集读取
    轻量架构ShuffleNet V2:从理论复杂度到实用设计准则
    CBAM: 卷积块注意模块
  • 原文地址:https://www.cnblogs.com/anni-qianqian/p/5405514.html
Copyright © 2011-2022 走看看