Disable mobile network programmatically

2020-02-26 03:22发布

I'm developing an app in which user can enable/disable mobile network on button click. I googled regarding this, but I get the solution of Airplane mode only. In airplane mode, WI-FI and Bluetooth are also disabled. I don't want them to disable by using airplane mode concept. I want only mobile network to be disabled. What are the possible way to implement it pro-grammatically?

3条回答
冷血范
2楼-- · 2020-02-26 03:48

Use below code for Disable/Enable Mobile Network..

private void setMobileConnectionEnabled(Context context, boolean enabled) {
           final ConnectivityManager mConnectivityManager = (ConnectivityManager)  context.getSystemService(Context.CONNECTIVITY_SERVICE);
           final Class mClass = Class.forName(mConnectivityManager.getClass().getName());
           final Field mField = mClass.getDeclaredField("mService");
           mField.setAccessible(true);
           final Object mObject = mField.get(mConnectivityManager);
           final Class mConnectivityManagerClass =  Class.forName(mObject.getClass().getName());
           final Method setMobileDataEnabledMethod = mConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
           setMobileDataEnabledMethod.setAccessible(true);

           setMobileDataEnabledMethod.invoke(mObject, enabled);
}

you must add below permission in menifest.xml

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>

If you want Disable whole network then you can do Disable/Enable AirplaneMode

查看更多
小情绪 Triste *
3楼-- · 2020-02-26 04:03

If you have root-permission you can use this method to disable mobile network:

try {
    Runtime.getRuntime().exec("su -c svc data disable");
} catch (IOException e) {
    Log.d("Error", e.getMessage());
}

If you want to enable mobile network, just replace disable with enable.

查看更多
走好不送
4楼-- · 2020-02-26 04:11

Mobile data is a Global setting (formerly Secure setting). According to the documentation

Global system settings, containing preferences that always apply identically to all defined users. Applications can read these but are not allowed to write; like the "Secure" settings, these are for preferences that the user must explicitly modify through the system UI or specialized APIs for those values.

The reason to not allow apps to change this setting without a direct user involvement is to protect users from paying unexpected bills coming after malicious app "decides" to switch mobile data on while in roaming or for users without data flat rate.

Any existing solution which allows to change this setting uses reflection and exploits a security hole in Android system which might still exist on some phones. In any case, Android team is very serious about such holes and they fix security issues as soon as they find them. Thus any working today's solution will stop working tomorrow. I would not rely on them.

If you want to help users to switch mobile data faster out of your application, you should rather popup a system setting dialog where users can turn mobile data on or off by themselves. In Android 4.0 and higher you can do this like following.

Intent intent = new Intent();
intent.setClassName("com.android.settings",
     "com.android.settings.Settings$DataUsageSummaryActivity");
startActivity(intent);

If you still decide to have a "direct switch", you can use one of reflection-based solutions posted in this thread. But be aware, they won't work on every phone and can stop working after every new system update.

查看更多
登录 后发表回答