【问题标题】:On Android 4.2.2, Intent INSTALL_SHORTCUT creates shortcut in the second page在 Android 4.2.2 上,Intent INSTALL_SHORTCUT 在第二页创建快捷方式
【发布时间】:2014-03-24 15:13:33
【问题描述】:

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

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

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 上,快捷方式是在正确的位置创建的。

【问题讨论】:

  • 第二页是默认页吗?
  • 我点击“主页按钮”然后滑动聚焦下一页,这是第二页(在主页的右侧)
  • 所以将默认页面更改为第一页而不是第二页
  • 我有点迷茫,怎么办?
  • 是的,您使用的是哪个启动器?

标签: android android-intent shortcut android-4.2-jelly-bean homescreen


【解决方案1】:

有数百种主屏幕实现,包括预安装的和可通过 Play 商店安装的。欢迎每个人:

  • 完全忽略您的Intent,只是没有<intent-filter>,或者
  • 将快捷方式放在任何需要的地方

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

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

【讨论】:

  • 好的,现在很清楚了。因此,如果我不知道如何设置安装在我的低成本平板电脑中的主屏幕,我将无能为力。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-14
  • 1970-01-01
  • 2018-07-21
  • 2011-11-09
  • 2011-06-21
  • 2018-03-29
  • 1970-01-01
相关资源
最近更新 更多