Android(安卓)通过程序添加桌面快捷方式
16lz
2021-12-04
原理:通过代码向 Launcher 中的广播接收者发送广播来创建快捷图标。
首先要声明的权限是:
<!--添加图标的权限--> <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
//创建用于发送广播的intent Intent broadcastIntent = new Intent(); // 指定动作名称 broadcastIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); // 指定快捷方式的图标 Parcelable icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher); broadcastIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); // 指定快捷方式的名称 broadcastIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷方式的名称"); // 指定快捷图标激活哪个activity Intent activityIntent = new Intent(); activityIntent.setAction(Intent.ACTION_MAIN); activityIntent.addCategory(Intent.CATEGORY_LAUNCHER); ComponentName component = new ComponentName(this, MyActivity.class); activityIntent.setComponent(component); broadcastIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, activityIntent); // 只创建一次快捷方式 broadcastIntent.putExtra("duplicate", false); sendBroadcast(broadcastIntent);
更多相关文章
- Android零基础入门第8节:HelloWorld,我的第一趟旅程出发点
- Android(安卓)Market中产品图标设计原则
- android 修改AVD的存放位置
- Android(安卓)studio怎么创建文件? Android(安卓)studio新建Java
- Android(安卓)Studio下载安装使用教程
- Android快捷方式解密
- Android(安卓)创建与解析XML(一)—— 概述
- Android(安卓)创建与解析XML(一)—— 概述
- Android(安卓)Activity应用窗口的创建过程分析