<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/msg"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/open"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="打开WIFI" />
<Button
android:id="@+id/close"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="关闭WIFI" />
<Button
android:id="@+id/check"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="检查WIFI状态" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.lxh.demo"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".WifiDemo"
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>
<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"/>
</manifest>
package org.lxh.demo;
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.TextView;
public class WifiDemo extends Activity {
private Button open = null; // 按钮组件
private Button close = null; // 按钮组件
private Button check = null; // 按钮组件
private TextView msg = null; // 文本组件
private WifiManager wifiManager = null; // wifi管理
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main); // 配置布局管理器
this.open = (Button) super.findViewById(R.id.open); // 取得组件
this.close = (Button) super.findViewById(R.id.close); // 取得组件
this.check = (Button) super.findViewById(R.id.check); // 取得组件
this.msg = (TextView) super.findViewById(R.id.msg); // 取得组件
this.wifiManager = (WifiManager) super
.getSystemService(Context.WIFI_SERVICE); // 取得Wife服务
this.open.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
WifiDemo.this.wifiManager.setWifiEnabled(true); // 启用Wifi
WifiDemo.this.msg.setText("打开WIFI,状态:"
+ WifiDemo.this.wifiManager.getWifiState());// 设置文字
}
});
this.close.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
WifiDemo.this.wifiManager.setWifiEnabled(false); // 关闭WIFI
WifiDemo.this.msg.setText("关闭WIFI,状态:"
+ WifiDemo.this.wifiManager.getWifiState());// 设置文字
}
});
this.check.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
WifiDemo.this.msg.setText("检查WIFI,状态:"
+ WifiDemo.this.wifiManager.getWifiState());// 设置文字
}
});
}
}