提交 ee6dbaf8 编写于 作者: 杜庆泉's avatar 杜庆泉

add new module

上级 1685eb3a
......@@ -7,7 +7,7 @@
</view>
</template>
<script>
import getBatteryInfo from "../../uni_modules/uts-batteryinfo";
import getBatteryInfo from "../../uni_modules/uni-getbatteryinfo";
export default {
data() {
......@@ -17,14 +17,16 @@
},
onUnload: function() {},
methods: {
testGetBatteryCapacity: async function() {
var info = await getBatteryInfo()
console.log(info);
uni.showToast({
title: "当前电量:" + info.level + '%',
icon: 'none'
});
testGetBatteryCapacity() {
getBatteryInfo({
success(res) {
console.log(res);
uni.showToast({
title: "当前电量:" + res.level + '%',
icon: 'none'
});
}
})
},
}
......
{
"id": "uts-batteryinfo",
"id": "DCloud-uts-batteryinfo",
"displayName": "uts-batteryinfo",
"version": "1.0.0",
"description": "uts-batteryinfo",
......
{
"id": "uni-getbatteryinfo",
"displayName": "uni-getbatteryinfo",
"version": "1.0.0",
"description": "uni-getbatteryinfo",
"keywords": [
"uni-getbatteryinfo"
],
"repository": "",
"engines": {
"HBuilderX": "^3.6.0"
},
"dcloudext": {
"type": "uts",
"sale": {
"regular": {
"price": "0.00"
},
"sourcecode": {
"price": "0.00"
}
},
"contact": {
"qq": ""
},
"declaration": {
"ads": "",
"data": "",
"permissions": ""
},
"npmurl": ""
},
"uni_modules": {
"dependencies": [],
"encrypt": [],
"platforms": {
"cloud": {
"tcb": "u",
"aliyun": "u"
},
"client": {
"Vue": {
"vue2": "u",
"vue3": "u"
},
"App": {
"app-android": "u",
"app-ios": "u"
},
"H5-mobile": {
"Safari": "u",
"Android Browser": "u",
"微信浏览器(Android)": "u",
"QQ浏览器(Android)": "u"
},
"H5-pc": {
"Chrome": "u",
"IE": "u",
"Edge": "u",
"Firefox": "u",
"Safari": "u"
},
"小程序": {
"微信": "u",
"阿里": "u",
"百度": "u",
"字节跳动": "u",
"QQ": "u",
"钉钉": "u",
"快手": "u",
"飞书": "u",
"京东": "u"
},
"快应用": {
"华为": "u",
"联盟": "u"
}
}
}
}
}
\ No newline at end of file
# uts-batteryinfo
### 开发文档
[UTS 语法](https://uniapp.dcloud.net.cn/tutorial/syntax-uts.html)
[UTS 原生插件](https://uniapp.dcloud.net.cn/plugin/uts-plugin.html)
[Hello UTS](https://gitcode.net/dcloud/hello-uts/-/tree/dev)
\ No newline at end of file
import Context from "android.content.Context";
import BatteryManager from "android.os.BatteryManager";
import { getAppContext } from "io.dcloud.uts.android";
type GetBatteryInfoOptions = {
success?: (res: UTSJSONObject) => void
fail?: (res: UTSJSONObject) => void
complete?: (res: UTSJSONObject) => void
}
export default async function getBatteryInfo(options: GetBatteryInfoOptions) {
const context = getAppContext();
if (context != null) {
const manager = context.getSystemService(
Context.BATTERY_SERVICE
) as BatteryManager;
const level = manager.getIntProperty(
BatteryManager.BATTERY_PROPERTY_CAPACITY
);
const res = {
errMsg: 'getBatteryInfo:ok',
level,
isCharging: manager.isCharging()
}
if (options.success != null) {
options.success!(res)
}
if (options.complete != null) {
options.complete!(res)
}
} else {
const res = {
errMsg: 'getBatteryInfo:fail getAppContext is null'
}
if (options.fail != null) {
options.fail!(res)
}
if (options.complete != null) {
options.complete!(res)
}
}
}
\ No newline at end of file
export default function getBatteryInfo(options) {
return wx.getBatteryInfo(options)
}
export default function getBatteryInfo(options) {
if (navigator.getBattery) {
navigator.getBattery().then(battery => {
const res = {
errMsg: 'getBatteryInfo:ok',
level: battery.level * 100,
isCharging: battery.charging
}
options.success && options.success(res)
options.complete && options.complete(res)
})
} else {
const res = {
errMsg: 'getBatteryInfo:fail navigator.getBattery is unsupported'
}
options.fail && options.fail(res)
options.complete && options.complete(res)
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册