shortcut 快捷方式创建及删除

AndroidShortCutUtils.java

/**
	 * 添加到Shortcut选项中(默认桌面上长按调出)
	 * @param activity
	 * @param pakageName
	 * @param className
	 * @param shortcutName
	 * @param icon
	 * @param duplicate
	 * 
	 * 同时需要在manifest中为activity提供一个包含
	 * action="android.intent.action.CREATE_SHORTCUT"的intent-filter
	 */
	public static void addShortcutToOptions(Activity activity, String pakageName, String className, String shortcutName, Drawable icon, boolean duplicate){
		Intent shortcut = new Intent();
		String label = shortcutName;
		BitmapDrawable iconBitmapDrawabel = (BitmapDrawable)icon;
		PackageManager packageManager = activity.getPackageManager();
		try {
			ApplicationInfo appInfo = packageManager.getApplicationInfo(pakageName, PackageManager.GET_META_DATA|PackageManager.GET_UNINSTALLED_PACKAGES);
			if(label==null){
				label = packageManager.getApplicationLabel(appInfo).toString();
			}
			if(iconBitmapDrawabel==null){
				iconBitmapDrawabel = (BitmapDrawable) packageManager.getApplicationIcon(appInfo);
			}
		} catch (NameNotFoundException e) {
			e.printStackTrace();
			Toast.makeText(activity, e.toString(), Toast.LENGTH_SHORT);
			return;
		}
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON, iconBitmapDrawabel.getBitmap());
		ComponentName comp = new ComponentName(pakageName, className);
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
		activity.setResult(Activity.RESULT_OK, shortcut);
	}

	static final String ACTION_INSTALL = "com.android.launcher.action.INSTALL_SHORTCUT";
	static final String ACTION_UNINSTALL = "com.android.launcher.action.UNINSTALL_SHORTCUT";
	
	/**
	 * 添加快捷方式到桌面
	 * @param context
	 * @param pakageName
	 * @param className
	 * @param shortcutName 可手动指定快捷方式的名称,删除时也要一致。null则使用默认名称
	 * @param icon 手动指定快捷方式的图标,null则使用默认图标
	 * @param duplicate
	 * 
	 * 同时需要在manifest中设置以下权限:
	 * <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
	 */
	public static void addShortcutToDesktop(Context context, String pakageName, String className, String shortcutName, Drawable icon, boolean duplicate){
		Intent shortcut = new Intent(ACTION_INSTALL);
		String label = shortcutName;
		BitmapDrawable iconBitmapDrawabel = (BitmapDrawable)icon;
		PackageManager packageManager = context.getPackageManager();
		try {
			ApplicationInfo appInfo = packageManager.getApplicationInfo(pakageName, PackageManager.GET_META_DATA|PackageManager.GET_ACTIVITIES);
			if(label==null){
				label = packageManager.getApplicationLabel(appInfo).toString();
			}
			if(iconBitmapDrawabel==null){
				iconBitmapDrawabel = (BitmapDrawable) packageManager.getApplicationIcon(appInfo);
			}
		} catch (NameNotFoundException e) {
			e.printStackTrace();
			Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT);
			return;
		}
		
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON, iconBitmapDrawabel.getBitmap());
		shortcut.putExtra("duplicate", duplicate); 
		ComponentName comp = new ComponentName(pakageName, className);
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setFlags(Intent.FLAG_FROM_BACKGROUND).setComponent(comp));
		context.sendBroadcast(shortcut);
	}
	
	/**
	 * 删除桌面快捷方式
	 * @param context
	 * @param pakageName
	 * @param className
	 * @param shortcutName 如果当初制定的快捷方式名称并非应用名,请手动指定,否则无法删除。null则使用默认名称
	 * 
	 * 同时需要在manifest中设置以下权限:
	 * <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
	 */
	public static void delShortcutFromDesktop(Context context, String pakageName, String className, String shortcutName){
		Intent shortcut = new Intent(ACTION_UNINSTALL);
		String label = shortcutName;
		PackageManager packageManager = context.getPackageManager();
		try {
			ApplicationInfo appInfo = packageManager.getApplicationInfo(pakageName, PackageManager.GET_META_DATA|PackageManager.GET_UNINSTALLED_PACKAGES);
			if(label==null){
				label = packageManager.getApplicationLabel(appInfo).toString();
			}
		} catch (NameNotFoundException e) {
			e.printStackTrace();
			Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT);
			return;
		}
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);
		ComponentName comp = new ComponentName(pakageName, className);
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
		context.sendBroadcast(shortcut);
	}

 PS:关于删除

网上有人说shortcut的删除需要root。其实是不用的。

只是在删除的时候有点和创建不同的地方,看代码:

 

AndroidShortCutUtils.addShortcutToDesktop(this, this.getPackageName(), ".MainActivity", null, null, false);
//删除的时候className需要包括有package的信息。
AndroidShortCutUtils.delShortcutFromDesktop(this,  this.getPackageName(), "com.knowhow.android.client.MainActivity", null);
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值