// index.uts
import Context from "android.content.Context";
export function getBatteryLevel() : Int {
const context = UTSAndroid.getAppContext() as Context
return NativeCode.getBatteryLevel(context)
}
// NativeCode.kt
package uts.sdk.modules.ottoBatteryLevel;
import android.content.Context
import android.os.BatteryManager
object NativeCode {
fun getBatteryLevel(context: Context): Int {
val batteryManager = context.getSystemService(Context.BATTERY_SERVICE) as BatteryManager
val batteryLevel = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
return batteryLevel
}
}
报错内容:
11:45:18.359 error: Unresolved reference: NativeCode
11:45:18.359 at uni_modules/test-modules/utssdk/app-android/index.uts:5:8
11:45:18.359 3 | export function getBatteryLevel() : Int {
11:45:18.359 4 | const context = UTSAndroid.getAppContext() as Context
11:45:18.359 5 | return NativeCode.getBatteryLevel(context)
11:45:18.359 | ^
11:45:18.359 6 | }