`
dingran
  • 浏览: 371635 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

阅读更多

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

 

生成快捷方式代码:

 

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));  
       // 是否可以有多个快捷方式的副本,参数如果是true就可以生成多个快捷方式,如果是false就不会重复添加       
         shortcutIntent.putExtra(EXTRA_SHORTCUT_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"));  
          
        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。

 

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

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,即应用程序包名+activity的名字
 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);


最后要记得加上权限:
添加的权限:<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
删除的权限:<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>

 

 

 

10
1
分享到:
评论
6 楼 1846396994 2012-04-21  
总结的关于添加和删除及判断是否存在快捷方式,Android应用添加(创建)和删除及判断是否存在桌面快捷方式
5 楼 HHLgloden110 2011-10-20  
老兄,请教一个问题,
给应用程序创建快捷方式时,这个快捷方式的点击事件并不是打开给应用程序,而是执行应用程序里的一个服务,那快捷方式的事件要怎么写呢?直接通过intent不能实现跳转到服务,老是提示“应用程序尚未安装在你的手机上”
如何解决阿???指点下
4 楼 solo- 2011-10-09  
桌面的快捷方式只能到指定的activity吗?如果我想回到离开应用程序时的那个activity要怎么设置的??(长按应用程序那个图标生成的快捷方式就可以的)
3 楼 莫言_MoYan 2011-09-21  
请教怎么判断快捷方式已存在?具体的实现方法怎么写?
2 楼 dingran 2010-12-29  
1.取launcher.db查看数据,就能知道桌面上是否已经有这个应用的快捷方式,就算被换成别的图标也一样能确定。
2.屏蔽创建快捷方式的Toast只能从Launcher下手,那是写死的内容。如果从本应用里屏蔽,这个也许有办法,但是抱歉,我不清楚该怎么做。
1 楼 hshm 2010-12-13  
如何判断快捷方式已存在?
如何屏蔽系统的提示:创建快捷方式

相关推荐

Global site tag (gtag.js) - Google Analytics