- swan.getBatteryInfo
- 方法参数
- object 参数说明
- success 回调函数参数
- 示例
- 图片示例
- 代码示例
- 方法参数
swan.getBatteryInfo
基础库 3.30.2 开始支持,低版本需做兼容处理。
解释:支持在小程序内获取当前设备电量。
Web 态说明:由于浏览器对于 W3C 规范的实现差异,getBatteryInfo 功能仅在 Android 的部分浏览器下可用,对于不支持的浏览器调用该 API 失败会触发 fail 回调。
方法参数
Object object
object 参数说明
属性名 | 类型 | 必填 | 默认值 | 说明 |
---|---|---|---|---|
success |
Function |
否 |
接口调用成功的回调 |
|
fail |
Function |
否 |
接口调用失败的回调函数 |
|
complete |
Function |
否 |
接口调用结束的回调函数(调用成功、失败都会执行) |
success 回调函数参数
参数名 | 类型 | 说明 |
---|---|---|
level |
string |
设备电量, 范围为 1-100 |
isCharging |
boolean |
设备是否正在充电 |
示例
在开发者工具中打开
在开发者工具中打开
在 WEB IDE 中打开
扫码体验
代码示例
请使用百度APP扫码
图片示例
代码示例
- SWAN
- JS
<view class="wrap">
<view class="card-area">
<view class="list-area border-bottom" s-for="item in infoList">
<text class="list-item-key-4">{{item.chineseName}}</text>
<text class="list-item-value">{{item.value}}</text>
</view>
<view class="button-group">
<button bind:tap="getBatteryInfo" type="primary" hover-stop-propagation="true">获取电量信息</button>
</view>
</view>
</view>