Android realizes the switching between WiFi and GPRS networks

In the development of the project, because we want to use the switching between WiFi and GPRS networks, we studied the work of opening WiFi and GPRS through code.

Whether you switch WiFi or GPRS networks, you need to set corresponding permissions, so you need to add the following lines of code in the androidmanifest.xml file.

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

1. Switch WiFi network

public static void toggleWiFi(Context context,boolean enabled) {
 WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
 wm.setWifiEnabled(enabled);
 }

2. Switching GPRS network

Since Android does not provide a method to directly switch the GPRS network, it is found by checking the system source code that the system calls the setmobiledataenabled (Boolean) method in iconnectivitymanager class to set the GPRS network. Since the method is invisible, it can only be called by reflection. The code is as follows.

public static void toggleMobileData(Context context,boolean enabled) {
 ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

 Class<?> conMgrClass = null; // ConnectivityManager类
 Field conMgrField = null; // ConnectivityManager类中的字段
 Object iConMgr = null; // IConnectivityManager类的引用
 Class<?> iConMgrClass = null; // IConnectivityManager类
 Method setMobileDataEnabledMethod = null; // setMobileDataEnabled方法

 try {
 // 取得ConnectivityManager类
 conMgrClass = Class.forName(conMgr.getClass().getName());
 // 取得ConnectivityManager类中的对象mService
 conMgrField = conMgrClass.getDeclaredField("mService");
 // 设置mService可访问
 conMgrField.setAccessible(true);
 // 取得mService的实例化类IConnectivityManager
 iConMgr = conMgrField.get(conMgr);
 // 取得IConnectivityManager类
 iConMgrClass = Class.forName(iConMgr.getClass().getName());
 // 取得IConnectivityManager类中的setMobileDataEnabled(boolean)方法
 setMobileDataEnabledMethod = iConMgrClass.getDeclaredMethod("setMobileDataEnabled",Boolean.TYPE);
 // 设置setMobileDataEnabled方法可访问
 setMobileDataEnabledMethod.setAccessible(true);
 // 调用setMobileDataEnabled方法
 setMobileDataEnabledMethod.invoke(iConMgr,enabled);
 }
 catch (ClassNotFoundException e) {
 e.printStackTrace();
 }
 catch (NoSuchFieldException e) {
 e.printStackTrace();
 }
 catch (SecurityException e) {
 e.printStackTrace();
 }
 catch (NoSuchMethodException e) {
 e.printStackTrace();
 }
 catch (IllegalArgumentException e) {
 e.printStackTrace();
 }
 catch (illegalaccessexception e) {
 e.printStackTrace();
 }
 catch (InvocationTargetException e) {
 e.printStackTrace();
 }
 }

According to the above, you can switch between WiFi network and GPRS network.

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>