ext.md 3.5 KB
Newer Older
D
DCloud_LXH 已提交
1 2 3 4 5 6 7
# 其它api

插件市场和hello示例中还有一批可替代uni内置api的插件或示例代码,比如:
- [剪切板](https://ext.dcloud.net.cn/search?q=%E5%89%AA%E5%88%87%E6%9D%BF&orderBy=Relevance&cat1=8&cat2=81)`uni.setClipboardData``uni.getClipboardData`
- [拨打电话](https://ext.dcloud.net.cn/plugin?id=15235)`uni.makePhoneCall`
- [打开三方应用](https://gitcode.net/dcloud/hello-uni-app-x/-/blob/dev/pages/template/schema/schema.uvue):打开浏览器、应用商店、地图并传参。`plus.openUrl`
- [分享](https://gitcode.net/dcloud/hello-uni-app-x/-/blob/dev/pages/template/share/share.uvue)`uni.shareWithSystem`
W
x  
wanganxp 已提交
8
- 打开文件:`uni.openDocument`[hello uts示例](https://gitcode.net/dcloud/hello-uts/-/blob/dev/uni_modules/uts-nativepage/utssdk/app-android/index.uts)[插件市场](https://ext.dcloud.net.cn/plugin?id=12731)
D
DCloud_LXH 已提交
9 10 11
- [扫码](https://ext.dcloud.net.cn/search?q=%E6%89%AB%E7%A0%81&cat1=8&type=UpdatedDate)`uni.scanCode`
- [文件选择](https://ext.dcloud.net.cn/search?q=%E6%96%87%E4%BB%B6%E9%80%89%E6%8B%A9&cat1=8&cat2=81)`uni.chooseFile`
- [蓝牙](https://ext.dcloud.net.cn/search?q=%E8%93%9D%E7%89%99&orderBy=Relevance&cat1=8&cat2=81)
W
X  
wanganxp 已提交
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
- [NFC](https://ext.dcloud.net.cn/search?q=nfc&orderBy=Relevance&cat1=8&cat2=81)
- 播放音频:`uni.getBackgroundAudioManager``uni.createInnerAudioContext`,参考如下代码:
```vue
<template>
	<button @click="playAudio">播放音频</button>
</template>
<script>
	import MediaPlayer from "android.media.MediaPlayer"; //hx中对这里alt+左键转到定义,查看该库的方法清单
	export default {
		data() {
			return {}
		},
		methods: {
			playAudio() {
				let mediaPlayer = new MediaPlayer()
				try {
					mediaPlayer.setDataSource("https://www.w3cschool.cn/statics/demosource/horse.mp3") // 网络音频文件URL  //大体积的网络音频应在协程里加载,否则容易卡ui
					// mediaPlayer.setDataSource(UTSAndroid.convert2AbsFullPath("/static/horse.mp3")) // 本地音频文件
					mediaPlayer.prepare()
					mediaPlayer.start() // 开始播放
					// mediaPlayer.pause()
					// mediaPlayer.stop()
					// mediaPlayer.release() // 释放MediaPlayer对象
					// 倍速播放参考:https://developer.android.google.cn/reference/android/media/MediaPlayer#setPlaybackParams(android.media.PlaybackParams)
				} catch (e) {
					console.log(e);
				}
			}
		}
	}
</script>
```
W
x  
wanganxp 已提交
44 45 46 47 48 49 50 51 52 53 54 55

uni-app x 中不再支持plus和weex的API。过于plus api中一些常用的api,在uni-app x中进行了替换增补。
- plus.runtime.quit => [uni.exit](./exit.md)
- plus.runtime.install => [uni.installApk](./install-apk.md)
  
还有一些plus API未封装为 uni 的API,但可以直接使用如下代码调用。
- plus.runtime.openURL
```vue
<template>
	<view>
		<button @click="openSchema('https://uniapp.dcloud.io/uni-app-x')">使用浏览器打开指定URL</button>
		<button @click="openSchema('market://details?id=com.tencent.mm')">使用应用商店打开指定App</button>
W
X  
wanganxp 已提交
56
		<button @click="openSchema('androidamap://viewMap?sourceApplication=Hello%20uni-app&poiname=DCloud&lat=39.9631018208&lon=116.3406135236&dev=0')">打开地图坐标</button>
W
x  
wanganxp 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	</view>
</template>
<script>
	import Intent from 'android.content.Intent';
	import Uri from 'android.net.Uri';
	export default {
		data() {
			return {}
		},
		methods: {
			openSchema(url : string) {
				const context = UTSAndroid.getUniActivity()!;
				const uri = Uri.parse(url)
				const intent = new Intent(Intent.ACTION_VIEW, uri)
				intent.setData(uri);
				context.startActivity(intent);
			}
		}
	}
</script>
```