uniapp 安卓桌面
uniapp 安卓桌面快捷方式创建
使用HBuilderX提供的原生插件ShortcutBadger或自定义原生插件实现桌面快捷方式创建。需在manifest.json中配置插件权限,并在页面调用相关API。
// 示例代码(需配合原生插件)
uni.requireNativePlugin('ShortcutBadger');
const shortcut = uni.requireNativePlugin('ShortcutBadger');
shortcut.createShortcut({
id: 'home',
label: '应用首页',
icon: '/static/logo.png',
intent: 'pages/index/index'
});
配置manifest.json
在项目配置文件中声明安卓权限并添加插件依赖:
{
"app-plus": {
"android": {
"permissions": [
"com.android.launcher.permission.INSTALL_SHORTCUT"
],
"plugins": {
"ShortcutBadger": {
"version": "1.0.0",
"provider": "uniapp官方插件"
}
}
}
}
}
动态创建桌面图标
通过原生安卓开发扩展能力,使用Intent方式创建快捷方式。需编写原生代码并封装为uni-app可调用的模块:
// 原生安卓代码示例
Intent shortcutIntent = new Intent();
shortcutIntent.setClassName("包名", "启动Activity");
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "应用名称");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
context.sendBroadcast(addIntent);
注意事项
- 需真机测试,部分安卓版本可能限制快捷方式创建
- 华为等厂商系统可能需单独适配
- 图标建议使用48x48px或72x72px的PNG格式
- 从Android 8.0开始需动态申请权限
替代方案
考虑使用PWA技术生成桌面图标:
- 配置
manifest.json的PWA设置 - 添加
service-worker.js实现离线缓存 - 用户访问H5页面时可选择"添加到主屏幕"







