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(接口inte易做图ce 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 =