Android - 在ConnectivityManager中的公共方法setMobileDataEnabled在SDK中不可用

时间:2011-02-10 09:09:50

标签: android connectivity

我正在尝试使用SDK 2.2来使用ConnectivityManager类的setMobileDataEnabled方法。 根据{{​​3}},这个方法被公开,但SDK中和Eclipse中没有@hide。

为了绕过隐藏,我编写了以下函数来打开/关闭移动数据连接。

public void setMobileData(boolean toBeEnabled){

        Object myObj= getSystemService(CONNECTIVITY_SERVICE); 
        ConnectivityManager cm = (ConnectivityManager) myObj;

        Class c = null;
        try {
            c = Class.forName(cm.getClass().getName());
        } catch (ClassNotFoundException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        Method m = null;
        try {
            m = c.getDeclaredMethod("getMobileDataEnabled");
        } catch (SecurityException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        } catch (NoSuchMethodException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        Object mobileDataEnabled=null;
        if (m!=null){
            m.setAccessible(true);
            Type res_of_m=  m.getGenericReturnType();
            Type[] pars_of_m=  m.getGenericParameterTypes();
            try {
                mobileDataEnabled = (m.invoke(cm));
                if (mobileDataEnabled!=null)
                    if (mobileDataEnabled.equals(!toBeEnabled)){
                        Method m2 = null;
                        try {
                            int index=0;
                            boolean method_found=false;
                            Method[] available_methods= c.getDeclaredMethods();
                            for (Method method : available_methods) {
                                // following line doesn't work
                                // method.getName()=="setMobileDataEnabled" 
                                if (method.getName().contains("setMobileDataEnabled")) 
                                {
                                    method_found=true;
                                }

                                if (method_found==false) 
                                    index++;
                            }
                            // following line doesn't work
                            //m2 = c.getDeclaredMethod("setMobileDataEnabled"); 
                            m2 = (c.getDeclaredMethods())[index];
                            if (m2!=null){
                                m2.setAccessible(true);
                                m2.invoke(cm,toBeEnabled);
                            }
                        } catch (SecurityException e2) {
                            // TODO Auto-generated catch block
                            e2.printStackTrace();
                        } catch (InvocationTargetException e2) {
                            // TODO Auto-generated catch block
                            e2.printStackTrace();
                        }
                    }
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

为了使其正常工作,我还在清单中添加了android.permission.WRITE_SECURE_SETTINGS,并根据http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2.1_r1/android/net/ConnectivityManager.java/?v=source安装在/ system / app中。

有没有人知道更好的方法?

2 个答案:

答案 0 :(得分:2)

   try {
        final ConnectivityManager cm = (ConnectivityManager) this.context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        final Class connectivityManager = Class.forName(cm.getClass()
                .getName());

        final Method[] methods = connectivityManager.getDeclaredMethods();

        for (final Method method : methods) {
            if (MLauncherConnectivityManager.SET_MOBILE_DATA_ENABLED
                    .equals(method.getName())) {
                method.invoke(cm, true);
            }
        }

    } catch (final ClassNotFoundException e) {
        Log.e("Class", e.getMessage());
    } catch (final IllegalArgumentException e) {
        Log.e("Class", e.getMessage());
    } catch (final IllegalAccessException e) {
        Log.e("Class", e.getMessage());
    } catch (final InvocationTargetException e) {
        Log.e("Class", e.getMessage());
    }

答案 1 :(得分:0)

你可以查看这个链接How to disable Mobile Data on Android,我想你可以像@Raghu那样做