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);
相关文章
|
5天前
|
Android开发
Android Launcher研究(二)-----------Launcher为何物,究竟是干什么
Android Launcher研究(二)-----------Launcher为何物,究竟是干什么
12 2
|
5天前
|
Android开发
Android 如何将定制的Launcher成为系统中唯一的Launcher
Android 如何将定制的Launcher成为系统中唯一的Launcher
19 2
|
5天前
|
JSON Android开发 数据格式
android与Web服务器交互时的cookie使用-兼谈大众点评数据获得(原创)
android与Web服务器交互时的cookie使用-兼谈大众点评数据获得(原创)
21 2
|
5天前
|
Android开发
Android 状态栏WiFi图标的显示逻辑
Android 状态栏WiFi图标的显示逻辑
31 0
|
5天前
|
定位技术 Android开发
Intent在Android中的几种用法
Intent在Android中的几种用法
12 1
|
5天前
|
XML JSON API
转Android上基于JSON的数据交互应用
转Android上基于JSON的数据交互应用
11 1
|
5天前
|
Linux 开发工具 Android开发
Android Launcher研究(一)-----------图文详解手把手教你在Windows环
Android Launcher研究(一)-----------图文详解手把手教你在Windows环
15 0
|
5天前
|
存储 Android开发
android launcher总体分析
android launcher总体分析
12 1
|
5天前
|
Android开发
Android launcher development resources
Android launcher development resources
11 1
|
5天前
|
Shell 开发工具 Android开发
如何单独 build android ap (以launcher为例)
如何单独 build android ap (以launcher为例)
10 1