安卓代码中创建快捷方式

<span style="white-space:pre">	</span>/**
	 * 判断是否已经添加快捷方式
	 * content://com.cyanogenmod.trebuchet.settings/favorites 不同的手机,launcher.db数据库放到地方不同
	 * @return
	 */
	private boolean shortcutInScreen() {
		Cursor cursor = getContentResolver()
				//content://com.android.launcher/favorites
				.query(Uri.parse("content://com.cyanogenmod.trebuchet.settings/favorites"),
						null,
						"intent like ?",
						new String[] { "%component=com.jike.superflashlight/.MainActivity%" },
						null);
		if (cursor.getCount() > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 添加桌面快捷方式按钮
	 * 
	 * @param view
	 */
	public void onClickAddShortcut(View view) {
		if (!shortcutInScreen()) {

			Intent installShortcut = new Intent(
					"com.android.launcher.action.INSTALL_SHORTCUT");
			installShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "应用的名称");
			Parcelable icon = Intent.ShortcutIconResource.fromContext(this,
					R.drawable.icon);
			// 启动主窗口
			Intent flashlightIntent = new Intent();
			flashlightIntent.setClassName("com.jike.superflashlight",
					"com.jike.superflashlight.MainActivity");
			flashlightIntent.setAction(Intent.ACTION_MAIN);
			flashlightIntent.addCategory(Intent.CATEGORY_LAUNCHER);
			// 快捷图标,添加flashlightIntent意图,打开主界面
			installShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
					flashlightIntent);
			// 发送广播
			sendBroadcast(installShortcut);
			Toast.makeText(this, "已成功将快捷方式添加到桌面", Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(this, "快捷方式已存在,无法继续添加!", Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * 移除快捷方式按钮
	 * 
	 * @param view
	 */
	public void onClickRemoveShortcut(View view) {
		if (shortcutInScreen()) {

			// 删除快捷图标意图
			Intent uninstallShortcut = new Intent(
					"com.android.launcher.action.UNINSTALL_SHORTCUT");
			uninstallShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "应用的名称");
			// 启动主界面意图
			Intent flashlightIntent = new Intent();
			flashlightIntent.setClassName("com.jike.superflashlight",
					"com.jike.superflashlight.MainActivity");

			uninstallShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
					flashlightIntent);

			flashlightIntent.setAction(Intent.ACTION_MAIN);
			flashlightIntent.addCategory(Intent.CATEGORY_LAUNCHER);

			sendBroadcast(uninstallShortcut);
		} else {
			Toast.makeText(this, "没有快捷方式,无法删除!", Toast.LENGTH_SHORT).show();
		}
	}</span>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值