网络状态
返回网络类型, 有效值:
- wifi
- 2g
- 3g
- 4g
- unknown(Android下不常见的网络类型)
- none(无网络)
wx.getNetworkType(OBJECT)
<view class="container"><button bindtap='getNetwork'>获取网络类型</button><view wx:if="{
{
networkType !=''}}"><view>网络类型:{
{
networkType}}</view></view> </view>
//index.js
//获取应用实例
const app = getApp()Page({data: {networkType:''},onLoad: function () {
},getNetwork:function(){
var _this=this;wx.getNetworkType({success: function (res) {
// 返回网络类型, 有效值:// wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)var networkType = res.networkType_this.setData({networkType: networkType})}})}
})
监听网络状态变化
监听网络状态变化。
基础库 1.1.0 开始支持,低版本需做兼容处理
wx.onNetworkStatusChange(CALLBACK)
<view class="container"><button bindtap='getNetwork'>监听网络状态变化</button><view wx:if="{
{
networkType !=''}}"><view>当前是否有网络连接:{
{
isConnected}}</view><view>网络类型:{
{
networkType}}</view></view> </view>
//index.js
//获取应用实例
const app = getApp()Page({data: {networkType:'',isConnected:''},onLoad: function () {
},getNetwork:function(){
var _this=this;wx.onNetworkStatusChange(function (res) {
var networkType = res.networkTypevar isConnected = res.isConnected_this.setData({networkType: networkType,isConnected: isConnected})})}
})