如何正确安装 APK 文件,以便启动器在主屏幕上为其创建一个新的应用程序图标? [英] How to properly install an APK file, so that the launcher will create a new app icon of it on the home screen?

查看:22
本文介绍了如何正确安装 APK 文件,以便启动器在主屏幕上为其创建一个新的应用程序图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a spare time app which I've made (here), that one of its main features is to install APK files.

安装应用的用户希望启动器上会出现一个新的应用图标.

Users who install apps expects that a new app icon will appear on the launcher.

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

This can occur from the Play Store, but for some reason launchers ignore other types of installing.

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

Something is different in the way the Play Store installs apps, than how third party apps do it.

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

I'd like to know how to do it properly, like on the Play Store, so that installing an APK file will also create an app icon.

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

The only way I've found to install an APK is as such:

@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>

但这不会在启动器上创建应用图标.

But this doesn't create the app icon on the launcher.

在问题跟踪器(此处),我知道我应该做什么了.我被告知:

Writing about this on the issue tracker (here), I got a clue of what I should do. I was informed that :

如果应用程序是通过新版本安装的,图标会添加到主屏幕PackageInstaller API(并提供正确的安装原因).为 command like 安装应用程序不支持此功能.

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,在启动器上有一个新图标?如果是这样,具体如何?

  1. Is there an API for third party apps to install an app properly, having a new icon for it on the launcher? If so, how exactly?

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

Is there a way to do it even using root ? And using adb command via PC ?

推荐答案

以下是使用 Intent 的方法(需要知道应用程序的包名):

Here's how it's done using the Intent (need to know the package name of the app) :

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
}

这篇关于如何正确安装 APK 文件,以便启动器在主屏幕上为其创建一个新的应用程序图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆