android 当中 wifi 控制非常简单。
在android 系统当中 wifi 有五种状态:
// Wi-Fi AP is currently being disabled. The state will change to
public static final int WIFI_AP_STATE_DISABLING = 0;
// Wi-Fi AP is disabled.
public static final int WIFI_AP_STATE_DISABLED = 1;
// Wi-Fi AP is currently being enabled. The state will change to
public static final int WIFI_AP_STATE_ENABLING = 2;
// Wi-Fi AP is enabled.
public static final int WIFI_AP_STATE_ENABLED = 3;
// Wi-Fi AP is in a failed state. This state will occur when an error occurs during
public static final int WIFI_AP_STATE_FAILED = 4;
可以通过系统调用去改变其状态进而控制wifi
实例效果如下:点击按钮来改变 wifi 状态,并用 Toast 打印出当前的 wifi 状态
代码如下
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/openWIFI"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="openWIFI" />
<Button
android:id="@+id/closeWIFI"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="closeWIFI" />
<Button
android:id="@+id/checkState"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="checkState" />
</LinearLayout>
package cie.WIFI;
import cie.WIFI.R;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
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;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startButton = (Button)findViewById(R.id.openWIFI);
stopButton = (Button)findViewById(R.id.closeWIFI);
checkButton = (Button)findViewById(R.id.checkState);
startButton.setOnClickListener(new StartWifiListener());
stopButton.setOnClickListener(new StopWifiListener());
checkButton.setOnClickListener(new CheckWifiListener());
}
class StartWifiListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
wifiManager = (WifiManager)WIFIActivity.this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
System.out.println("wifi state ----" + wifiManager.getWifiState());
Toast toast = Toast.makeText(WIFIActivity.this, "当前网卡状态为"+wifiManager.getWifiState(), Toast.LENGTH_LONG);
toast.show();
}
}
class StopWifiListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
wifiManager = (WifiManager)WIFIActivity.this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
System.out.println("wifi state ----" + wifiManager.getWifiState());
Toast toast = Toast.makeText(WIFIActivity.this, "当前网卡状态为"+wifiManager.getWifiState(), Toast.LENGTH_LONG);
toast.show();
}
}
class CheckWifiListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
wifiManager = (WifiManager)WIFIActivity.this.getSystemService(Context.WIFI_SERVICE);
System.out.println("wifi state ----" + wifiManager.getWifiState());
Toast toast = Toast.makeText(WIFIActivity.this, "当前网卡状态为"+wifiManager.getWifiState(), Toast.LENGTH_LONG);
toast.show();
}
}
}