首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在launcher主屏幕上放置应用程序图标?

如何在launcher主屏幕上放置应用程序图标?
EN

Stack Overflow用户
提问于 2011-02-01 01:47:25
回答 6查看 33K关注 0票数 18

如你所知,当应用程序正常安装时,会在launcher菜单屏幕上创建图标。我想做的是在安装过程中在用户主屏幕上创建图标。(不按住图标5秒钟。)

我从另一个来源听到这个,只是补充一下

代码语言:javascript
运行
复制
<category android:value="android.intent.category.HOME" />

转到AndroidManifest.xml文件,但它不起作用。

还有没有别的办法呢?

EN

回答 6

Stack Overflow用户

发布于 2011-09-18 21:24:20

您可以使用以下命令:

代码语言:javascript
运行
复制
Intent shortcutIntent = new Intent();
shortcutIntent.setClassName("packageName", "className");
//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, "shortcut_name");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));
//intent.putExtra("duplicate", false);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
            context.sendBroadcast(addIntent);

您必须在AndroidManaifest.xml中使用以下权限

代码语言:javascript
运行
复制
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

您可以根据需要使用注释过的代码。

请注意,上面的API可能没有文档记录。但它是有效的。

票数 17
EN

Stack Overflow用户

发布于 2014-07-09 14:55:41

现在,Google Play服务解决了这一问题。你不再需要添加任何代码来完成这项工作。现在,当你从Google Play商店安装一个应用程序时,它会自动在主屏幕上创建徽标。它可以在Google Play商店设置中处理。例外:如果您正在使用任何自定义roms或启动器,它不能与一些工作。

票数 9
EN

Stack Overflow用户

发布于 2012-09-26 18:30:54

我使用这些方法来正确地添加或删除快捷键。当用户手动添加/删除快捷方式时,这些方法工作得很好,并且与安卓系统的相同。

代码语言:javascript
运行
复制
public static void addShortcut(Context context)
{
    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");

    ApplicationInfo appInfo = context.getApplicationInfo();

    // Shortcut name
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, appInfo.name);
    shortcut.putExtra("duplicate", false); // Just create once

    // Setup activity shoud be shortcut object 
    ComponentName component = new ComponentName(appInfo.packageName, appInfo.className);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(component));

    // Set shortcut icon
    ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(context, appInfo.icon);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

    context.sendBroadcast(shortcut);
}

public static void deleteShortcut(Context context)
{
    Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");

    ApplicationInfo appInfo = context.getApplicationInfo();

    // Shortcut name
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, appInfo.name);

    ComponentName comp = new ComponentName(appInfo.packageName, appInfo.className);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));

    context.sendBroadcast(shortcut);
}

权限:

代码语言:javascript
运行
复制
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4854197

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档