31

我想在安装我的应用程序后立即在主屏幕上创建我的应用程序的快捷方式/启动器图标(甚至在我启动它之前)。那可能吗?我该怎么做?

4

6 回答 6

45

从 ICS 开始,您可以这样做:

public void createShortCut(){
    Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    shortcutintent.putExtra("duplicate", false);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));
    Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), EnterActivity.class));
    sendBroadcast(shortcutintent);
}

另请参阅启动器的源代码:此链接

编辑:如果有人会错过阅读评论,请添加以下行。

这需要"com.android.launcher.permission.INSTALL_SHORTCUT"许可

于 2013-08-20T05:03:16.853 回答
13

您忘记添加权限:

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

很棒的教程在这里: http ://androidforbegineers.blogspot.in/2014/01/android-home-screen-shortcut.html# !

于 2014-05-13T10:54:06.240 回答
12

注意:这个答案现在是错误的。例如,请参阅Robin 的回答,了解如何做到这一点。


据我所知,一个应用程序不能强制自己进入主屏幕。它被添加到启动器应用程序维护的应用程序列表中,但主屏幕通常在用户控制之下。让应用程序能够弄乱主屏幕将是一个公开的滥用邀请。

于 2013-08-20T04:55:31.163 回答
5

如果您安装您的应用程序,您的应用程序、任何服务或其他进程都不会处于活动状态!Android 希望用户在“打开”应用程序中迈出第一步。您不能直接安装快捷方式!这就是答案。注意:在大多数设备上,启动器将为您的应用程序本身创建一个快捷方式。

在用户以某种方式启动您的应用程序至少一次后,您可以在运行时安装快捷方式吗?:的(见罗宾斯的回答)。

有解决方法吗?也许,但没有一个好人。

更新,另一个提示:如果您在用户的设备上已经有一个应用程序。(例如,如果您安装的第二个应用程序是“going pro”之类的关键),那么您实际上可以从第一个应用程序启动第二个应用程序,而无需用户启动第二个应用程序(并添加快捷方式) .

于 2015-10-29T18:45:11.660 回答
4
  1. 创建函数来调用快捷方式的意图:

    private void criarAtalho() {
        Intent shortcutIntent = new Intent(getApplicationContext(), SplashScreen.class);
        shortcutIntent.setAction(Intent.ACTION_MAIN);
        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "nomeDaApp");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.mipmap.ic_launcher));
    
        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        addIntent.putExtra("duplicate", false);  //may it's already there so don't duplicate
        getApplicationContext().sendBroadcast(addIntent);
    }
    
  2. 在 OnCreat 上写下函数的调用:

    if(!getSharedPreferences("APP_PREFERENCE", Activity.MODE_PRIVATE).getBoolean("IS_ICON_CREATED", false)){
        criarAtalho();
        getSharedPreferences("APP_PREFERENCE", Activity.MODE_PRIVATE).edit().putBoolean("IS_ICON_CREATED", true).commit();
    }
    
  3. 设置清单的权限:

    <uses-permission
        android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    
于 2017-04-05T19:45:47.217 回答
1

首先,添加向清单添加快捷方式的权限:

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

然后,调用下面的函数。

public void createShortcut(){
    Intent intentShortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    intentShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
    Parcelable appicon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher);
    intentShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, appicon);
    intentShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), MainActivity.class));
    intentShortcut.putExtra("duplicate", false);
    sendBroadcast(intentShortcut);
}
于 2017-05-03T05:49:22.630 回答