【问题标题】:How to enable automatic shortcut creation after app downloads and installs from Android Market?从 Android Market 下载和安装应用程序后,如何启用自动创建快捷方式?
【发布时间】:2015-03-31 16:14:15
【问题描述】:

我注意到 Android Market 上的一些应用在下载并安装到您的设备后会自动在桌面上创建快捷方式,而有些则不会。我将如何实施这种行为?

【问题讨论】:

  • 你说的是非Honeycomb设备吗?对于“桌面”,您是指启动器吗?我以前从未见过这种情况,我希望我永远也不会,因为这听起来像是过度侵入的行为。如果我想在启动器中添加一些东西,我会自己把它放在那里。但一般来说,安装时无法执行任何操作,您的代码只有在运行后才会执行。您可以选择在应用程序列表中有一个或多个图标,或者没有。这是在清单中完成的。
  • 不,这是针对我正在开发的 Honeycomb 应用程序。我注意到对于 SoundHound 和 Google Body 等应用程序,在我收到安装成功的通知后,屏幕底部会弹出一个小模式通知我已创建快捷方式。我意识到这对用户来说可能并不理想,但我的客户正在打电话,所以我只是在寻找一个可行的实现。

标签: android google-play


【解决方案1】:

向启动器发送一个意图。使用 EXTRA_SHORTCUT_NAME 和 EXTRA_SHORTCUT_INTENT 广播 INSTALL_SHORTCUT 意图。额外的 EXTRA_SHORTCUT_DUPLICATE 可用于帮助管理重复的快捷方式。 details 可以在Launcher2 project 中找到AOSP repository

请注意这一点,有些用户可能不喜欢未经许可创建快捷方式。

这是一些伪代码:

Intent installIntent = new Intent(InstallShortcutReceiver.ACTION_CREATE_SHORTCUT);
Intent myAppIntent = new Intent(Context.getPackageContext(), MyActivity.class);
installIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, Context.getString(R.string.app_name));
installIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myAppIntent);
installIntent.putExtra(Intent.SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));
Context.sendBroadcast(installIntent);

Intent 类以及备用 Intent action 中有更多信息。

【讨论】:

  • 你能给出更具体的例子来说明如何实现这个吗?我对 Android 开发很陌生,任何指导都会很棒。
  • 所有指向 android.git.kernel.org 的链接都已失效。
  • Launcher2 的答案是历史性的。 Launcher3是当前版本,链接已经更新到当前网站。
【解决方案2】:

事实证明,当您下载应用程序时,Android Market 会自动安装桌面快捷方式。我注意到带有 Honeycomb (3.0+) 目标应用程序的 Android 3.1 平板电脑上的这种默认行为。这似乎不是在以前的 Android 版本上运行的应用程序的标准约定,在第一次下载/启动应用程序时需要明确的用户输入/许可。

【讨论】:

  • 是的,我也经历过同样的事情,从 android 市场下载的应用程序,在 android 3.1 平板电脑上会在主屏幕上创建快捷方式,但在 android 2.3 上它不会在主屏幕上创建任何快捷方式,我在 android 2.3 手机上试过...但它是否在“Android 网站”或任何博客的任何地方都提到,默认情况下,应用快捷方式将在 android 3.0 + 更高版本平板电脑的主屏幕上创建
  • 根据我使用 Android 2.3 的经验,然后升级到 3.0、3.1 和 4.0.3,只有少数应用程序会自动在我的屏幕上占据一席之地(我不喜欢这样称呼它)桌面或启动器)。
【解决方案3】:

我已经测试了this demo。并将 addShortcut() 放在 onCreate() 中。然后从 IntelliJ 部署,我的 GEL(Google 体验启动器)会为第一次启动创建快捷方式。我担心它会继续创建快捷方式,但如果下次启动应用程序,它似乎不会创建另一个快捷方式。如果需要,请自行尝试。

private void addShortcut() {
    //Adding shortcut for MainActivity
    //on Home screen
    Intent shortcutIntent = new Intent(getApplicationContext(),
            MainActivity.class);

    shortcutIntent.setAction(Intent.ACTION_MAIN);

    Intent addIntent = new Intent();
    addIntent
            .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                    R.drawable.ic_launcher));

    addIntent
            .setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    getApplicationContext().sendBroadcast(addIntent);
}

【讨论】:

  • 这就像魅力一样,但在启动时添加了另一个图标......任何方式只做一次?在安装时而不是在onCreate()?
  • 您可以在安装后首次启动时使用首选项。检查它是否为空,然后它是第一次启动,然后转到 addShortcut()。记得在之后给它一个值以逃避 if-else。
  • 使用ACTION_PACKAGE_FIRST_LAUNCH 意图过滤器。
【解决方案4】:

添加快捷方式

private void addShortcut() {

    //Adding shortcut for MainActivity 
    //on Home screen
    Intent shortcutIntent = new Intent(getApplicationContext(),
            MainActivity.class);

    shortcutIntent.setAction(Intent.ACTION_MAIN);

    Intent addIntent = new Intent();
    addIntent
            .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ApplicationName");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                    R.drawable.ic_launcher));

    addIntent
            .setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    getApplicationContext().sendBroadcast(addIntent);
}

用于删除快捷方式

private void removeShortcut() {

    //Deleting shortcut for MainActivity 
    //on Home screen
    Intent shortcutIntent = new Intent(getApplicationContext(),
            MainActivity.class);
    shortcutIntent.setAction(Intent.ACTION_MAIN);

    Intent addIntent = new Intent();
    addIntent
            .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ApplicationName");

    addIntent
            .setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
    getApplicationContext().sendBroadcast(addIntent);
}

权限

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

【讨论】:

    猜你喜欢
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多