MainActivity如下:
[java]
package wy.testnetwork;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
/**
* Demo描述:
* 通過代碼打開wifi或者移動網絡
*
* 學習資料:
* 1 http://blog.csdn.net/way_ping_li/article/details/8493700
* 2 http://stackoverflow.com/questions/12535101/how-can-i-turn-off-3g-data-programmatically-on-android
* 3 http://www.cnblogs.com/willyan/archive/2012/07/03/2574513.html
* 4 http://ask.csdn.net/questions/152
* 5 http://blog.csdn.net/lfdfhl/article/details/10311505
* 6 http://blog.csdn.net/tu_bingbing/article/details/8469871
* 7 http://blog.csdn.net/stevenhu_223/article/details/9052083
* Thank you very much
*
* 備注說明:
* 1 在學習資料1中還涉及到利用代碼打開GPS
* 2 通過代碼打開wifi和移動網絡在某些機型上是不可實現的
*
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toggleWiFi(this, false);
toggleMobileData(this, true);
}
//設置是否打開Wifi
private void toggleWiFi(Context context, boolean enabled) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(enabled);
}
/**
* 設置是否打開移動網絡
*
* 但沒有直接的API可調用,但是我們發現:
* 在ConnectivityManager中有一隱藏的方法setMobileDataEnabled()
* 源碼如下:
* public void setMobileDataEnabled(boolean enabled) {
* try {
* mService.setMobileDataEnabled(enabled);
* } catch (RemoteException e) {
* }
* }
*
* 這裡的重點就是mService,查看其聲明:
* private IConnectivityManager mService;
* 繼續查看源碼可知IConnectivityManager為了一個AIDL(接口interface IConnectivityManager)
*
*
* 調用過程:
* ConnectivityManager中有一隱藏的方法setMobileDataEnabled()
* 在setMobileDataEnabled()中調用了IConnectivityManager中的setMobileDataEnabled(boolean)
*
* 所以我們首先需要反射出ConnectivityManager類的成員變量mService(IConnectivityManager類型)
*/
private void toggleMobileData(Context context, boolean enabled) {
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//ConnectivityManager類
Class<?> connectivityManagerClass = null;
//ConnectivityManager類中的字段
Field connectivityManagerField = null;
//IConnectivityManager接口
Class<?> iConnectivityManagerClass = null;
//IConnectivityManager接口的對象
Object iConnectivityManagerObject = null;
//IConnectivityManager接口的對象的方法
Method setMobileDataEnabledMethod = null;
try {
//取得ConnectivityManager類
connectivityManagerClass = Class.forName(connectivityManager.getClass().getName());
//取得ConnectivityManager類中的字段mService
connectivityManagerField = connectivityManagerClass.getDeclaredField("mService");
//取消訪問私有字段的合法性檢查
//該方法來自java.lang.reflect.AccessibleObject
connectivityManagerField.setAccessible(true);
//實例化mService
//該get()方法來自java.lang.reflect.Field
//一定要注意該get()方法的參數:
//它是mService所屬類的對象
//完整例子請參見:
//http://blog.csdn.net/lfdfhl/article/details/13509839
iConnectivityManagerObject = connectivityManagerField.get(connectivityManager);
//得到mService所屬接口的Class
iConnectivityManagerClass = Class.forName(iConnectivityManagerObject.getClass().getName());
//取得IConnectivityManager接口中的setMobileDataEnabled(boolean)方法
//該方法來自java.lang.Class.getDeclaredMethod
setMobileDataEnabledMethod =
iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
//取消訪問私有方法的合法性檢查
//該方法來自java.lang.reflect.AccessibleObject
setMobileDataEnabledMethod.setAccessible(true);
//調用setMobileDataEnabled方法
setMobileDataEnabledMethod.invoke(iConnectivityManagerObject,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();
}
}
}
AndroidManifest.xml如下:
[html]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="wy.testnetwork"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<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_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="wy.testnetwork.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
main.xml如下:
[html]
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="通過代碼打開wifi或者移動網絡"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dip"
/>
</RelativeLayout>