天天看點

uniapp 安卓快捷方式插件(桌面長按app圖示) Ba-Shortcut

簡介(下載下傳位址)

Ba-Shortcut 是一款App Shortcuts(安卓快捷方式)插件。Shortcuts是指在桌面長按app圖示而出現的快捷方式, 可以為你的app的關鍵功能添加更快速的入口而不用先打開app,點選快捷方式可以通路應用功能, 并且這種快捷方式也可以被拖拽到桌面單獨放置, 變成單獨的桌面快捷方式。

  • 支援建立多個快捷方式
  • 支援自定義圖示
  • 支援設定長文本和短文本(優先顯示長文本,空間不夠時自動顯示短文本)
  • 支援建立、更新、删除
  • 應用添加App Shortcuts是Android 7.1(API 25)的API, 是以隻能在Android 7.1的裝置上顯示, 同時需要launcher支援, 比如Pixel launcher(Pixel裝置的預設launcher), Now launcher(Nexus裝置上的launcher)現在就支援, 其他launcher也可以提供支援.

效果展示

uniapp 安卓快捷方式插件(桌面長按app圖示) Ba-Shortcut

使用方法

引用

script

中引入元件

示例1(App.vue)

可在App.vue中快速內建,建立在onLauncher和onShow都可,點選事件在onShow中監聽

<script>
	const shortcut = uni.requireNativePlugin('Ba-Shortcut')
	export default {
		onLaunch: function() {
			console.log('App Launch')
			//建立快捷方式
			shortcut.create({
					shortcutId: "MyCamera",//快捷方式id
					shortLabel: "随手拍",//快捷方式顯示短文本
					longLabel: "随時随地,拍一拍",//快捷方式顯示長文本
					iconName: "ic_camera",//快捷方式圖示資源名稱,參照‘UI 圖示設定’
				},
				(res) => {
					console.log(res);
				});
		},
		onShow: function() {
			console.log('App Show')
			//快捷方式點選事件監聽
            var args = plus.runtime.arguments;
            if (args) {
                if(args.shortcutId){
                    //args參數如:{"shortLabel":"随手拍","shortcutId":"MyCamera"}
                    //根據快捷方式的 shortcutId 判斷
                    //這裡寫你的處理邏輯
                }
                console.log(args);    
            }
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>
           

示例2(頁面)

可在頁面

script

中調用(示例參考,可根據自己業務和調用方法自行修改)

data() {
			return {
				shortcutId: "MyCamera",
				shortLabel: "随手拍",
				longLabel: "随時随地,拍一拍",
				iconName: "ic_camera",
			}
		},
		methods: {
			create() { //建立
				shortcut.create({
						shortcutId: this.shortcutId,
						shortLabel: this.shortLabel,
						longLabel: this.longLabel,
						iconName: this.iconName,
					},
					(res) => {
						console.log(res);
						uni.showToast({
							title: res.msg,
							icon: "none",
							duration: 3000
						})
					});
			},
			update() { //更新
				shortcut.update({
						shortcutId: this.shortcutId,
						shortLabel: this.shortLabel,
						longLabel: this.longLabel,
					},
					(res) => {
						console.log(res);
						uni.showToast({
							title: res.msg,
							icon: "none",
							duration: 3000
						})
					});
			},
			deleteS() { //删除
				shortcut.delete({
						shortcutId: this.shortcutId,
					},
					(res) => {
						console.log(res);
						uni.showToast({
							title: res.msg,
							icon: "none",
							duration: 3000
						})
					});
			},
		}
           

UI 圖示設定

  • 快捷方式圖示:在項目的 “nativeplugins\Ba-Shortcut\android\res\drawable” 目錄下(沒有就建立),任意添加圖檔(支援png、jpg、xml矢量圖),名字在 create 方法的 “iconName”字段設定即可。如添加自定義圖檔"ic_camera.png",那麼設定 iconName 為 “ic_camera”。注意:更改後需要重新制作基座才能生效,建議提前配置。
    uniapp 安卓快捷方式插件(桌面長按app圖示) Ba-Shortcut

快捷方式點選事件監聽

在應用生命周期app.vue的onLaunch事件中設定監聽:

export default {
		...
        onLaunch: function() {
			this.checkArguments();
			// 重點是以下: 一定要監聽背景恢複 !一定要   
			plus.globalEvent.addEventListener('newintent', (e) => {
				this.checkArguments(); // 檢測啟動參數  
			});
		},
		onShow: function() {
		},
		onHide: function() {
		},
		methods: {
			checkArguments() {
				var args = plus.runtime.arguments;
				if (args) {
				    console.log(args);	
					let args1 = JSON.parse(args);
					if(args1.shortcutId){
						//args參數如:{"shortLabel":"随手拍","shortcutId":"MyCamera"}
						//根據快捷方式的 shortcutId 判斷
						//這裡寫你的處理邏輯
					}
					// 處理args參數,如直達到某新頁面等
				}
			},
		}
		...
	}
           

方法清單

名稱 說明
create 建立快捷方式
update 更新快捷方式,也可以用create重建更新
delete 删除快捷方式

create 方法參數

建立快捷方式

屬性名 類型 必填 預設值 說明
shortcutId String true ‘’ 快捷方式id
shortLabel String true ‘’ 快捷方式顯示短文本
longLabel String true ‘’ 快捷方式顯示長文本
iconName String true ‘’ 快捷方式圖示資源名稱,參照‘UI 圖示設定’

update 方法參數

更新快捷方式

屬性名 類型 必填 預設值 說明
shortcutId String true ‘’ 快捷方式id
shortLabel String true ‘’ 快捷方式顯示短文本
longLabel String true ‘’ 快捷方式顯示長文本

delete 方法參數

删除快捷方式

屬性名 類型 必填 預設值 說明
shortcutId String true ‘’ 快捷方式id

系列插件

圖檔選擇插件 Ba-MediaPicker (文檔)

圖檔編輯插件 Ba-ImageEditor (文檔)

檔案選擇插件 Ba-FilePicker (文檔)

應用消息通知插件 Ba-Notify(文檔)

應用未讀角标插件 Ba-Shortcut-Badge (文檔)

應用開機自啟插件 Ba-Autoboot(文檔)

掃碼原生插件(毫秒級、支援多碼)Ba-Scanner-G(文檔)

掃碼原生插件 - 新(可任意自定義界面版本;支援連續掃碼;支援設定掃碼格式)Ba-Scanner(文檔)

動态修改狀态欄、導航欄背景色、字型顔色插件 Ba-AppBar(文檔)

原生sqlite本地資料庫管理 Ba-Sqlite(文檔)

安卓保活插件(采用多種主流技術) Ba-KeepAlive(文檔)

安卓快捷方式(桌面長按app圖示) Ba-Shortcut(文檔)

自定義圖檔水印(任意位置) Ba-Watermark(文檔)

最接近微信的圖檔壓縮插件 Ba-ImageCompressor(文檔)

視訊壓縮、視訊剪輯插件 Ba-VideoCompressor(文檔)

動态切換應用圖示、名稱(如新年、國慶等) Ba-ChangeIcon(文檔)

原生Toast彈窗提示(穿透所有界面、穿透原生;自定義顔色、圖示 ) Ba-Toast(文檔)

圖檔塗鴉、畫筆 Ba-ImagePaint(文檔)

pdf閱讀(手勢縮放、顯示頁數) Ba-Pdf(文檔)

聲音提示、震動提示、語音播報 Ba-Beep(文檔)

websocket原生服務(自動重連、心跳檢測) Ba-Websocket(文檔)

繼續閱讀