代码之家  ›  专栏  ›  技术社区  ›  Daniel Llorente

安卓创建应用程序直接访问

  •  1
  • Daniel Llorente  · 技术社区  · 12 年前

    我观察到,在某些情况下,当你安装一个应用程序(例如shazam)时,它会在主屏幕中自动创建直接访问。

    我想将这种行为导出到我开发的应用程序中。

    有人知道怎么可能吗?

    2 回复  |  直到 12 年前
        1
  •  0
  •   Jonathon Reinhart    12 年前

    无法在安装时通过应用程序在主屏幕上创建快捷方式。 Play Store App中有可选设置,可在安装时自动创建快捷方式。

    或者,您可以使用以下代码在MainActivity的OnCreate方法中创建快捷方式:

    需要清单权限。

    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    

    使用以下代码以编程方式创建快捷方式。

    Intent shortcutIntent = new Intent();
    shortcutIntent.setClassName("<Your app package>", "Class Name");
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    

    创建快捷方式本身

    Intent addSCIntent = new Intent();
    addSCIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addSCIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "<Shortcut Name>");
    addSCIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher);
    addSCIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    context.sendBroadcast(addSCIntent);
    
        2
  •  0
  •   ottse    12 年前

    我猜你的意思是在主屏幕上添加快捷方式。这 tutorial 将帮助您。