【问题标题】:Android create shortcuts on the home screenAndroid在主屏幕上创建快捷方式
【发布时间】:2011-09-14 07:28:26
【问题描述】:

我想做的是:

1) 我在一个活动中,有 2 个按钮。如果我单击第一个,则会在我的主屏幕中创建一个快捷方式。该快捷方式打开了一个先前已下载的html 页面,因此我希望它使用默认浏览器,但我不想使用互联网,因为我已经拥有该页面。

2)第二个按钮创建另一个启动活动的快捷方式。我想向活动传递一些额外的参数(例如字符串)............

这些事情可能吗?我找到了一些链接和一些类似的问题,例如Android: Is there a programming way to create a web shortcut on home screen

它们似乎是我问题的答案,但有人告诉我,此代码不适用于所有设备,并且已被弃用,我想做的事情是不可能的......

不推荐这种技术。这是一个内部实现,不是 Android SDK 的一部分。它不适用于所有主屏幕实现。它可能不适用于所有过去的 Android 版本。它可能不适用于未来的 Android 版本,因为 Google 没有义务维护内部未记录的接口。请不要使用这个

什么是内部实现?该代码是否可信......请帮助我......

【问题讨论】:

  • 你应该接受几个答案。人们会更愿意帮助你。
  • 似乎不再允许:“应用程序及其广告不得修改或添加浏览器设置或书签、添加主屏幕快捷方式或用户设备上的图标作为向第三方提供的服务或用于广告目的." 但我不确定,因为最后一句话。 (来自新政策:play.google.com/intl/en/about/developer-content-policy.html

标签: android shortcut homescreen


【解决方案1】:

示例代码使用未记录的接口(权限和意图)来安装快捷方式。正如“某人”告诉你的那样,这可能不适用于所有手机,并且可能会在未来的 Android 版本中中断。不要这样做。

正确的方法是监听来自主屏幕的快捷请求——在清单中使用类似这样的意图过滤器:

<activity android:name=".ShortCutActivity" android:label="@string/shortcut_label">
  <intent-filter>
    <action android:name="android.intent.action.CREATE_SHORTCUT" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>

然后在接收到意图的活动中,为快捷方式创建一个意图并将其作为活动结果返回。

// create shortcut if requested
ShortcutIconResource icon =
    Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);

Intent intent = new Intent();

Intent launchIntent = new Intent(this,ActivityToLaunch.class);

intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, someNickname());
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

setResult(RESULT_OK, intent);

【讨论】:

  • 权限和意图没有记录?你的意思是,文档中有很多关于意图的内容,你也是你的意图,有什么区别? amyway,tnx 很多,只有一件事让我们说我的应用程序被称为“Myapplication”我必须在其清单中添加权限,并且活动的第二部分显示 2 个按钮来创建快捷方式....但是你的意思是 wirh “正确的方法是监听来自主屏幕的快捷方式请求——在你的清单中使用类似这样的意图过滤器:”主屏幕如何以及何时请求快捷方式?我的应用程序......
  • @user280560 -- 所有应用程序都将使用权限和意图;只是一些权限和意图没有记录——Android 将它们用于其内部实现(因为源代码是公开的,所以很容易找到这些),但你不应该在第三方应用程序中——因为它们是没有记录,他们可能会更改,恕不另行通知。
  • 我试过你的代码,它没有做我想要的,而不是我发布的链接中的那个......这就是你的代码所做的:如果我长按在主屏幕上,我选择快捷方式,我的 MARKET 显示在列表中,如果我选择它,则会引发一个意图(但在桌面上没有创建快捷方式)。我想要的是,当我在我的市场内并单击一个按钮时,会在主屏幕中添加一个带有图标的快捷方式..addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); this.sendBroadcast(addIntent);这 2 行让他们产生了差异,他们有什么问题和邪恶?
  • 没有什么是错的和邪恶的。他只是说这个界面可能会改变,然后你的应用程序将不再工作。
  • 您可以检查设备是否能够使用此 sn-p 处理快捷方式创建: if(getPackageManager().queryBroadcastReceivers(new Intent(INSTALL_SHORTCUT_ACTION), 0).size() > 0)
【解决方案2】:

我在下面开发了一种在 android 主屏幕上创建快捷方式图标的方法 [在我自己的应用上测试]。直接调用吧。

private void ShortcutIcon(){

    Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Test");
    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);
}

不要忘记更改您的活动名称、图标资源和权限。

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

编码愉快!!!

编辑:

对于重复问题,第一个选项是在代码中添加以下行,否则每次都会创建新问题。

addIntent.putExtra("duplicate", false);

第二个选项是先卸载应用快捷方式图标,如果第一个选项不起作用,则重新安装。

【讨论】:

  • 这个解决方案看起来比公认的更简单。它在我的情况下有效。
  • 添加addIntent.putExtra("duplicate", false); 否则每次都会创建一个新的。
  • @PratikButani 重复在 kitkat 中不起作用..你能帮我吗
  • 如果你删除缓存,你会得到一个重复的:)
  • 每次我打开应用程序时都会创建一个 toast 通知。这不好
【解决方案3】:

com.android.launcher.action.INSTALL_SHORTCUT 广播自 android oreo 以来不再有任何效果。 LINK

如果你想支持所有安卓版本,尤其是android 8.0 或 oreo 及更新版本,请使用以下代码创建快捷方式:

public static void addShortcutToHomeScreen(Context context)
{
    if (ShortcutManagerCompat.isRequestPinShortcutSupported(context))
    {
        ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, "#1")
                .setIntent(new Intent(context, YourActivity.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo
                .setShortLabel("Test")
                .setIcon(IconCompat.createWithResource(context, R.drawable.ic_launcher))
                .build();
        ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
    }
    else
    {
        // Shortcut is not supported by your launcher
    }
}

在清单文件中添加权限:

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

【讨论】:

  • @FunGapApp 您需要添加此权限才能在 android <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
  • 谢谢,它成功了(我的设备有 API 27)。是否可以添加快捷方式而无需确认“添加到主屏幕”窗口?虽然这可能是 Google 商店应用的默认行为,但我的应用是私有的。
【解决方案4】:

从Android O开始,这是创建快捷方式的方式:

if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
    final ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

    ShortcutInfo pinShortcutInfo = new ShortcutInfo.Builder(context, shortcutId)
            .setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher))
            .setShortLabel(label)
            .setIntent(new Intent(context, MainActivity.class).setAction(Intent.ACTION_MAIN))
            .build();
    shortcutManager.requestPinShortcut(pinShortcutInfo, null);
}

遗憾的是,它有很多限制:

  1. 要求用户接受添加。
  2. 无法在后台添加/删除。
  3. 如果目标应用程序被删除,则不会被删除。只有创造它的人。
  4. 无法基于目标应用程序的资源创建图标,除非它是当前应用程序的资源。不过,您可以通过位图进行操作。

对于 Android O 之前的版本,您也可以使用 ShortcutManagerCompat 创建新的快捷方式,而没有任何这些限制。

【讨论】:

  • 这些似乎不是严重的限制(除了第 3 个)。也许它现在可以工作了。
  • @MinasMina 是的,它工作正常。那时我做得不好,或者 API 还没有准备好等等……不过这些都是令人讨厌的限制。
  • 如果你更新了关于数字 3 的答案,那就太好了,这样它就会包含更新的信息:)
【解决方案5】:

我对上面的一个解决方案进行了一些改进。现在,它会在首选项中保存是否已添加快捷方式,并且如果用户删除了快捷方式,则不会将其添加到应用程序的新启动中。这也节省了一点时间,因为添加现有快捷方式的代码不再运行。

final static public String PREFS_NAME = "PREFS_NAME";
final static private String PREF_KEY_SHORTCUT_ADDED = "PREF_KEY_SHORTCUT_ADDED";


// Creates shortcut on Android widget screen
private void createShortcutIcon(){

    // Checking if ShortCut was already added
    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    boolean shortCutWasAlreadyAdded = sharedPreferences.getBoolean(PREF_KEY_SHORTCUT_ADDED, false);
    if (shortCutWasAlreadyAdded) return;

    Intent shortcutIntent = new Intent(getApplicationContext(), IntroActivity.class);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "YourAppName");
    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);

    // Remembering that ShortCut was already added
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean(PREF_KEY_SHORTCUT_ADDED, true);
    editor.commit();
}

【讨论】:

  • 会不会在打开应用前添加快捷方式。在不打开刚安装的应用程序的活动的情况下,此代码将如何运行。
  • 使用共享偏好似乎是完美的选择。其他尝试卸载重新安装使用“重复,错误”添加额外内容在大多数设备上都不起作用。
【解决方案6】:

@Siddiq Abu Bakkar 回答有效。但为了防止在每次应用启动时创建快捷方式,请使用共享首选项。

final String PREF_FIRST_START = "AppFirstLaunch";
 SharedPreferences settings = getSharedPreferences(PREF_FIRST_START, 0);
    if(settings.getBoolean("AppFirstLaunch", true)){

        // record the fact that the app has been started at least once
        settings.edit().putBoolean("AppFirstLaunch", false).commit();
        ShortcutIcon();

    }

【讨论】:

    【解决方案7】:

    由于API level 26,不推荐使用com.android.launcher.action.INSTALL_SHORTCUT。创建快捷方式的新方法是使用ShortcutManager

    它提到有 3 种快捷方式,静态、动态和固定。 根据您的要求,您可以选择创建它们。

    【讨论】:

    • Android O 还不是官方的(如果我错了请纠正我,“官方”是指官方稳定版本)。至此使用 Intents 就OK了
    • @CodigosTutoriales SDK 已经在稳定版本中可用,手机即将预装 Oreo 版本。始终使用可用的最新 API 级别来定位和编译您的应用程序是一种很好的做法。此答案适用于不想在其代码中看到弃用警告的人。
    【解决方案8】:

    要向主屏幕添加快捷方式,请使用此代码。

    public void addShortcutToHomeScreen(Context context) {
            if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
                ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, "#1")
                        .setIntent(new Intent(context, MainActivity.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo
                        .setShortLabel("Label Goes Here")
                        .setIcon(IconCompat.createWithResource(context, R.mipmap.ic_launcher_shortcut))
                        .build();
                ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
            } else {
                Toast.makeText(context, R.string.no_shortcut, Toast.LENGTH_SHORT).show();
            }
        }
    

    而且不需要额外的权限!!!

    【讨论】:

    • 需要权限
    【解决方案9】:
    final Intent shortcutIntent = new Intent(this, SomeActivity.class);
    
    final Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    // Sets the custom shortcut's title
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
    // Set the custom shortcut icon
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
    // add the shortcut
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    sendBroadcast(intent);
    

    【讨论】:

      【解决方案10】:
      public static void addShortcutToHomeScreen(Context context)
      {
          if (ShortcutManagerCompat.isRequestPinShortcutSupported(context))
          {
              ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, "#1")
                      .setIntent(new Intent(context, YourActivity.class).setAction(Intent.ACTION_MAIN)) // !!! intent's action must be set on oreo
                      .setShortLabel("Test")
                      .setIcon(IconCompat.createWithResource(context, R.drawable.ic_launcher))
                      .build();
              ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
          }
          else
          {
              // Shortcut is not supported by your launcher
          }
      }
      

      我用过,但有些设备在主屏幕上添加了 2 个图标。没看懂??

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-29
        • 2012-03-22
        • 1970-01-01
        • 1970-01-01
        • 2012-09-25
        • 1970-01-01
        • 2014-08-12
        相关资源
        最近更新 更多