从Android Lollipop或更高版本的代码禁用数据连接

时间:2016-01-25 17:36:40

标签: android android-networking

有没有办法在拥有Lollipop或更高版本(代码中)的Android上禁用/启用数据连接?

1 个答案:

答案 0 :(得分:0)

int currentapiVersion = android.os.Build.VERSION.SDK_INT;


if (currentapiVersion >= 21) {
    // Do what u want (eneble or disaqble) eg. u want to enable in this condition

 Method dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
            dataMtd.setAccessible(true);
            dataMtd.invoke(connectivityManager, true);




} else {

     // Do what u want (eneble or disaqble) eg. u want to disable here


Method dataMtd =     ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
dataMtd.setAccessible(false);
dataMtd.invoke(connectivityManager, false);
}