<!-- 以下是使用wifi访问网络所需要的权限 -->
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
public class WifiActivity extends Activity {/** Called when the activity is first created. */private Button startButton = null;private Button stopButton = null;private Button checkButton = null;private WifiManager wifiManager = null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);startButton = (Button)findViewById(R.id.startWifi);stopButton = (Button)findViewById(R.id.stopWifi);checkButton = (Button)findViewById(R.id.checkWifi);startButton.setOnClickListener(new StartWifiListener());stopButton.setOnClickListener(new StopWifiListener());checkButton.setOnClickListener(new CheckWifiListener());}class StartWifiListener implements OnClickListener{@Overridepublic void onClick(View v) {wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);wifiManager.setWifiEnabled(true);System.out.println("wifi state --->" wifiManager.getWifiState());Toast.makeText(WifiActivity.this, "当前Wifi网卡状态为" wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();}}class StopWifiListener implements OnClickListener{@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubwifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);wifiManager.setWifiEnabled(false);System.out.println("wifi state --->" wifiManager.getWifiState());Toast.makeText(WifiActivity.this, "当前Wifi网卡状态为" wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();}}class CheckWifiListener implements OnClickListener{@Overridepublic void onClick(View v) {wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);System.out.println("wifi state --->" wifiManager.getWifiState());Toast.makeText(WifiActivity.this, "当前Wifi网卡状态为" wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();}} }