android开发教程之wifi开发示例
时间:2022-03-04 11:05:47
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();
}
}
}
标签:android,wifi
0
投稿
猜你喜欢
java实现简单的加减乘除计算器
2022-11-14 01:25:59
SpringCloud远程服务调用实战笔记
2022-07-13 06:40:19
Android中使用七牛云存储进行图片上传下载的实例代码
2023-12-17 11:56:35
C# 获取客户端IPv4地址的示例代码
2022-09-12 12:39:34
SpringCloud OpenFeign超详细讲解模板化远程通信的实现
2023-04-27 06:43:29
Mybatis如何解决sql中like通配符模糊匹配问题
2023-12-22 19:39:52
Java学习之线程同步与线程间通信详解
2022-05-27 08:03:59
android选项卡TabHost功能用法详解
2021-09-24 01:41:27
C++索引越界的解决方法
2023-07-21 21:53:17
Kotlin开发笔记之委托属性与区间(译)
2022-07-12 19:14:32
HighCharts图表控件在ASP.NET WebForm中的使用总结(全)
2022-07-13 02:11:12
Groovy动态语言使用教程简介
2022-04-28 15:05:54
Java SSM框架如何配置静态资源加载
2023-03-22 17:40:01
Android创建简单发送和接收短信应用
2022-01-04 18:31:08
java学生管理系统界面简单实现(全)
2023-11-28 21:35:25
Java使用Tesseract-Ocr识别数字
2022-12-30 05:15:44
Log4j新手快速入门教程
2023-06-01 11:59:44
C# 使用鼠标点击对Chart控件实现数据提示效果
2023-03-05 14:20:06
SpringBoot配置文件中密码属性加密的实现
2022-07-08 18:32:03
C# WinForm程序处理后台繁忙导致前台控件假死现象解决方法
2021-09-02 12:22:44