# UTSAndroid app-android平台专有内置对象。在uni-app和uni-app x的uts环境中均可使用。 ## 静态方法 ### onAppConfigChange(callback) ### offAppConfigChange(callback?) ### onAppTrimMemory(callback?) ### offAppTrimMemory(callback?) ### onAppActivityPause(callback) ### offAppActivityPause(callback?) ### onAppActivityResume(callback) ### offAppActivityResume(callback?) ### onAppActivityDestroy(callback) ### offAppActivityDestroy(callback?) ### onAppActivityResult(callback) ### offAppActivityResult(callback?) ### onAppActivityBack(callback) ### offAppActivityBack(callback?) ### getAppContext() ```uts let packageName = UTSAndroid.getAppContext()?.packageName console.log("packageName",packageName) ``` ### getUniActivity() ```uts // 获取第一个可以响应图像采集行为组件 let takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(UTSAndroid.getUniActivity()!.getPackageManager()) != null) { UTSAndroid.getUniActivity()!.startActivityForResult(takePictureIntent, 1001); } ``` ### getResourcePath(resourceName) ### exit() ### getDispatcher(threadName) ```uts // 不传任何参数,得到是当前代码运行线程 let currentDispatcher = UTSAndroid.getDispatcher() console.log("currentDispatcher",currentDispatcher) // 期望在 io 任务队列执行 UTSAndroid.getDispatcher("io").async(function(_){ console.log("当前任务执行在",Thread.currentThread().getName()) // 期望在 主线程 任务队列执行 UTSAndroid.getDispatcher("main").async(function(_){ console.log("当前任务执行在",Thread.currentThread().getName()) currentDispatcher.async(function(_){ console.log("起始任务执行在",Thread.currentThread().getName()) },null) },null) },null) ``` ### getAppId() ```uts let appid = UTSAndroid.getAppId() console.log("appid",appid) ``` ### getOsTheme() ```uts let theme = UTSAndroid.getOsTheme() console.log("OsTheme",theme) ``` ### isUniMp() ```uts let isUniMp = UTSAndroid.isUniMp() console.log("isUniMp",isUniMp) ``` ### getAppName() ```uts let appName = UTSAndroid.getAppName() console.log("AppName",appName) ``` ### getAppVersion() ```uts let appVersion = UTSAndroid.getAppVersion() console.log("AppVersion",appVersion) ``` ### getInnerVersion() ```uts let innerVersion = UTSAndroid.getInnerVersion() console.log("InnerVersion",innerVersion) ``` ### isUniAppX() ```uts let isUniAppX = UTSAndroid.isUniAppX() console.log("isUniAppX",isUniAppX) ``` ### getAppCachePath() * 如果编译报错找不到该方法,手动删除“uniapp-runextension”插件重新安装 或者 重新下载安装最新版本 HBuilder X 即可修复* ```uts let appCachePath = UTSAndroid.getAppCachePath() console.log("AppCachePath",appCachePath) ``` ### rpx2px(rpx:number) : number; 推荐使用uni.rpx2px来替代本API,[详见](https://doc.dcloud.net.cn/uni-app-x/api/rpx2px.html) ### devicePX2px(devicePX:number) : number; ### isPrivacyAgree() : boolean; ### setPrivacyAgree(state:boolean):void; ### resetPrivacyAgree():void; ### requestSystemPermission() ```uts let permissionNeed = ["android.permission.CAMERA"] // 请求拍照权限 UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, permissionNeed, function (allRight : boolean, _ : string[]) { if (allRight) { // 权限请求成功 } else { //用户拒绝了部分权限 } }, function (_ : boolean, _ : string[]) { //用户拒绝了部分权限 }) ``` 请求权限后有三种情况: + 用户允许了全部权限请求,会通过 `success`回调通知调用者,并且此时`allRight`参数为 `true` + 用户拒绝了全部权限请求,会通过 `fail` 回调通知调用者,`doNotAskAgain` 参数标识了用户拒绝时是否选择了`不再询问` + 用户允许了部分请求,拒绝了部分权限请求,此时既会调用`success`也会调用`fail`。由其中的 string数组参数 标识具体被拒绝/允许的权限 ### checkSystemPermissionGranted() ```uts let permissionCheck = ["android.permission.CAMERA"] // 请求拍照权限 if (UTSAndroid.checkSystemPermissionGranted(UTSAndroid.getUniActivity()!, permissionCheck)) { console.log("当前已具备指定权限") }else{ console.log("当前不具备指定权限") } ``` ### gotoSystemPermissionActivity() ### getSystemPermissionDenied() ```uts let permissionNeed = ["android.permission.READ_PHONE_STATE"] if (UTSAndroid.getSystemPermissionDenied(UTSAndroid.getUniActivity()!, permissionNeed).isEmpty()) { console.log("当前已具备指定权限") } ``` ### convert2AbsFullPath(path:string) ### getFileProviderUri(file:File) ```ts // 使用外部应用打开项目内置图片资源 let file = new File(UTSAndroid.getResourcePath("static/logo.png")) const uri = UTSAndroid.getFileProviderUri(file) const intent = new Intent(Intent.ACTION_VIEW, uri) intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) const context = UTSAndroid.getUniActivity()!; context.startActivity(intent); ``` ### getJavaClass(any:Any) ```uts export function getJavaClassTest() : boolean { let dispatcherClass = UTSAndroid.getJavaClass(UTSAndroid.getDispatcher()) if("io.dcloud.uts.task.UTSTaskDispatcher" == dispatcherClass.name){ return true } let applicationClass = UTSAndroid.getJavaClass(UTSAndroid.getAppContext()!) if("io.dcloud.uniapp.UniApplication" == applicationClass.name){ return true } return false } ``` ### getTopPageActivity()