【Android】实现类似于QQ将好友的头像用作快捷方式。

原因:

写这个的原因就是网上大都是告诉你如何用drawable里的资源做快捷方式,那么本地的图片能不能做快捷方式呢?肯定是可以的,所以我找了下安卓的源码,发现有这样的介绍
    /**
     * Activity Action: Creates a shortcut.
     * <p>Input: Nothing.</p>
     * <p>Output: An Intent representing the shortcut. The intent must contain three
     * extras: SHORTCUT_INTENT (value: Intent), SHORTCUT_NAME (value: String),
     * and SHORTCUT_ICON (value: Bitmap) or SHORTCUT_ICON_RESOURCE
     * (value: ShortcutIconResource).</p>
     *
     * @see #EXTRA_SHORTCUT_INTENT
     * @see #EXTRA_SHORTCUT_NAME
     * @see #EXTRA_SHORTCUT_ICON
     * @see #EXTRA_SHORTCUT_ICON_RESOURCE
     * @see android.content.Intent.ShortcutIconResource
     */

不难看出有两种方式创建快捷方式图标,不过大都用的第一种。所以我们就试试第二种吧。

解决方案:

既然都看了源码的介绍了,自然就知道怎么做了。
	public void CreateShotCast(Context context, Class<?> clazz) {
		Intent shortcut = new Intent(Intent.ACTION_CREATE_SHORTCUT);
		
		Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
		shortcutIntent.setClass(context, clazz);
		
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "测试");
		// Intent.EXTRA_SHORTCUT_ICON 是bitmap对象
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeFile("路径"));
		context.sendBroadcast(shortcut);
	}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值