61

我想做的是:

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

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

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

它们似乎是我问题的答案,但有人告诉我,这段代码不能在所有设备上运行,而且已被弃用,我想做的事情是不可能的......

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

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

4

10 回答 10

86

示例代码使用未记录的接口(权限和意图)来安装快捷方式。正如“某人”告诉你的那样,这可能不适用于所有手机,并且可能会在未来的 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);
于 2011-06-13T23:42:32.830 回答
68

我在下面开发了一种在 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);

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

于 2013-12-12T05:54:32.187 回答
20

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

如果你想支持所有的安卓版本,尤其是安卓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"/>
于 2018-05-15T05:14:32.617 回答
11

从 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 创建新的快捷方式,而没有任何这些限制。

于 2017-11-18T21:19:51.547 回答
8

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

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();
}
于 2014-05-27T15:13:10.150 回答
3

@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();

    }
于 2015-10-12T07:51:24.720 回答
3

由于API level 26, usingcom.android.launcher.action.INSTALL_SHORTCUT已被弃用。创建快捷方式的新方法是使用ShortcutManager.

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

于 2017-08-24T09:00:50.583 回答
2

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

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();
        }
    }

并且不需要额外的许可!!!

于 2020-04-18T16:32:28.557 回答
1
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);
于 2016-09-24T16:52:18.277 回答
1
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 个图标。我不明白??

于 2018-10-08T07:34:43.007 回答