雷火电竞-中国电竞赛事及体育赛事平台

歡迎來(lái)到入門(mén)教程網(wǎng)!

Android

當(dāng)前位置:主頁(yè) > 軟件編程 > Android >

Android實(shí)現(xiàn)WIFI和GPRS網(wǎng)絡(luò)的切換

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:Android|點(diǎn)擊:

在項(xiàng)目的開(kāi)發(fā)中因?yàn)橐褂玫絎IFI和GPRS網(wǎng)絡(luò)的切換,因此就研究了一下通過(guò)代碼打開(kāi)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ò)的,由于方法不可見(jiàn),只能采用反射來(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ù)以上所寫(xiě)就可以做到WIFI網(wǎng)絡(luò)和GPRS網(wǎng)絡(luò)的切換了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:兩個(gè)surfaceView實(shí)現(xiàn)切換效果

欄    目:Android

下一篇:Flutter 假異步的實(shí)現(xiàn)示例

本文標(biāo)題:Android實(shí)現(xiàn)WIFI和GPRS網(wǎng)絡(luò)的切換

本文地址:http://www.jygsgssxh.com/a1/Android/9115.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有