android添加桌面快捷方式

封装类、直接放到项目中去就可以用

记在主配置文件添加权限

    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

 

package com.hhj.namespace;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;

public class Shortcut {
	private Context mContext;

	public Shortcut(Context mContext) {
		this.mContext = mContext;
	}

	/*第一个传的是在桌面上应用的名字,第二个是在桌面上的图标*/
	public void createShortcut(String shortcutName, int icon) {
		Intent intent = new Intent(
				"com.android.launcher.action.INSTALL_SHORTCUT");
		Parcelable img = Intent.ShortcutIconResource
				.fromContext(mContext, icon);
		intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
		intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, img);
		/*FdffActivity.class启动应用的类名 的启动类*/
		Intent sendToAct = new Intent(mContext, FdffActivity.class);
		sendToAct.setAction(Intent.ACTION_MAIN);
		sendToAct.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		// sendToAct.putExtra("id", id);
		intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, sendToAct);
		mContext.sendBroadcast(intent);
	}

	// 删除快捷方式:
	public void deleteShortcut(String scName) {
		final String ACTION_UNINSTALL_SHORTCUT = "com.android.launcher.action.UNINSTALL_SHORTCUT";
		Intent intent = new Intent(ACTION_UNINSTALL_SHORTCUT);

		// scName是快捷方式的名字。
		intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, scName);

		// 第一个参数使用application里定义的包,第二个参数使用Activity里定义的名字,
		// 写完整的类名:包+类名。
		ComponentName comp = new ComponentName("synmin.app.shortcut","synmin.app.shortcut.ExeShortcutAct");
		intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent().setComponent(comp).setAction(Intent.ACTION_MAIN));
		mContext.sendBroadcast(intent);
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值