歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android在代碼中打開Wifi、移動網絡和GPS

Android在代碼中打開Wifi、移動網絡和GPS

日期:2017/3/1 9:51:25   编辑:Linux編程

以下方法前2個在2.3.5和4.1.1中測試成功,第3個方法在2.3.5中測試沒問題,但在4.1.1中無效,待解決。詳見Android.provider.Settings.Secure類。

記得在AndroidManifest.xml中聲明相關權限:

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

1. 打開WiFi最簡單,直接調用系統的方法即可:

/**
* WIFI網絡開關 */
private void toggleWiFi(Context context, boolean enabled) {
WifiManager wm = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
wm.setWifiEnabled(enabled);
}

2. 打開移動網絡比較麻煩,系統沒有直接提供開放的方法,只在ConnectivityManager類中有一個不可見的setMobileDataEnabled方法,查看源代碼發現,它是調用IConnectivityManager類中的setMobileDataEnabled(boolean)方法。由於方法不可見,只能采用反射來調用:

/**
* 移動網絡開關
*/
private void toggleMobileData(Context context, boolean enabled) {
ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Class<?> conMgrClass = null; // ConnectivityManager類
Field iConMgrField = null; // ConnectivityManager類中的字段
Object iConMgr = null; // IConnectivityManager類的引用
Class<?> iConMgrClass = null; // IConnectivityManager類
Method setMobileDataEnabledMethod = null; // setMobileDataEnabled方法
try {
// 取得ConnectivityManager類
conMgrClass = Class.forName(conMgr.getClass().getName());
// 取得ConnectivityManager類中的對象mService
iConMgrField = conMgrClass.getDeclaredField("mService");
// 設置mService可訪問
iConMgrField.setAccessible(true);
// 取得mService的實例化類IConnectivityManager
iConMgr = iConMgrField.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();
}
}

3. 打開GPS也比較麻煩,與打開移動網絡一樣,沒有直接的方法。在網上搜索了一下,據說采用反射去調用系統的方法仍然會失敗,有網友用另外一條路實現了同樣的功能,在2.3.5系統測試可以通過,在4.1.1系統中測試無效,待解決:

/**
* <p>GPS開關
* <p>當前若關則打開
* <p>當前若開則關閉
*/
private void toggleGPS() {
Intent gpsIntent = new Intent();
gpsIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
gpsIntent.addCategory("android.intent.category.ALTERNATIVE"); gpsIntent.setData(Uri.parse("custom:3"));
try {
PendingIntent.getBroadcast(this, 0, gpsIntent, 0).send();
} catch (CanceledException e) {
e.printStackTrace();
}
}

Copyright © Linux教程網 All Rights Reserved