ITeye博客

Android应用自动添加桌面图标

dimple · 2011-10-09 22:37 · android开发 · android · 移动开发 · Markdown

每个可以交互的应用,在项目清单文件中都有Launcher类,除了提示系统这个Activity是入口函数外,还会在应用列表中添加一个应用的快捷图 标。本文讲述Launcher通过自己注册的InstallShortCutReceiver和UnInstallShortCutReceiver实现 了快捷方式图标的生成与移除过程,分析外部apk实用Intent请求生成快捷方式和移除快捷方式图标的问题。

 

添加图标:

 Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
  intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
  // 是否可以有多个快捷方式的副本,参数如果是true就可以生成多个快捷方式,如果是false就不会重复添加
  intent.putExtra("duplicate", false);
  
  Intent intent2 = new Intent(Intent.ACTION_MAIN);
  intent2.addCategory(Intent.CATEGORY_LAUNCHER);
  // 删除的应用程序的ComponentName,即应用程序包名+activity的名字
  intent2.setComponent(new ComponentName(this.getPackageName(), this.getPackageName() + ".Main"));

  intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent2);
  intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this,
    R.drawable.icon));
  sendBroadcast(intent);

 

需要添加的权限:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

 

删除图标:

 

Intent intent = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT" );
  intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);
// 要删除的应用程序的ComponentName,即应用程序包名+activity的名字
  intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent()
  .setComponent(new ComponentName(info.activityInfo.packageName,
    info.activityInfo.name)).setAction("android.intent.action.MAIN"));
  sendBroadcast(intent);

 

添加删除的权限:

 

<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。