Skip to content Skip to sidebar Skip to footer

Mobile Data Is Not Turning Off Programmatically

I am using the following code repeatedly to toggle between the ON and OFF states in mobile data. //Turning mobile data ON/OFF boolean mobileDataEnabled = false; // Assume disabled

Solution 1:

You can try this:

publicbooleangetMobileDataEnabled()throws Exception {
    ConnectivityManagermcm= (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    ClassownerClass= mcm.getClass();
    Methodmethod= ownerClass.getMethod("getMobileDataEnabled");
    return (Boolean)method.invoke(mcm);
}

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

In OnClickListener() add these code:

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

Post a Comment for "Mobile Data Is Not Turning Off Programmatically"