如何正确安装 APK 文件,以便启动器在主屏幕上创建一个新的应用程序图标?

How to properly install an APK file, so that the launcher will create a new app icon of it on the home screen?

背景

我有一个业余时间制作的应用程序 (here),它的主要功能之一是安装 APK 文件。

问题

安装应用的用户希望启动器上出现新的应用图标。

这可能发生在 Play 商店,但出于某种原因,启动器会忽略其他类型的安装。

Play 商店安装应用的方式与第三方应用的安装方式有所不同。

我想知道如何正确执行此操作,例如在 Play 商店中,以便安装 APK 文件时也会创建一个应用程序图标。

我试过的

我发现安装 APK 的唯一方法是:

@Nullable
public static Intent prepareAppInstallationIntent(Context context, File file, final boolean requestResult) {
    Intent intent = null;
    try {
        intent = new Intent(Intent.ACTION_INSTALL_PACKAGE)//
                .setDataAndType(
                        VERSION.SDK_INT >= VERSION_CODES.N ?
                                android.support.v4.content.FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file)
                                : Uri.fromFile(file),
                        "application/vnd.android.package-archive")
                .putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true)
                .putExtra(Intent.EXTRA_RETURN_RESULT, requestResult)
                .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        if (VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN)
            intent.putExtra(Intent.EXTRA_ALLOW_REPLACE, true);
    } catch (Throwable e) {
    }
    return intent;
}

清单

<provider
  android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true">
  <meta-data
    android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/>
</provider>

xml/provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
  <!--<external-path name="external_files" path="."/>-->
  <external-path
    name="files_root" path="Android/data/${applicationId}"/>
  <external-path
    name="external_storage_root" path="."/>
</paths>

但这不会在启动器上创建应用程序图标。

在问题跟踪器 (here) 上写下这个,我得到了我应该做什么的线索。我被告知:

Icons are added to the home screen if the app is installed via the new PackageInstaller APIs (and proper install reason is provided). Installing an app for command like does not support this feature.

问题

  1. 是否有 API 第三方应用程序可以正确安装应用程序,在启动器上有一个新图标?如果是,具体是怎样的?

  2. 有没有办法甚至使用 root 来做到这一点?并通过 PC 使用 adb 命令?

这是使用 Intent 完成的方法(需要知道应用程序的包名称):

fun prepareAppInstallationIntent(context: Context, file: File, requestResult: Boolean, packageName: String? = null): Intent? {
    var intent: Intent? = null
    try {
        intent = Intent(Intent.ACTION_INSTALL_PACKAGE) //
            .setDataAndType(
                if (VERSION.SDK_INT >= VERSION_CODES.N)
                    androidx.core.content.FileProvider.getUriForFile(
                        context,
                        context.packageName,
                        file
                    )
                else
                    Uri.fromFile(file),
                "application/vnd.android.package-archive"
            )
            .putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true)
            .putExtra(Intent.EXTRA_RETURN_RESULT, requestResult)
            .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        if (packageName != null && VERSION.SDK_INT >= VERSION_CODES.N) {
            intent.putExtra(Intent.EXTRA_PACKAGE_NAME, packageName)
        }
        intent.putExtra(Intent.EXTRA_ALLOW_REPLACE, true)
    } catch (e: Throwable) {
    }
    return intent
}