有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android api29如何在安装时自动在主屏幕上添加应用程序图标?

我希望我的应用程序在安装时自动在主屏幕上添加启动图标

我已经试过了:

在ActivityMain中添加快捷方式

private void addShortcut() {

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

    shortcutIntent.setAction(Intent.ACTION_MAIN);

    Intent addIntent = new Intent();
    addIntent
            .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                    R.drawable.ic_launcher_background));

    addIntent
            .setAction("com.安卓.launcher.action.INSTALL_SHORTCUT");
    addIntent.putExtra("duplicate", false);
    getApplicationContext().sendBroadcast(addIntent);
}

AndroidManifest。xml

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

不幸的是,它不起作用,我认为这种方法有点过时了? 谢谢!


共 (1) 个答案

  1. # 1 楼答案

    不可能对第一次安装的应用程序运行直接响应的代码——只有在用户打开应用程序之后。但是,如果用户从Google Play安装你的应用程序,默认情况下,它通常会将图标添加到主屏幕