在Android 4.2.2上,Intent INSTALL_快捷方式在第二页创建快捷方式

在Android 4.2.2上,Intent INSTALL_快捷方式在第二页创建快捷方式,android,android-intent,shortcut,android-4.2-jelly-bean,homescreen,Android,Android Intent,Shortcut,Android 4.2 Jelly Bean,Homescreen,当调用此代码在主屏幕中创建快捷方式时,我得到了一个奇怪的结果 快捷方式在主屏幕的第二页上创建(第一页是空的,因此有足够的空间!)。有什么想法吗 public static void installShortcut(Context context, String packageName, String componentName, String shortcutName, Parcelable icon) { Intent shortcut = new Intent("com.androi

当调用此代码在主屏幕中创建快捷方式时,我得到了一个奇怪的结果

快捷方式在主屏幕的第二页上创建(第一页是空的,因此有足够的空间!)。有什么想法吗

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) {
    }
}
这是默认的Android 4.2.2主屏幕:

更新:
在Android 4.0.4上,快捷方式是在正确的位置创建的。

有数百种主屏幕实现,包括预安装的和可通过Play Store安装的。欢迎每一位参加:

  • 完全忽略您的
    意图
    ,只需不为其设置
    ,或者
  • 把捷径放在它想走的地方
在Android 4.0.4上,快捷方式是在正确的位置创建的


不,在这两种情况下,它都放在“正确的位置”,因为决定“正确的位置”的是主屏幕的作者,而不是你。

这第二页是默认页面?我点击“主页按钮”,然后滑动以聚焦下一页,这是第二页(在主页的右侧)所以把你的默认页面改为第一页而不是第二页我有点困惑,我该怎么办?是的,你用的是哪个启动器?好的,现在清楚了。因此,如果我不知道如何设置安装在低成本平板电脑上的主屏幕,我将无能为力。