http://blog.csdn.net/wwj_748/article/details/7746319
布局文件:main.xml
[html] view plaincopyprint?
<SPAN style="FONT-FAMILY: KaiTi_GB2312; FONT-SIZE: 18px"><?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/startWifi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="启动手机的wifi网卡"
/>
<Button
android:id="@+id/stopWifi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="关闭手机的wifi网卡"
/>
<Button
android:id="@+id/checkWifi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="检查当前wifi网卡状态"
/>
</LinearLayout></SPAN>
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/startWifi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="启动手机的wifi网卡"
/>
<Button
android:id="@+id/stopWifi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="关闭手机的wifi网卡"
/>
<Button
android:id="@+id/checkWifi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="检查当前wifi网卡状态"
/>
</LinearLayout>
在manifest文件设置权限:AndroidManifest.xml
[html] view plaincopyprint?
<SPAN style="FONT-FAMILY: KaiTi_GB2312; FONT-SIZE: 18px"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mars.wifi" android:versionCode="1" android:versionName="1.0">
<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>
<uses-sdk android:minSdkVersion="4" />
<!-- 以下是使用wifi访问网络所需要的权限 -->
<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>
</manifest> </SPAN>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mars.wifi" android:versionCode="1" android:versionName="1.0">
<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>
<uses-sdk android:minSdkVersion="4" />
<!-- 以下是使用wifi访问网络所需要的权限 -->
<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>
</manifest>
==>WifiActivity.java
[java] view plaincopyprint?
<SPAN style="FONT-FAMILY: KaiTi_GB2312; FONT-SIZE: 18px">package mars.wifi;
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.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{
@Override
public 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{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
wifiManager = (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{
@Override
public 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();
}
}
}</SPAN>