Android打开WIFI或者移动网络的代码实现
发表于2年前(2013-12-17 23:00) 阅读(
799) | 评论(
0)
14人收藏此文章, 我要收藏赞0
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; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); toggleWiFi(this, false); toggleMobileData(this, true); } private void toggleWiFi(Context context, boolean enabled) { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); wifiManager.setWifiEnabled(enabled); } private void toggleMobileData(Context context, boolean enabled) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); Class<?> connectivityManagerClass = null; Field connectivityManagerField = null; Class<?> iConnectivityManagerClass = null; Object iConnectivityManagerObject = null; Method setMobileDataEnabledMethod = null; try { connectivityManagerClass = Class.forName(connectivityManager.getClass().getName()); connectivityManagerField = connectivityManagerClass.getDeclaredField("mService"); connectivityManagerField.setAccessible(true); iConnectivityManagerObject = connectivityManagerField.get(connectivityManager); iConnectivityManagerClass = Class.forName(iConnectivityManagerObject.getClass().getName()); setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE); setMobileDataEnabledMethod.setAccessible(true); 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>