android开发教程之wifi开发示例
程序员文章站
2023-08-29 20:35:37
1、 wifi网卡的状态wifi网卡的状态信息都以整型变量的形式存放在 android.net.wifi.wifimanager 类中,有以下状态:wifi_state_d...
1、 wifi网卡的状态
wifi网卡的状态信息都以整型变量的形式存放在 android.net.wifi.wifimanager 类中,有以下状态:
wifi_state_disabled
wifi网卡不可用
wifi_state_disabling
wifi网卡正在关闭
wifi_state_enabled
wifi网卡可用
wifi_state_enabling
wifi网卡正在打开
wifi_state_unknown
wifi网卡状态未知
2、 操作wifi 网卡所需的权限
change_network_state
允许应用程序改变网络连接状态
change_wifi_state
允许应用程序改变wifi 连接状态
access_network_state
允许应用程序获取网络的状态信息
access_wifi_state
允许应用程序获得wifi 的状态信息
3、 改变wifi 网卡的状态
对wifi 网卡进行操作需要通过对 wifimanager 对象来进行,获取该对象的方法如下:
复制代码 代码如下:
wifimanager wifimanager=(wifimanager) context.getsystemservice(service.wifi_service);
打开wifi 网卡:
复制代码 代码如下:
wifimanager.setwifienabled(true);
关闭wifi 网卡
复制代码 代码如下:
wifimanager.setwifienabled(false);
获取网卡的当前状态:
复制代码 代码如下:
wifimanager.getwifistate();
示例代码:由于android模拟器不支持wifi操作,所以例子需要在实体手机上运行
androidmanifest.xml
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.bym" 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>
activity
复制代码 代码如下:
package me.bym;
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 {
private button startwifibutton = null;
private button shutdownwifibutton = null;
private button checkstatebutton = null;
private wifimanager wifimanager = null;
private string res = ""; // 用来存放操作结果信息的字符串
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
startwifibutton = (button) findviewbyid(r.id.startwifibutton);
shutdownwifibutton = (button) findviewbyid(r.id.shutdownwifibutton);
checkstatebutton = (button) findviewbyid(r.id.checkstatebutton);
startwifibutton.setonclicklistener(new startwifilistener());
shutdownwifibutton.setonclicklistener(new shutdownwifilistener());
checkstatebutton.setonclicklistener(new checkstatelistener());
}
class startwifilistener implements onclicklistener {
@override
public void onclick(view v) {
wifimanager = (wifimanager) wifiactivity.this
.getsystemservice(context.wifi_service);
if (wifimanager.setwifienabled(true)) {
res = "启动wifi服务成功";
} else {
res = "启动wifi服务失败";
}
toast.maketext(wifiactivity.this, res, toast.length_short).show();
}
}
class shutdownwifilistener implements onclicklistener {
@override
public void onclick(view v) {
wifimanager = (wifimanager) wifiactivity.this
.getsystemservice(context.wifi_service);
if (wifimanager.setwifienabled(false)) {
res = "关闭wifi服务成功";
} else {
res = "关闭wifi服务失败";
}
toast.maketext(wifiactivity.this, res, toast.length_short).show();
}
}
class checkstatelistener implements onclicklistener {
@override
public void onclick(view v) {
wifimanager = (wifimanager) wifiactivity.this
.getsystemservice(context.wifi_service);
switch (wifimanager.getwifistate()) {
case wifimanager.wifi_state_disabled:
res = "wifi已关闭";
break;
case wifimanager.wifi_state_disabling:
res = "wifi正在关闭中";
break;
case wifimanager.wifi_state_enabled:
res = "wifi已启用";
break;
case wifimanager.wifi_state_enabling:
res = "wifi正在启动中";
break;
case wifimanager.wifi_state_unknown:
res = "未知wifi状态";
break;
}
toast.maketext(wifiactivity.this, res, toast.length_short).show();
}
}
}
上一篇: java反射android的r文件的示例
下一篇: 华硕F81S本本看视屏不能全屏