接收其他应用程序发出的创建快捷方式的广播

public class InstallShortcutReceiver extends BroadcastReceiver {

	private static final String ACTION_INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		if (action.equals(ACTION_INSTALL_SHORTCUT)) {
			String name = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
			Intent extraIntent = intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
			if (name == null) {
				try {
					PackageManager pm = context.getPackageManager();
					ActivityInfo info = pm.getActivityInfo(intent.getComponent(), 0);
					name = info.loadLabel(pm).toString();
				} catch (PackageManager.NameNotFoundException nnfe) {
					return;
				}
			}

			ComponentName component = null;
			Bitmap bitmap = null;
			ShortcutIconResource iconResource = null;
			Parcelable bitmapParcelable = intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
			if (bitmapParcelable != null && bitmapParcelable instanceof Bitmap) {
				bitmap = (Bitmap) bitmapParcelable;
			} else {
				Parcelable extra = intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
				if (extra != null && extra instanceof ShortcutIconResource) {
					try {
						iconResource = (ShortcutIconResource) extra;
						PackageManager packageManager = context.getPackageManager();
						Resources resources = packageManager.getResourcesForApplication(iconResource.packageName);
						int id = resources.getIdentifier(iconResource.resourceName, null, null);
						Drawable drawable = resources.getDrawable(id);
						bitmap = Util.drawableToBitmap(drawable);
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
			}

			if (extraIntent == null) {
				return;
			} else {
				if (extraIntent.getAction() == null) {
					extraIntent.setAction(Intent.ACTION_VIEW);
				} else if (extraIntent.getAction().equals(Intent.ACTION_MAIN) && extraIntent.getCategories() != null
						&& extraIntent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
					extraIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
				}
				component = extraIntent.getComponent();
				if (component == null) {

					if (iconResource != null) {
						component = new ComponentName(iconResource.packageName, iconResource.resourceName);
					} else {
						component = new ComponentName("com.joy.other", "com.joy.otherActivity");
					}

				}
				TrinityButtonInfo buttonInfo = new TrinityButtonInfo(new ButtonMetaData());

				buttonInfo.mComponentName = component;
				buttonInfo.mTitle = name;
				buttonInfo.mBitmap = BitmapHelper.createIconBitmap(bitmap, LauncherApplication.mContext, null, true);
				buttonInfo.mIntent = extraIntent;
				buttonInfo.mShortcutTitle = name;
				buttonInfo.mShortcutBitmap = bitmap;
				addButtonsToCurrentHomePage(buttonInfo);
			}

		}
	}
}


转载于:https://my.oschina.net/u/1429620/blog/265219

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值