小編給大家分享一下如何使用Android實(shí)現(xiàn)WIFI和GPRS網(wǎng)絡(luò)的切換,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)公司2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)、做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢想脫穎而出為使命,1280元昭陽做網(wǎng)站,已為上家服務(wù),為昭陽各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108
在項(xiàng)目的開發(fā)中因?yàn)橐褂玫絎IFI和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類中的對(duì)象mService conMgrField = conMgrClass.getDeclaredField("mService"); // 設(shè)置mService可訪問 conMgrField.setAccessible(true); // 取得mService的實(shí)例化類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實(shí)現(xiàn)WIFI和GPRS網(wǎng)絡(luò)的切換”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站題目:如何使用Android實(shí)現(xiàn)WIFI和GPRS網(wǎng)絡(luò)的切換
轉(zhuǎn)載注明:http://chinadenli.net/article30/jgceso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、動(dòng)態(tài)網(wǎng)站、網(wǎng)站改版、建站公司、響應(yīng)式網(wǎng)站、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)