Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-api
提交
83954b95
U
uni-api
项目概览
DCloud
/
uni-api
通知
679
Star
23
Fork
12
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
3
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-api
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
3
Issue
3
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
83954b95
编写于
7月 05, 2024
作者:
DCloud-yyl
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
统一代码格式,同步web平台实现
上级
2c15ed70
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
105 addition
and
114 deletion
+105
-114
uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts
uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts
+60
-68
uni_modules/uni-getbatteryinfo/utssdk/app-ios/index.uts
uni_modules/uni-getbatteryinfo/utssdk/app-ios/index.uts
+25
-23
uni_modules/uni-getbatteryinfo/utssdk/web/index.js
uni_modules/uni-getbatteryinfo/utssdk/web/index.js
+0
-23
uni_modules/uni-getbatteryinfo/utssdk/web/index.uts
uni_modules/uni-getbatteryinfo/utssdk/web/index.uts
+20
-0
未找到文件。
uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts
浏览文件 @
83954b95
import Context from "android.content.Context";
import BatteryManager from "android.os.BatteryManager";
import { GetBatteryInfo, GetBatteryInfoOptions, GetBatteryInfoSuccess, GetBatteryInfoResult,
GetBatteryInfoSync } from '../interface.uts'
import IntentFilter from 'android.content.IntentFilter';
import Intent from 'android.content.Intent';
import { GetBatteryInfoFailImpl } from '../unierror';
import { GetBatteryInfo, GetBatteryInfoOptions, GetBatteryInfoSuccess, GetBatteryInfoResult,
GetBatteryInfoSync } from '../interface.uts'
import IntentFilter from 'android.content.IntentFilter';
import Intent from 'android.content.Intent';
import { GetBatteryInfoFailImpl } from '../unierror';
/**
* 异步获取电量
*/
export const getBatteryInfo : GetBatteryInfo = function (options : GetBatteryInfoOptions) {
export const getBatteryInfo : GetBatteryInfo = function (options : GetBatteryInfoOptions) {
const context = UTSAndroid.getAppContext();
if (context != null) {
const manager = context.getSystemService(
Context.BATTERY_SERVICE
) as BatteryManager;
const level = manager.getIntProperty(
BatteryManager.BATTERY_PROPERTY_CAPACITY
);
let ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
let batteryStatus = context.registerReceiver(null, ifilter);
let status = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
let isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
const context = UTSAndroid.getAppContext();
if (context != null) {
const manager = context.getSystemService(
Context.BATTERY_SERVICE
) as BatteryManager;
const level = manager.getIntProperty(
BatteryManager.BATTERY_PROPERTY_CAPACITY
);
let ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
let batteryStatus = context.registerReceiver(null, ifilter);
let status = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
let isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
const res : GetBatteryInfoSuccess = {
errMsg: 'getBatteryInfo:ok',
level,
isCharging: isCharging
}
options.success?.(res)
options.complete?.(res)
} else {
let res = new GetBatteryInfoFailImpl(1001);
options.fail?.(res)
options.complete?.(res)
const res : GetBatteryInfoSuccess = {
errMsg: 'getBatteryInfo:ok',
level,
isCharging: isCharging
}
options.success?.(res)
options.complete?.(res)
} else {
let res = new GetBatteryInfoFailImpl(1001);
options.fail?.(res)
options.complete?.(res)
}
}
/**
* 同步获取电量
示例
* 同步获取电量
*/
export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryInfoResult {
const context = UTSAndroid.getAppContext();
if (context != null) {
const manager = context.getSystemService(
Context.BATTERY_SERVICE
) as BatteryManager;
const level = manager.getIntProperty(
BatteryManager.BATTERY_PROPERTY_CAPACITY
);
let ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
let batteryStatus = context.registerReceiver(null, ifilter);
let status = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
let isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
const res : GetBatteryInfoResult = {
level: level,
isCharging: isCharging
};
return res;
}
else {
/**
* 无有效上下文
*/
const res : GetBatteryInfoResult = {
level: -1,
isCharging: false
};
return res;
}
export const getBatteryInfoSync : GetBatteryInfoSync = function () : GetBatteryInfoResult {
const context = UTSAndroid.getAppContext();
if (context != null) {
const manager = context.getSystemService(
Context.BATTERY_SERVICE
) as BatteryManager;
const level = manager.getIntProperty(
BatteryManager.BATTERY_PROPERTY_CAPACITY
);
let ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
let batteryStatus = context.registerReceiver(null, ifilter);
let status = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
let isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
const res : GetBatteryInfoResult = {
level: level,
isCharging: isCharging
};
return res;
}
else {
/**
* 无有效上下文
*/
const res : GetBatteryInfoResult = {
level: -1,
isCharging: false
};
return res;
}
}
uni_modules/uni-getbatteryinfo/utssdk/app-ios/index.uts
浏览文件 @
83954b95
...
...
@@ -2,33 +2,35 @@
import { UIDevice } from "UIKit";
import { GetBatteryInfo, GetBatteryInfoSuccess, GetBatteryInfoResult, GetBatteryInfoSync } from '../interface.uts';
/**
*
导出 获取电量方法
*
异步获取电量
*/
export const getBatteryInfo : GetBatteryInfo = function (options) {
// 开启电量检测
UIDevice.current.isBatteryMonitoringEnabled = true
// 开启电量检测
UIDevice.current.isBatteryMonitoringEnabled = true
// 返回数据
const res : GetBatteryInfoSuccess = {
errMsg: "getBatteryInfo:ok",
level: Number(UIDevice.current.batteryLevel * 100),
isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging,
};
options.success?.(res);
options.complete?.(res);
// 返回数据
const res : GetBatteryInfoSuccess = {
errMsg: "getBatteryInfo:ok",
level: Math.abs(Number(UIDevice.current.batteryLevel * 100)),
isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging,
};
options.success?.(res);
options.complete?.(res);
}
export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryInfoResult {
// 开启电量检测
UIDevice.current.isBatteryMonitoringEnabled = true
/**
* 同步获取电量
*/
export const getBatteryInfoSync : GetBatteryInfoSync = function () : GetBatteryInfoResult {
// 开启电量检测
UIDevice.current.isBatteryMonitoringEnabled = true
// 返回数据
const res : GetBatteryInfoResult = {
level: Number(UIDevice.current.batteryLevel * 100),
isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging,
};
return res;
}
\ No newline at end of file
// 返回数据
const res : GetBatteryInfoResult = {
level: Math.abs(Number(UIDevice.current.batteryLevel * 100)),
isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging,
};
return res;
}
uni_modules/uni-getbatteryinfo/utssdk/web/index.js
已删除
100644 → 0
浏览文件 @
2c15ed70
export
function
getBatteryInfo
(
options
)
{
if
(
navigator
.
getBattery
)
{
navigator
.
getBattery
().
then
(
battery
=>
{
const
res
=
{
errCode
:
0
,
errSubject
:
"
uni-getBatteryInfo
"
,
errMsg
:
'
getBatteryInfo:ok
'
,
level
:
battery
.
level
*
100
,
isCharging
:
battery
.
charging
}
options
.
success
&&
options
.
success
(
res
)
options
.
complete
&&
options
.
complete
(
res
)
})
}
else
{
const
res
=
{
errCode
:
1002
,
errSubject
:
"
uni-getBatteryInfo
"
,
errMsg
:
'
getBatteryInfo:fail navigator.getBattery is unsupported
'
}
options
.
fail
&&
options
.
fail
(
res
)
options
.
complete
&&
options
.
complete
(
res
)
}
}
uni_modules/uni-getbatteryinfo/utssdk/web/index.uts
0 → 100644
浏览文件 @
83954b95
import { GetBatteryInfo, GetBatteryInfoOptions, GetBatteryInfoSuccess } from '../interface.uts'
export const getBatteryInfo : GetBatteryInfo = function (options : GetBatteryInfoOptions) {
if (navigator.getBattery) {
navigator.getBattery().then(battery => {
const res = {
errCode: 0,
errSubject: "uni-getBatteryInfo",
errMsg: 'getBatteryInfo:ok',
level: battery.level * 100,
isCharging: battery.charging
} as GetBatteryInfoSuccess
options.success && options.success(res)
options.complete && options.complete(res)
})
} else {
const res = new UniError("uni-getBatteryInfo", 1002, "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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录