utsandroid.md 11.8 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

## 静态方法


### 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 -->

### getUniActivity()

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

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

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

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

### 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
杜庆泉 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

```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 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
### getAppId()

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

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

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

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

### getOsTheme()

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

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

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

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

### isUniMp()

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

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

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

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

### getAppName()

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

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

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

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

### getAppVersion()

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

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

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

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

### getInnerVersion()

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

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

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

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

### isUniAppX()

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

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

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

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

杜庆泉's avatar
杜庆泉 已提交
286 287 288 289 290 291 292 293 294 295 296

### getAppCachePath

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

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

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

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

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

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

D
DCloud_LXH 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
### 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
杜庆泉 已提交
362 363 364
```uts

	let permissionNeed = ["android.permission.CAMERA"]
杜庆泉's avatar
杜庆泉 已提交
365
    // 请求拍照权限
杜庆泉's avatar
杜庆泉 已提交
366 367
	UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, permissionNeed, function (allRight : boolean, _ : string[]) {
		if (allRight) {
杜庆泉's avatar
杜庆泉 已提交
368
			// 权限请求成功			
杜庆泉's avatar
杜庆泉 已提交
369
		} else {
杜庆泉's avatar
杜庆泉 已提交
370
			//用户拒绝了部分权限
杜庆泉's avatar
杜庆泉 已提交
371 372
		}
	}, function (_ : boolean, _ : string[]) {
杜庆泉's avatar
杜庆泉 已提交
373
		//用户拒绝了部分权限
杜庆泉's avatar
杜庆泉 已提交
374 375 376
	})

```
D
DCloud_LXH 已提交
377 378 379 380 381 382 383 384 385 386 387

### checkSystemPermissionGranted

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

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

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

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

杜庆泉's avatar
杜庆泉 已提交
388 389 390 391 392 393 394 395 396
```uts
let permissionCheck = ["android.permission.CAMERA"]
// 请求拍照权限
if (UTSAndroid.checkSystemPermissionGranted(UTSAndroid.getUniActivity()!, permissionCheck)) {
	console.log("当前已具备指定权限")
}else{
	console.log("当前不具备指定权限")
}
```
D
DCloud_LXH 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420


### 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
杜庆泉 已提交
421 422 423 424 425 426 427
```uts
	let permissionNeed = ["android.permission.READ_PHONE_STATE"]
	if (UTSAndroid.getSystemPermissionDenied(UTSAndroid.getUniActivity()!, permissionNeed).isEmpty()) {
    	console.log("当前已具备指定权限")
	}
```

D
DCloud_LXH 已提交
428 429 430 431 432 433 434 435 436 437
### convert2AbsFullPath

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

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

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

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

D
DCloud_LXH 已提交
438 439 440 441 442 443 444 445 446 447
### getFileProviderUri

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

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

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

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

D
DCloud_LXH 已提交
448 449 450 451 452 453 454 455 456 457 458 459
```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
杜庆泉 已提交
460
<!-- UTSJSON.UTSAndroid.tutorial -->