WIFI网卡操作

状态列表:
WIFI_STATE_DISABLED WIFI网卡不可用
WIFI_STATE_DISABLING WIFI正在关闭
WIFI_STATE_ENABLED WIFI网卡可用
WIFI_STATE_ENABLING WIFI网卡正在打开
WIFI_STATE_UNKNOWN 未知网卡状态


权限别表:
CHANGE_NETWORK_STATE 修改网络连接状态权限
CHANGE_WIFI_STATE 修改WIFI连接状态权限
ACCESS_NETWORK_STATE 访问网络状态权限
ACCESS_WIFI_STATE 访问WIFI状态权限



WIFI常用方法和例子:
public class WifiActivity extends Activity {
    /** Called when the activity is first created. */
	WifiManager wm = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button1 = (Button)findViewById(R.id.button1);
        Button button2 = (Button)findViewById(R.id.button2);
        Button button3 = (Button)findViewById(R.id.button3);
        Button button4 = (Button)findViewById(R.id.button4);
        button1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				wm = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
				wm.setWifiEnabled(true);
				System.out.println("--->"+wm.getWifiState());
				Toast.makeText(WifiActivity.this, "当前网卡状态"+wm.getWifiState(), Toast.LENGTH_LONG);
			}
		});
        button2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				wm = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
				wm.setWifiEnabled(false);
				System.out.println("--->"+wm.getWifiState());
				Toast.makeText(WifiActivity.this, "当前网卡状态"+wm.getWifiState(), Toast.LENGTH_LONG);
			}
		});
    		}
}


AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.cn"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="4" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".WifiActivity"
                  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>

PS:AndroidMainfest.xml中的uses-permission部分是需要加入的权限部分

你可能感兴趣的:(wifi)