---
title: "创建和卸载快捷方式"
author: "Java-Object"
date: "2013-06-26T01:40:39Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2013-06-26T01:40:59Z"
id: 1894460
url: "https://hezd.iteye.com/blog/1894460"
---

转载:创建和卸载快捷方式

 

代码如下： 写道/\** * 创建快捷方式 \*/ private void createShortCut() { Intent shortcut = new Intent(com.android.launcher.action.INSTALL\_SHORTCUT); shortcut.putExtra(Intent.EXTRA\_SHORTCUT\_NAME, 创建快捷方式); shortcut.putExtra(duplicate, fals

 

代码如下：

 写道/\**  
\* 创建快捷方式  
\*/  
private void createShortCut() {

Intent shortcut = new Intent("com.android.launcher.action.INSTALL\_SHORTCUT");  
shortcut.putExtra(Intent.EXTRA\_SHORTCUT\_NAME, "创建快捷方式");  
shortcut.putExtra("duplicate", false); // 不允许重复  
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this,R.drawable.icon);// 设置快捷方式的图标  
shortcut.putExtra(Intent.EXTRA\_SHORTCUT\_ICON\_RESOURCE, iconRes);   
// 定义shortcut点击事件  
String action = "com.android.action.test";  
Intent respondIntent = new Intent(this, this.getClass());  
respondIntent.setAction(action);  
shortcut.putExtra(Intent.EXTRA\_SHORTCUT\_INTENT, respondIntent);

sendBroadcast(shortcut);

}

 需要权限：&lt;uses-permission android:name="com.android.launcher.permission.INSTALL\_SHORTCUT" /&gt; 

 卸载快捷方式：

 写道/\**  
\* 卸载快捷方式  
\*/  
void deleteShortcut() {  
Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL\_SHORTCUT");  
shortcut.putExtra(Intent.EXTRA\_SHORTCUT\_NAME, "卸载快捷方式"); //指定要卸载的快捷方式的名称  
String action = "com.android.action.test";  
String appClass = this.getPackageName() + "." + this.getLocalClassName();  
ComponentName comp = new ComponentName(this.getPackageName(), appClass);  
shortcut.putExtra(Intent.EXTRA\_SHORTCUT\_INTENT, new Intent(action).setComponent(comp));

sendBroadcast(shortcut);

} 需要权限：&lt;uses-permission android:name="com.android.launcher.permission.UNINSTALL\_SHORTCUT" /&gt;