本篇內(nèi)容介紹了“Android怎么實(shí)現(xiàn)WIFI和GPRS網(wǎng)絡(luò)的切換”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

在項(xiàng)目的開發(fā)中因?yàn)橐褂玫絎IFI和GPRS網(wǎng)絡(luò)的切換,因此就研究了一下通過(guò)代碼打開WIFI和GPRS的工作。
無(wú)論是切換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沒(méi)有提供直接切換GPRS網(wǎng)絡(luò)的方法,通過(guò)查看系統(tǒng)源碼發(fā)現(xiàn),系統(tǒng)是調(diào)用IConnectivityManager類中的setMobileDataEnabled(boolean)方法來(lái)設(shè)置GPRS網(wǎng)絡(luò)的,由于方法不可見,只能采用反射來(lái)調(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可訪問(wèn) 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方法可訪問(wèn) 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)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
網(wǎng)站名稱:Android怎么實(shí)現(xiàn)WIFI和GPRS網(wǎng)絡(luò)的切換-創(chuàng)新互聯(lián)
分享URL:http://www.chinadenli.net/article2/dchooc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、網(wǎng)站導(dǎo)航、網(wǎng)站內(nèi)鏈、用戶體驗(yàn)、品牌網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容
移動(dòng)網(wǎng)站建設(shè)知識(shí)