打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

添加图标:

  1. Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
  2. intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
  3. // 是否可以有多个快捷方式的副本,参数如果是true就可以生成多个快捷方式,如果是false就不会重复添加 intent.putExtra("duplicate", false);
  4. Intent intent2 = new Intent(Intent.ACTION_MAIN);
  5. intent2.addCategory(Intent.CATEGORY_LAUNCHER);
  6. // 删除的应用程序的ComponentName,即应用程序包名+activity的名字 intent2.setComponent(new ComponentName(this.getPackageName(), this.getPackageName() + ".Main"));
  7. intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent2);
  8. intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this,
  9. R.drawable.icon));
  10. sendBroadcast(intent);
复制代码

需要添加的权限:
<uses-permission android:name=“com.android.launcher.permission.INSTALL_SHORTCUT” />


删除图标:

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


添加删除的权限:

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


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Android生成快捷方式
[Android]为指定的应用创建桌面快捷方式
Android中快捷方式的创建和删除(ShortCut)
android自动创建快捷方式,Android开发之生成桌面快捷方式细则(原创)
Android之在launcher里面动态加载桌面图标
Android之生成桌面快捷方式(二)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服