移动数据未以编程方式关闭

时间:2014-05-02 07:25:14

标签: android

我反复使用以下代码在移动数据中的ON和OFF状态之间切换。

//Turning mobile data ON/OFF
boolean mobileDataEnabled = false; // Assume disabled
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
try {
    Class cmClass = Class.forName(cm.getClass().getName());
    Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
    method.setAccessible(true); // Make the method callable
    // get the setting for "mobile data"
    mobileDataEnabled = (Boolean)method.invoke(cm);
    method.setAccessible(false);
} catch (Exception e) {
    // Some problem accessible private API
    // TODO do whatever error handling you want here
    e.printStackTrace();
}

if(mobileDataEnabled)
{
    try {
        System.out.println("Mobile data switching off");
        ConnectivityManager enableDataConn = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
        Class enableClass = Class.forName(enableDataConn.getClass().getName());
        java.lang.reflect.Field iConnectivityManagerField = enableClass.getDeclaredField("mService");
        iConnectivityManagerField.setAccessible(true);
        final Object iConnectivityManager = iConnectivityManagerField.get(enableDataConn);
        Method method2 = enableClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
        method2.setAccessible(true); // Make the method callable
        // get the setting for "mobile data"
        method2.invoke(enableDataConn, false);
    } catch (Exception e) {
        //  Toast.makeText(getApplicationContext(),"Mobile data cannot be enabled",Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
} else {
    try {
        System.out.println("Mobile data switching on");
        ConnectivityManager enableDataConn = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
        Class enableClass = Class.forName(enableDataConn.getClass().getName());
        java.lang.reflect.Field iConnectivityManagerField = enableClass.getDeclaredField("mService");
        iConnectivityManagerField.setAccessible(true);
        final Object iConnectivityManager = iConnectivityManagerField.get(enableDataConn);
        Method method2 = enableClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
        method2.setAccessible(true); // Make the method callable
        // get the setting for "mobile data"
        method2.invoke(enableDataConn, true);
    } catch (Exception e) {
        //Toast.makeText(getApplicationContext(),"Mobile data cannot be enabled",Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
}

上面提到的代码保存在onClicklistener方法的onclick方法中。

移动数据处于关闭状态时,打开时没有问题,但是当我尝试再次切换时它没有关闭。在这种情况下我也没有任何例外。 它总是进入代码的 else 条件。

请让我知道我必须做出哪些改变,以便这两种方式都有效。

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

public boolean getMobileDataEnabled() throws Exception {
    ConnectivityManager mcm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    Class ownerClass = mcm.getClass();
    Method method = ownerClass.getMethod("getMobileDataEnabled");
    return (Boolean)method.invoke(mcm);
}

public void setMobileDataEnabled(boolean enabled) throws Exception {
    ConnectivityManager mcm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    Class ownerClass = mcm.getClass();
    ownerClass.getMethod("setMobileDataEnabled",boolean.class).invoke(mcm, enabled);
}

OnClickListener()中添加以下代码:

try {
    boolean isMobileDataEnable = getMobileDataEnabled();
    setMobileDataEnabled(!isMobileDataEnable);
} catch (Exception e) {
    e.printStackTrace();
}