溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何使用Android實現(xiàn)WIFI和GPRS網(wǎng)絡(luò)的切換

發(fā)布時間:2021-04-16 09:46:51 來源:億速云 閱讀:498 作者:小新 欄目:移動開發(fā)

小編給大家分享一下如何使用Android實現(xiàn)WIFI和GPRS網(wǎng)絡(luò)的切換,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

在項目的開發(fā)中因為要使用到WIFI和GPRS網(wǎng)絡(luò)的切換,因此就研究了一下通過代碼打開WIFI和GPRS的工作。

無論是切換WIFI還是切換GPRS網(wǎng)絡(luò)都需要設(shè)置相應(yīng)的權(quán)限,所以需要在AndroidManifest.xml文件中加入以下幾行代碼。

<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、切換WIFI網(wǎng)絡(luò)

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

2、切換GPRS網(wǎng)絡(luò)

由于Android沒有提供直接切換GPRS網(wǎng)絡(luò)的方法,通過查看系統(tǒng)源碼發(fā)現(xiàn),系統(tǒng)是調(diào)用IConnectivityManager類中的setMobileDataEnabled(boolean)方法來設(shè)置GPRS網(wǎng)絡(luò)的,由于方法不可見,只能采用反射來調(diào)用,代碼如下。

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");
 // 設(shè)置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);
 // 設(shè)置setMobileDataEnabled方法可訪問
 setMobileDataEnabledMethod.setAccessible(true);
 // 調(diào)用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();
 }
 }

根據(jù)以上所寫就可以做到WIFI網(wǎng)絡(luò)和GPRS網(wǎng)絡(luò)的切換了。

以上是“如何使用Android實現(xiàn)WIFI和GPRS網(wǎng)絡(luò)的切換”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI