一、代码
1.xml
(1)main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <Button 8 android:id="@+id/startWifi" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:text="启动手机的wifi网卡" 12 /> 13 <Button 14 android:id="@+id/stopWifi" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" 17 android:text="关闭手机的wifi网卡" 18 /> 19 <Button 20 android:id="@+id/checkWifi" 21 android:layout_width="fill_parent" 22 android:layout_height="wrap_content" 23 android:text="检查当前wifi网卡状态" 24 /> 25 </LinearLayout>
(2)AndroidManifest.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.s01_original_e23_wifi" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="8" 9 android:targetSdkVersion="21" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name=".WifiActivity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 </application> 26 <!-- 以下是使用wifi访问网络所需要的权限 --> 27 <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/> 28 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> 29 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 30 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 31 </manifest>
2.java
(1)WifiActivity.java
1 package com.example.s01_original_e23_wifi; 2 3 import android.app.Activity; 4 import android.content.Context; 5 import android.net.wifi.WifiManager; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button; 10 import android.widget.Toast; 11 12 public class WifiActivity extends Activity { 13 /** Called when the activity is first created. */ 14 private Button startButton = null; 15 private Button stopButton = null; 16 private Button checkButton = null; 17 private WifiManager wifiManager = null; 18 @Override 19 public void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.main); 22 startButton = (Button)findViewById(R.id.startWifi); 23 stopButton = (Button)findViewById(R.id.stopWifi); 24 checkButton = (Button)findViewById(R.id.checkWifi); 25 startButton.setOnClickListener(new StartWifiListener()); 26 stopButton.setOnClickListener(new StopWifiListener()); 27 checkButton.setOnClickListener(new CheckWifiListener()); 28 } 29 class StartWifiListener implements OnClickListener{ 30 31 @Override 32 public void onClick(View v) { 33 wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE); 34 wifiManager.setWifiEnabled(true); 35 System.out.println("wifi state --->" + wifiManager.getWifiState()); 36 Toast.makeText(WifiActivity.this, "当前无线网卡状态:" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show(); 37 } 38 } 39 class StopWifiListener implements OnClickListener{ 40 41 @Override 42 public void onClick(View arg0) { 43 // TODO Auto-generated method stub 44 wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE); 45 wifiManager.setWifiEnabled(false); 46 System.out.println("wifi state --->" + wifiManager.getWifiState()); 47 Toast.makeText(WifiActivity.this, "当前无线网卡状态:" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show(); 48 } 49 50 } 51 52 class CheckWifiListener implements OnClickListener{ 53 54 @Override 55 public void onClick(View v) { 56 wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE); 57 System.out.println("wifi state --->" + wifiManager.getWifiState()); 58 Toast.makeText(WifiActivity.this, "当前无线网卡状态:" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show(); 59 } 60 61 } 62 }