android直接在桌面生成快捷方式
16lz
2021-01-24
Android在桌面上生成快捷方式有两种情况,一种是直接在桌面直接生成;一种是长按桌面,在弹出的快捷菜单中生成。这是讨论第一种,直接在桌面生成。
这种是通过广播(Broadcast)的形式向Luncher发送请求生成快捷方式的。
首先添加权限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
下面就是代码层的实现:
在activity中创建一个创建快捷方式的方法:addShortCut();
public boolean addShortcut() {// 创建快捷方式的IntentIntent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");// 不允许重复创建shortcutintent.putExtra("duplicate", false);// 快捷方式名称shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME,getString(R.string.shortcutname));// 快捷方式图片Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.shortcut);shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);// 点击快捷图片,运行的程序主入口shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), Activity01.class));// 发送广播。OKsendBroadcast(shortcutintent);return true;}
更多相关文章
- android TextView显示文字和图片
- 理解与应用Android桌面组件AppWidget
- Android(安卓)TextView中显示图片的4种方式
- 详解ImageView的CENTER_CROP,CENTER_INSIDE,FIT_CENTER等属性
- [置顶] Android(安卓)Gallery用法(自定义边框+底部小圆点)
- --android bitmap oom 分析
- Android(安卓)UI 之 我的页面 圆形图片+通用item封装(简化代码量)
- 一句话让Android(安卓)原生launcher2的桌面变的有弹性
- Android(安卓)DrawableTextView图片文字居中显示