在Android 4.2.2,意图在INSTALL_SHORTCUT第二页创建快捷方式 [英] On Android 4.2.2, Intent INSTALL_SHORTCUT creates shortcut in the second page

查看:338
本文介绍了在Android 4.2.2,意图在INSTALL_SHORTCUT第二页创建快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调用这个code简单地创建主屏幕快捷方式,当我得到一个奇怪的结果。

I get a strange result when calling this code to simply create a shortcut in the home screen.

快捷键与第二页在主屏幕上创建(第一页是空的,所以有足够多的空间!)。任何想法?

The shortcut is created on the second page of the home screen (and the first page is empty so there is enought space!). Any ideas?

public static void installShortcut(Context context, String packageName, String componentName, String shortcutName, Parcelable icon) {
    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    ComponentName cn = new ComponentName(packageName, componentName);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(cn));
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
    shortcut.putExtra("duplicate", false);
    context.sendBroadcast(shortcut);
}

// gets some info from external package by name
public static void createShortcutForPackage(Context context, String packageName, String className) {
    Intent intent = new Intent();
    intent.setComponent(new ComponentName(packageName, className));

    PackageManager pm = context.getPackageManager();
    ResolveInfo ri = pm.resolveActivity(intent, 0);

    String shortcutName = ri.loadLabel(pm).toString();
    String activityName = ri.activityInfo.name;
    int iconId = ri.activityInfo.applicationInfo.icon;

    Context pkgContext;
    try {
        pkgContext = context.createPackageContext(packageName, Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
        if (pkgContext != null) {
            ShortcutIconResource sir = Intent.ShortcutIconResource.fromContext(pkgContext, iconId);
            installShortcut(pkgContext, packageName, activityName, shortcutName, sir);
        }
    } catch (NameNotFoundException e) {
    }
}

这是默认的Andr​​oid 4.2.2主屏幕:

This is the default Android 4.2.2 Home screen:

更新:
在Android 4.0.4快捷方式在正确的地方创建。

UPDATE: On Android 4.0.4 the shortcut is created in the right place.

推荐答案

有数百个主屏幕的实现,既pre-安装并通过那些Play商店安装。每个都欢迎之一:

There are hundreds of home screen implementations, both pre-installed and ones installable via the Play Store. Each is welcome to either:


  • 忽略你的意图完全,通过简单的不具有<意向滤光器> 它,或

  • 把快捷方式的地方就是了

  • ignore your Intent entirely, by simply not having an <intent-filter> for it, or
  • put the shortcut wherever it wants

在Android 4.0.4快捷方式在正确的地方创建。

On Android 4.0.4 the shortcut is created in the right place.

没有,这是摆在正确的地方,在这两种情况下,因为它是主屏幕的作者 - 不是你 - 谁决定什么是正确的地方,是

No, it is put in "the right place" in both cases, as it is the authors of the home screen -- not you -- who determines what "the right place" is.

这篇关于在Android 4.2.2,意图在INSTALL_SHORTCUT第二页创建快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆