Android添加桌面快捷方式和删除桌面快捷方式

<span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">下面的方式是用来判断是否包含相应标题名称的桌面快捷方式。</span>

public boolean hasShortCut(String title)
	{
		String url = "";
		url = "content://" + getAuthorityFromPermission(this, READ_SETTINGS)
				+ "/favorites?notify=true";

		// 获取当前应用名称
		// Log.i("url:", url);

		// try {
		// final PackageManager pm = getPackageManager();
		// title = pm.getApplicationLabel(
		// pm.getApplicationInfo(getPackageName(),
		// PackageManager.GET_META_DATA)).toString();
		// } catch (Exception e) {
		// }
		ContentResolver resolver = getContentResolver();
		Cursor cursor = resolver.query(Uri.parse(url), null, "title=?",
				new String[] { title }, null);
		if (cursor != null && cursor.getCount() > 0)
		{
			cursor.close();
			return true;
		}

		return false;
	}

添加删除桌面快捷方式

/**
	 * 添加快捷方式
	 * */
	public void creatShortCut(String shortcutName, int resourceId)
	{
		Intent intent = new Intent();
		intent.setClass(this, LaunchActivity.class);
		// intent.setClass(this, LaunchActivity.class);
		/* 以下两句是为了在卸载应用的时候同时删除桌面快捷方式 */
		intent.setAction("android.intent.action.MAIN");
		intent.addCategory("android.intent.category.LAUNCHER");

		Intent shortcutintent = new Intent(
				"com.android.launcher.action.INSTALL_SHORTCUT");

		// 不允许重复创建
		shortcutintent.putExtra("duplicate", false);
		// 需要显示的名称
		shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
		// 快捷图片
		Parcelable icon = Intent.ShortcutIconResource.fromContext(
				getApplicationContext(), resourceId);
		shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

		// ComponentName comp = new ComponentName(this.getPackageName(),
		// this.getPackageName() + "." + this.getLocalClassName());
		// shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(
		// Intent.ACTION_MAIN).setComponent(comp));
		// 点击快捷图片,运行的程序主入口
		shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
		// 发送广播。OK
		sendBroadcast(shortcutintent);
	}

	/**
	 * 删除程序的快捷方式
	 */
	private void delShortcut(String shortcutName)
	{
		Intent shortcut = new Intent(
				"com.android.launcher.action.UNINSTALL_SHORTCUT");
		// 快捷方式的名称
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
				getString(R.string.app_name));
		// 指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer
		// 注意: ComponentName的第二个参数必须是完整的类名(包名+类名),否则无法删除快捷方式
		String appClass = this.getPackageName() + "."
				+ this.getLocalClassName();
		ComponentName comp = new ComponentName(this.getPackageName(), appClass);
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(
				Intent.ACTION_MAIN).setComponent(comp));
		sendBroadcast(shortcut);
	}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值