写法有问题,修改后的关键代码 我复制在下面了。这样测试可以运行
import { MyApiOptions, MyApiResult, MyApi, MyApiSync } from '../interface.uts';
import { MyApiFailImpl } from '../unierror';
import CameraManager from "android.hardware.camera2.CameraManager";
import Build from 'android.os.Build';
import Context from "android.content.Context"
import CameraDevice from "android.hardware.camera2.CameraDevice"
import Handler from "android.os.Handler"
import Looper from "android.os.Looper"
import Manifest from "android.Manifest";
// 引用android api
import BatteryManager from "android.os.BatteryManager";// 引用uts环境库
export function getBatteryCapacity() : string {
// 获取android系统 application上下文
const context = UTSAndroid.getUniActivity()!;
const manager = context.getSystemService(
Context.BATTERY_SERVICE
) as BatteryManager;
const currentLevel : number = manager.getIntProperty(
BatteryManager.BATTERY_PROPERTY_CAPACITY
);
console.log(currentLevel)
return '' + currentLevel + '%';
}
const cameraManager = UTSAndroid.getUniActivity()!.getSystemService(Context.CAMERA_SERVICE) as CameraManager
export const myApi : MyApi = function (options : MyApiOptions) {
if (options.paramA == true) {
// 返回数据
const res : MyApiResult = {
fieldA: 85,
fieldB: true,
fieldC: 'some message'
};
options.success?.(res);
options.complete?.(res);
} else {
// 返回错误
const err = new MyApiFailImpl(9010001);
options.fail?.(err)
options.complete?.(err)
}
}
export const myApiSync : MyApiSync = function (paramA : boolean) : MyApiResult {
// 返回数据,根据插件功能获取实际的返回值
const res : MyApiResult = {
fieldA: 85,
fieldB: paramA,
fieldC: 'some message'
};
return res;
}
export function getModelNum() {
console.log(Build.MODEL)
}
class CustomCallback extends CameraDevice.StateCallback{
constructor (){
super();
}
override onOpened(camera: CameraDevice):void {
console.log("onOpened")
}
override onDisconnected(camera: CameraDevice):void {
console.log("onDisconnected")
}
override onError(camera: CameraDevice, error: Int):void {
console.log("onError")
}
}
export function getCameraIds() {
let permissionNeed = [Manifest.permission.CAMERA];
UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, permissionNeed, function (allRight:boolean,_:string[]) {
console.log("用户同意了权限",allRight)
const Cameradevice = cameraManager.getCameraCharacteristics('0')
console.log(cameraManager)
let mainHandler = Handler(Looper.getMainLooper())
const aa = cameraManager.openCamera('0', new CustomCallback(), mainHandler)
}, function (_:boolean,_:string[]) {
console.log("用户拒绝了部分权限:")
})
// cameraManager.getCameraIdList()
// let ca : number = cameraManager.openCamera('0')
// console.log(ca)
}
即时通讯开发 (作者)
楼下我放了附件,您帮忙看看谢谢
2024-01-24 12:50