Android桌面快捷方式图标生成与删除 使用Intent与launcher交互

简介: Android桌面快捷方式图标生成与删除 使用Intent与launcher交互

通过分析Launcher的生成快捷方式的过程,找出了使用Intent发送请求,Launcher通过自己注册的 InstallShortCutReceiver和UnInstallShortCutReceiver实现了快捷方式图标的生成与移除过程。本文主要分析外部apk如何使用Intent请求生成快捷方式和移除快捷方式图标的问题。

  生成快捷方式代码:

  Java代码

private static final String ACTION_INSTALL_SHORTCUT =
  "com.android.launcher.action.INSTALL_SHORTCUT";
  /**
  * 是否可以有多个快捷方式的副本
  */
  static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
  Intent shortcutIntent = new Intent(ACTION_INSTALL_SHORTCUT);
  shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
  getString(R.string.app_name));
  shortcutIntent.putExtra(EXTRA_SHORTCUT_DUPLICATE, false);
  Intent intent2 = new Intent(Intent.ACTION_MAIN);
  intent2.addCategory(Intent.CATEGORY_LAUNCHER);
  intent2.setComponent(new ComponentName(this.getPackageName(),
  ".Main"));
  shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent2);
  shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
  Intent.ShortcutIconResource.fromContext(this,
  R.drawable.icon));
  sendBroadcast(shortcutIntent);
  private static final String ACTION_INSTALL_SHORTCUT =
  "com.android.launcher.action.INSTALL_SHORTCUT";
  /**
  * 是否可以有多个快捷方式的副本
  */
  static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
  Intent shortcutIntent = new Intent(ACTION_INSTALL_SHORTCUT);
  shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
  getString(R.string.app_name));
  shortcutIntent.putExtra(EXTRA_SHORTCUT_DUPLICATE, false);
  Intent intent2 = new Intent(Intent.ACTION_MAIN);
  intent2.addCategory(Intent.CATEGORY_LAUNCHER);
  intent2.setComponent(new ComponentName(this.getPackageName(),
".Main"));
  shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent2);
  shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
  Intent.ShortcutIconResource.fromContext(this,
  R.drawable.icon));
  sendBroadcast(shortcutIntent);

  注:Intent intent2 = new Intent(Intent.ACTION_MAIN); 这个也可以换成的构造参数也可以是Intent.ACTION_CREATE_SHORTCUT,也可以生成快捷方式图标,但是这样不标准,在删除的时候如果不和这个对于相同则无法删除。所以还是用Intent.ACTION_MAIN。

  那么删除快捷方式的代码是:

  

Java代码
  private static final String ACTION_UNINSTALL_SHORTCUT =
  "com.android.launcher.action.UNINSTALL_SHORTCUT";
  Intent intent = new Intent(ACTION_UNINSTALL_SHORTCUT );
  intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);
  ComponentName comp = new ComponentName(info.activityInfo.packageName,
  info.activityInfo.name);
  intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent()
  .setComponent(comp).setAction("android.intent.action.MAIN"));
  sendBroadcast(intent);
相关文章
|
11月前
|
XML 编解码 Android开发
非常经典的Android开发问题-mipmap图标目录和drawable图标目录的区别和适用场景实战举例-优雅草卓伊凡
非常经典的Android开发问题-mipmap图标目录和drawable图标目录的区别和适用场景实战举例-优雅草卓伊凡
870 0
非常经典的Android开发问题-mipmap图标目录和drawable图标目录的区别和适用场景实战举例-优雅草卓伊凡
|
11月前
|
XML 存储 Java
【06】AI辅助编程完整的安卓二次商业实战-背景布局变更增加背景-二开发现页面跳转逻辑-替换剩余图标-优雅草卓伊凡
【06】AI辅助编程完整的安卓二次商业实战-背景布局变更增加背景-二开发现页面跳转逻辑-替换剩余图标-优雅草卓伊凡
250 3
【06】AI辅助编程完整的安卓二次商业实战-背景布局变更增加背景-二开发现页面跳转逻辑-替换剩余图标-优雅草卓伊凡
|
11月前
|
存储 消息中间件 人工智能
【04】AI辅助编程完整的安卓二次商业实战-寻找修改替换新UI首页图标-菜单图标-消息列表图标-优雅草伊凡
【04】AI辅助编程完整的安卓二次商业实战-寻找修改替换新UI首页图标-菜单图标-消息列表图标-优雅草伊凡
614 4
|
Android开发
Android控件样式的抽取(小提及快捷方式)
在Android开发中,若多个控件样式重复,可抽取公共部分以简化代码。例如对EditText提取样式,通过编辑`styles.xml`实现复用。为提高效率,Android Studio提供自动提取Style功能:右键点击控件样式选项,选择“Style...”,勾选需要提取的属性后确认,即可快速生成样式代码,显著提升开发便利性。
397 2
|
Android开发
解决android apk安装后出现2个相同的应用图标
解决android apk安装后出现2个相同的应用图标
1544 2
|
XML Android开发 数据格式
Android实战经验之Kotlin中快速实现动态更改应用图标和名称
本文介绍在Android中通过设置多个活动别名动态更改应用图标和名称的方法,涉及XML配置及Kotlin代码示例。
783 10
|
存储 Android开发
详细解读Android获取已安装应用信息(图标,名称,版本号,包)
详细解读Android获取已安装应用信息(图标,名称,版本号,包)
1092 0
|
Android开发
Android 状态栏WiFi图标的显示逻辑
Android 状态栏WiFi图标的显示逻辑
1092 0
|
Java Android开发
Android 中通过Intent传递类对象,通过实现Serializable和Parcelable接口两种方式传递对象
Android 中通过Intent传递类对象,通过实现Serializable和Parcelable接口两种方式传递对象
408 1

热门文章

最新文章