Android应用自动添加桌面图标

 

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

 

添加图标:

Java代码  
  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就不会重复添加   
  4.  intent.putExtra("duplicate"false);   
  5.     
  6.  Intent intent2 = new Intent(Intent.ACTION_MAIN);   
  7.  intent2.addCategory(Intent.CATEGORY_LAUNCHER);   
  8.  // 删除的应用程序的ComponentName,即应用程序包名+activity的名字   
  9.  intent2.setComponent(new ComponentName(this.getPackageName(), this.getPackageName() + ".Main"));   
  10.   
  11.  intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent2);   
  12.  intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this,   
  13.    R.drawable.icon));   
  14.  sendBroadcast(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);

 

需要添加的权限:

Xml代码  
  1. <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />  

 

删除图标:

 

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

 

添加删除的权限:

 

Xml代码  
  1. <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>  
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值