Android打开WIFI或者移动网络的代码实现
jopen
11年前
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如下:
<?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如下
<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>