Android在主屏幕上创建快捷方式

时间:2011-06-13 23:06:26

标签: android shortcut homescreen

我想做的是:

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

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

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

他们似乎是我的问题的答案,但有人告诉我,这个代码不会在所有设备上工作,这是不推荐的,我想做的事情是不可能.......

  

不建议使用此技术。这是一个内部实现,不是Android SDK的一部分。它不适用于所有主屏幕实现。它可能不适用于所有以前版本的Android。它可能无法在Android的未来版本中使用,因为Google没有义务维护内部未记录的界面。请不要使用此

内部实施意味着什么?该代码是否可信赖.....请帮助我.......

10 个答案:

答案 0 :(得分:83)

示例代码使用未记录的接口(权限和意图)来安装快捷方式。正如“有人”告诉你的那样,这可能无法在所有手机上运行,​​并且可能会在未来的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);

答案 1 :(得分:66)

我开发了一种方法,用于在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);
}

不要忘记更改您的活动名称,图标资源&amp;权限。

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

快乐的编码!!!

修改

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

addIntent.putExtra("duplicate", false);

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

答案 2 :(得分:9)

自从android oreo以来,com.android.launcher.action.INSTALL_SHORTCUT广播不再有任何影响。 LINK

如果你想支持所有Android版本,特别是 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"/>

答案 3 :(得分: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();
}

答案 4 :(得分:8)

从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. 如果删除了目标应用,则无法删除。只有创造它的那个。
  5. 无法根据当前应用的资源创建基于目标应用的资源的图标。你可以通过位图来实现。
  6. 对于pre-Android O,您也可以使用ShortcutManagerCompat创建新的快捷方式,而不受任何限制。

答案 5 :(得分:3)

SecondProduct以来,不推荐使用API level 26。创建快捷方式的新方法是使用ShortcutManager

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

答案 6 :(得分:2)

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

答案 8 :(得分:1)

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

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 :(得分:0)

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个图标。我不明白??