zoukankan      html  css  js  c++  java
  • 代码控制数据流量开关

        /** 
         * 操作数据流量
         * GPRS网络开关 反射ConnectivityManager中hide的方法setMobileDataEnabled 可以开启和关闭GPRS网络 
         * @param isEnable 
         * @throws Exception 
         */  
        public static void setGprsStatus(Context context,boolean isEnable){  
            ConnectivityManager mConnectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);  
            Class<?> cmClass = mConnectivityManager.getClass();  
            Class<?>[] argClasses = new Class[1];  
            argClasses[0] = boolean.class;  
      
            // 反射ConnectivityManager中hide的方法setMobileDataEnabled,可以开启和关闭GPRS网络  
            Method method;
            try {
                method = cmClass.getMethod("setMobileDataEnabled", argClasses);
                method.invoke(mConnectivityManager, isEnable);  
            } catch (NoSuchMethodException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }  
        } 

    这段代码可以设置GPRS的状态,用到了反射。

  • 相关阅读:
    最大子数组
    链表插入排序
    链表求和
    有效回文串
    排球比赛计分系统
    超市收银系统
    三层架构
    Wpf+数据库代码封装+策略模式封装
    封装逻辑用策略模式实现
    代码封装
  • 原文地址:https://www.cnblogs.com/tianzhijiexian/p/4251655.html
Copyright © 2011-2022 走看看