utsandroid.md 13.7 KB
Newer Older
D
DCloud_LXH 已提交
1 2
# UTSAndroid

W
x  
wanganxp 已提交
3
app-android平台专有内置对象。在uni-app和uni-app x的uts环境中均可使用。
D
DCloud_LXH 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

## 静态方法


### onAppConfigChange(callback)

<!-- UTSJSON.UTSAndroid.onAppConfigChange.description -->

<!-- UTSJSON.UTSAndroid.onAppConfigChange.param -->

<!-- UTSJSON.UTSAndroid.onAppConfigChange.returnValue -->

<!-- UTSJSON.UTSAndroid.onAppConfigChange.compatibility -->

### offAppConfigChange(callback?)

<!-- UTSJSON.UTSAndroid.offAppConfigChange.description -->

<!-- UTSJSON.UTSAndroid.offAppConfigChange.param -->

<!-- UTSJSON.UTSAndroid.offAppConfigChange.returnValue -->

<!-- UTSJSON.UTSAndroid.offAppConfigChange.compatibility -->

### onAppTrimMemory(callback?)

<!-- UTSJSON.UTSAndroid.onAppTrimMemory.description -->

<!-- UTSJSON.UTSAndroid.onAppTrimMemory.param -->

<!-- UTSJSON.UTSAndroid.onAppTrimMemory.returnValue -->

<!-- UTSJSON.UTSAndroid.onAppTrimMemory.compatibility -->

### offAppTrimMemory(callback?)

<!-- UTSJSON.UTSAndroid.offAppTrimMemory.description -->

<!-- UTSJSON.UTSAndroid.offAppTrimMemory.param -->

<!-- UTSJSON.UTSAndroid.offAppTrimMemory.returnValue -->

<!-- UTSJSON.UTSAndroid.offAppTrimMemory.compatibility -->

### onAppActivityPause(callback)

<!-- UTSJSON.UTSAndroid.onAppActivityPause.description -->

<!-- UTSJSON.UTSAndroid.onAppActivityPause.param -->

<!-- UTSJSON.UTSAndroid.onAppActivityPause.returnValue -->

<!-- UTSJSON.UTSAndroid.onAppActivityPause.compatibility -->

### offAppActivityPause(callback?)

<!-- UTSJSON.UTSAndroid.offAppActivityPause.description -->

<!-- UTSJSON.UTSAndroid.offAppActivityPause.param -->

<!-- UTSJSON.UTSAndroid.offAppActivityPause.returnValue -->

<!-- UTSJSON.UTSAndroid.offAppActivityPause.compatibility -->

### onAppActivityResume(callback)

<!-- UTSJSON.UTSAndroid.onAppActivityResume.description -->

<!-- UTSJSON.UTSAndroid.onAppActivityResume.param -->

<!-- UTSJSON.UTSAndroid.onAppActivityResume.returnValue -->

<!-- UTSJSON.UTSAndroid.onAppActivityResume.compatibility -->

### offAppActivityResume(callback?)

<!-- UTSJSON.UTSAndroid.offAppActivityResume.description -->

<!-- UTSJSON.UTSAndroid.offAppActivityResume.param -->

<!-- UTSJSON.UTSAndroid.offAppActivityResume.returnValue -->

<!-- UTSJSON.UTSAndroid.offAppActivityResume.compatibility -->

### onAppActivityDestroy(callback)

<!-- UTSJSON.UTSAndroid.onAppActivityDestroy.description -->

<!-- UTSJSON.UTSAndroid.onAppActivityDestroy.param -->

<!-- UTSJSON.UTSAndroid.onAppActivityDestroy.returnValue -->

<!-- UTSJSON.UTSAndroid.onAppActivityDestroy.compatibility -->

### offAppActivityDestroy(callback?)

<!-- UTSJSON.UTSAndroid.offAppActivityDestroy.description -->

<!-- UTSJSON.UTSAndroid.offAppActivityDestroy.param -->

<!-- UTSJSON.UTSAndroid.offAppActivityDestroy.returnValue -->

<!-- UTSJSON.UTSAndroid.offAppActivityDestroy.compatibility -->

### onAppActivityResult(callback)

<!-- UTSJSON.UTSAndroid.onAppActivityResult.description -->

<!-- UTSJSON.UTSAndroid.onAppActivityResult.param -->

<!-- UTSJSON.UTSAndroid.onAppActivityResult.returnValue -->

<!-- UTSJSON.UTSAndroid.onAppActivityResult.compatibility -->

### offAppActivityResult(callback?)

<!-- UTSJSON.UTSAndroid.offAppActivityResult.description -->

<!-- UTSJSON.UTSAndroid.offAppActivityResult.param -->

<!-- UTSJSON.UTSAndroid.offAppActivityResult.returnValue -->

<!-- UTSJSON.UTSAndroid.offAppActivityResult.compatibility -->

### onAppActivityBack(callback)

<!-- UTSJSON.UTSAndroid.onAppActivityBack.description -->

<!-- UTSJSON.UTSAndroid.onAppActivityBack.param -->

<!-- UTSJSON.UTSAndroid.onAppActivityBack.returnValue -->

<!-- UTSJSON.UTSAndroid.onAppActivityBack.compatibility -->

### offAppActivityBack(callback?)

<!-- UTSJSON.UTSAndroid.offAppActivityBack.description -->

<!-- UTSJSON.UTSAndroid.offAppActivityBack.param -->

<!-- UTSJSON.UTSAndroid.offAppActivityBack.returnValue -->

<!-- UTSJSON.UTSAndroid.offAppActivityBack.compatibility -->

### getAppContext()

<!-- UTSJSON.UTSAndroid.getAppContext.description -->

<!-- UTSJSON.UTSAndroid.getAppContext.param -->

<!-- UTSJSON.UTSAndroid.getAppContext.returnValue -->

<!-- UTSJSON.UTSAndroid.getAppContext.compatibility -->

杜庆泉's avatar
杜庆泉 已提交
158 159 160 161 162
```uts
let packageName = UTSAndroid.getAppContext()?.packageName
console.log("packageName",packageName)
```

D
DCloud_LXH 已提交
163 164 165 166 167 168 169 170 171 172
### getUniActivity()

<!-- UTSJSON.UTSAndroid.getUniActivity.description -->

<!-- UTSJSON.UTSAndroid.getUniActivity.param -->

<!-- UTSJSON.UTSAndroid.getUniActivity.returnValue -->

<!-- UTSJSON.UTSAndroid.getUniActivity.compatibility -->

杜庆泉's avatar
杜庆泉 已提交
173 174 175 176 177 178 179 180
```uts
// 获取第一个可以响应图像采集行为组件
let takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(UTSAndroid.getUniActivity()!.getPackageManager()) != null) {
	UTSAndroid.getUniActivity()!.startActivityForResult(takePictureIntent, 1001);
}
```

D
DCloud_LXH 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
### getResourcePath(resourceName)

<!-- UTSJSON.UTSAndroid.getResourcePath.description -->

<!-- UTSJSON.UTSAndroid.getResourcePath.param -->

<!-- UTSJSON.UTSAndroid.getResourcePath.returnValue -->

<!-- UTSJSON.UTSAndroid.getResourcePath.compatibility -->

### exit()

<!-- UTSJSON.UTSAndroid.exit.description -->

<!-- UTSJSON.UTSAndroid.exit.param -->

<!-- UTSJSON.UTSAndroid.exit.returnValue -->

<!-- UTSJSON.UTSAndroid.exit.compatibility -->

### getDispatcher(threadName)

<!-- UTSJSON.UTSAndroid.getDispatcher.description -->

<!-- UTSJSON.UTSAndroid.getDispatcher.param -->

<!-- UTSJSON.UTSAndroid.getDispatcher.returnValue -->

<!-- UTSJSON.UTSAndroid.getDispatcher.compatibility -->

杜庆泉's avatar
杜庆泉 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

```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)
```

D
DCloud_LXH 已提交
229 230 231 232 233 234 235 236 237 238
### getAppId()

<!-- UTSJSON.UTSAndroid.getAppId.description -->

<!-- UTSJSON.UTSAndroid.getAppId.param -->

<!-- UTSJSON.UTSAndroid.getAppId.returnValue -->

<!-- UTSJSON.UTSAndroid.getAppId.compatibility -->

杜庆泉's avatar
杜庆泉 已提交
239 240 241 242 243
```uts
let appid = UTSAndroid.getAppId()
console.log("appid",appid)
```

D
DCloud_LXH 已提交
244 245 246 247 248 249 250 251 252 253
### getOsTheme()

<!-- UTSJSON.UTSAndroid.getOsTheme.description -->

<!-- UTSJSON.UTSAndroid.getOsTheme.param -->

<!-- UTSJSON.UTSAndroid.getOsTheme.returnValue -->

<!-- UTSJSON.UTSAndroid.getOsTheme.compatibility -->

杜庆泉's avatar
杜庆泉 已提交
254 255 256 257 258
```uts
let theme = UTSAndroid.getOsTheme()
console.log("OsTheme",theme)
```

D
DCloud_LXH 已提交
259 260 261 262 263 264 265 266 267 268
### isUniMp()

<!-- UTSJSON.UTSAndroid.isUniMp.description -->

<!-- UTSJSON.UTSAndroid.isUniMp.param -->

<!-- UTSJSON.UTSAndroid.isUniMp.returnValue -->

<!-- UTSJSON.UTSAndroid.isUniMp.compatibility -->

杜庆泉's avatar
杜庆泉 已提交
269 270 271 272 273
```uts
let isUniMp = UTSAndroid.isUniMp()
console.log("isUniMp",isUniMp)
```

D
DCloud_LXH 已提交
274 275 276 277 278 279 280 281 282 283
### getAppName()

<!-- UTSJSON.UTSAndroid.getAppName.description -->

<!-- UTSJSON.UTSAndroid.getAppName.param -->

<!-- UTSJSON.UTSAndroid.getAppName.returnValue -->

<!-- UTSJSON.UTSAndroid.getAppName.compatibility -->

杜庆泉's avatar
杜庆泉 已提交
284 285 286 287 288
```uts
let appName = UTSAndroid.getAppName()
console.log("AppName",appName)
```

D
DCloud_LXH 已提交
289 290 291 292 293 294 295 296 297 298
### getAppVersion()

<!-- UTSJSON.UTSAndroid.getAppVersion.description -->

<!-- UTSJSON.UTSAndroid.getAppVersion.param -->

<!-- UTSJSON.UTSAndroid.getAppVersion.returnValue -->

<!-- UTSJSON.UTSAndroid.getAppVersion.compatibility -->

杜庆泉's avatar
杜庆泉 已提交
299 300 301 302 303
```uts
let appVersion = UTSAndroid.getAppVersion()
console.log("AppVersion",appVersion)
```

D
DCloud_LXH 已提交
304 305 306 307 308 309 310 311 312 313
### getInnerVersion()

<!-- UTSJSON.UTSAndroid.getInnerVersion.description -->

<!-- UTSJSON.UTSAndroid.getInnerVersion.param -->

<!-- UTSJSON.UTSAndroid.getInnerVersion.returnValue -->

<!-- UTSJSON.UTSAndroid.getInnerVersion.compatibility -->

杜庆泉's avatar
杜庆泉 已提交
314 315 316 317 318
```uts
let innerVersion = UTSAndroid.getInnerVersion()
console.log("InnerVersion",innerVersion)
```

D
DCloud_LXH 已提交
319 320 321 322 323 324 325 326 327 328
### isUniAppX()

<!-- UTSJSON.UTSAndroid.isUniAppX.description -->

<!-- UTSJSON.UTSAndroid.isUniAppX.param -->

<!-- UTSJSON.UTSAndroid.isUniAppX.returnValue -->

<!-- UTSJSON.UTSAndroid.isUniAppX.compatibility -->

杜庆泉's avatar
杜庆泉 已提交
329 330 331 332
```uts
let isUniAppX = UTSAndroid.isUniAppX()
console.log("isUniAppX",isUniAppX)
```
杜庆泉's avatar
杜庆泉 已提交
333 334 335 336 337 338 339 340 341 342 343

### getAppCachePath

<!-- UTSJSON.UTSAndroid.getAppCachePath.description -->

<!-- UTSJSON.UTSAndroid.getAppCachePath.param -->

<!-- UTSJSON.UTSAndroid.getAppCachePath.returnValue -->

<!-- UTSJSON.UTSAndroid.getAppCachePath.compatibility -->

杜庆泉's avatar
杜庆泉 已提交
344
* 如果编译报错找不到该方法,手动删除“uniapp-runextension”插件重新安装 或者 重新下载安装最新版本 HBuilder X 即可修复*
杜庆泉's avatar
杜庆泉 已提交
345

杜庆泉's avatar
杜庆泉 已提交
346

杜庆泉's avatar
杜庆泉 已提交
347 348 349 350 351 352
```uts
let appCachePath = UTSAndroid.getAppCachePath()
console.log("AppCachePath",appCachePath)
```


D
DCloud_LXH 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
### rpx2px(rpx:number) : number;

<!-- UTSJSON.UTSAndroid.rpx2px.description -->

<!-- UTSJSON.UTSAndroid.rpx2px.param -->

<!-- UTSJSON.UTSAndroid.rpx2px.returnValue -->

<!-- UTSJSON.UTSAndroid.rpx2px.compatibility -->

### devicePX2px(devicePX:number) : number;

<!-- UTSJSON.UTSAndroid.devicePX2px.description -->

<!-- UTSJSON.UTSAndroid.devicePX2px.param -->

<!-- UTSJSON.UTSAndroid.devicePX2px.returnValue -->

<!-- UTSJSON.UTSAndroid.devicePX2px.compatibility -->


### isPrivacyAgree() : boolean;

<!-- UTSJSON.UTSAndroid.isPrivacyAgree.description -->

<!-- UTSJSON.UTSAndroid.isPrivacyAgree.param -->

<!-- UTSJSON.UTSAndroid.isPrivacyAgree.returnValue -->

<!-- UTSJSON.UTSAndroid.isPrivacyAgree.compatibility -->


### setPrivacyAgree(state:boolean):void;

<!-- UTSJSON.UTSAndroid.setPrivacyAgree.description -->

<!-- UTSJSON.UTSAndroid.setPrivacyAgree.param -->

<!-- UTSJSON.UTSAndroid.setPrivacyAgree.returnValue -->

<!-- UTSJSON.UTSAndroid.setPrivacyAgree.compatibility -->

### resetPrivacyAgree():void;

<!-- UTSJSON.UTSAndroid.resetPrivacyAgree.description -->

<!-- UTSJSON.UTSAndroid.resetPrivacyAgree.param -->

<!-- UTSJSON.UTSAndroid.resetPrivacyAgree.returnValue -->

<!-- UTSJSON.UTSAndroid.resetPrivacyAgree.compatibility -->

### requestSystemPermission

<!-- UTSJSON.UTSAndroid.requestSystemPermission.description -->

<!-- UTSJSON.UTSAndroid.requestSystemPermission.param -->

<!-- UTSJSON.UTSAndroid.requestSystemPermission.returnValue -->

<!-- UTSJSON.UTSAndroid.requestSystemPermission.compatibility -->

杜庆泉's avatar
杜庆泉 已提交
415 416 417
```uts

	let permissionNeed = ["android.permission.CAMERA"]
杜庆泉's avatar
杜庆泉 已提交
418
    // 请求拍照权限
杜庆泉's avatar
杜庆泉 已提交
419 420
	UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, permissionNeed, function (allRight : boolean, _ : string[]) {
		if (allRight) {
D
DCloud_LXH 已提交
421
			// 权限请求成功
杜庆泉's avatar
杜庆泉 已提交
422
		} else {
杜庆泉's avatar
杜庆泉 已提交
423
			//用户拒绝了部分权限
杜庆泉's avatar
杜庆泉 已提交
424 425
		}
	}, function (_ : boolean, _ : string[]) {
杜庆泉's avatar
杜庆泉 已提交
426
		//用户拒绝了部分权限
杜庆泉's avatar
杜庆泉 已提交
427 428 429
	})

```
D
DCloud_LXH 已提交
430 431 432 433 434 435 436 437 438 439 440

### checkSystemPermissionGranted

<!-- UTSJSON.UTSAndroid.checkSystemPermissionGranted.description -->

<!-- UTSJSON.UTSAndroid.checkSystemPermissionGranted.param -->

<!-- UTSJSON.UTSAndroid.checkSystemPermissionGranted.returnValue -->

<!-- UTSJSON.UTSAndroid.checkSystemPermissionGranted.compatibility -->

杜庆泉's avatar
杜庆泉 已提交
441 442 443 444 445 446 447 448 449
```uts
let permissionCheck = ["android.permission.CAMERA"]
// 请求拍照权限
if (UTSAndroid.checkSystemPermissionGranted(UTSAndroid.getUniActivity()!, permissionCheck)) {
	console.log("当前已具备指定权限")
}else{
	console.log("当前不具备指定权限")
}
```
D
DCloud_LXH 已提交
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473


### gotoSystemPermissionActivity

<!-- UTSJSON.UTSAndroid.gotoSystemPermissionActivity.description -->

<!-- UTSJSON.UTSAndroid.gotoSystemPermissionActivity.param -->

<!-- UTSJSON.UTSAndroid.gotoSystemPermissionActivity.returnValue -->

<!-- UTSJSON.UTSAndroid.gotoSystemPermissionActivity.compatibility -->


### getSystemPermissionDenied

<!-- UTSJSON.UTSAndroid.getSystemPermissionDenied.description -->

<!-- UTSJSON.UTSAndroid.getSystemPermissionDenied.param -->

<!-- UTSJSON.UTSAndroid.getSystemPermissionDenied.returnValue -->

<!-- UTSJSON.UTSAndroid.getSystemPermissionDenied.compatibility -->


杜庆泉's avatar
杜庆泉 已提交
474 475 476 477 478 479 480
```uts
	let permissionNeed = ["android.permission.READ_PHONE_STATE"]
	if (UTSAndroid.getSystemPermissionDenied(UTSAndroid.getUniActivity()!, permissionNeed).isEmpty()) {
    	console.log("当前已具备指定权限")
	}
```

D
DCloud_LXH 已提交
481 482 483 484 485 486 487 488 489 490
### convert2AbsFullPath

<!-- UTSJSON.UTSAndroid.convert2AbsFullPath.description -->

<!-- UTSJSON.UTSAndroid.convert2AbsFullPath.param -->

<!-- UTSJSON.UTSAndroid.convert2AbsFullPath.returnValue -->

<!-- UTSJSON.UTSAndroid.convert2AbsFullPath.compatibility -->

D
DCloud_LXH 已提交
491 492 493 494 495 496 497 498 499 500
### getFileProviderUri

<!-- UTSJSON.UTSAndroid.getFileProviderUri.description -->

<!-- UTSJSON.UTSAndroid.getFileProviderUri.param -->

<!-- UTSJSON.UTSAndroid.getFileProviderUri.returnValue -->

<!-- UTSJSON.UTSAndroid.getFileProviderUri.compatibility -->

D
DCloud_LXH 已提交
501 502 503 504 505 506 507 508 509 510 511
```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);

```

杜庆泉's avatar
杜庆泉 已提交
512
<!-- UTSJSON.UTSAndroid.tutorial -->
D
DCloud_LXH 已提交
513 514 515 516 517 518 519 520 521 522

### getJavaClass

<!-- UTSJSON.UTSAndroid.getJavaClass.description -->

<!-- UTSJSON.UTSAndroid.getJavaClass.param -->

<!-- UTSJSON.UTSAndroid.getJavaClass.returnValue -->

<!-- UTSJSON.UTSAndroid.getJavaClass.compatibility -->
杜庆泉's avatar
杜庆泉 已提交
523

杜庆泉's avatar
杜庆泉 已提交
524 525 526 527 528 529 530 531 532 533 534 535 536 537
```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
}
```

杜庆泉's avatar
杜庆泉 已提交
538 539 540 541 542 543 544 545 546
### getTopPageActivity

<!-- UTSJSON.UTSAndroid.getTopPageActivity.description -->

<!-- UTSJSON.UTSAndroid.getTopPageActivity.param -->

<!-- UTSJSON.UTSAndroid.getTopPageActivity.returnValue -->

<!-- UTSJSON.UTSAndroid.getTopPageActivity.compatibility -->