0

我正在尝试使用 SDK 2.2 的 ConnectivityManager 类的 setMobileDataEnabled 方法。根据http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2.1_r1/android/net/ConnectivityManager.java/?v=source 这个方法被声明public 但在 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" 并根据Android 安装在 /system/app 中:将应用程序添加到固件,使用 WRITE_SECURE_SETTINGS

有谁知道更好的方法?

4

1 回答 1

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());
    }
于 2012-03-24T14:31:38.097 回答