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



Background

我有一个我制作的业余应用程序(在这里(,它的主要功能之一是安装 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>

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

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

如果通过新的安装应用程序,则图标将添加到主屏幕 包安装程序 API(并提供正确的安装原因(。 安装类似命令的应用程序不支持此功能。

问题

  1. 是否有第三方应用程序的 API 来正确安装应用程序,并在启动器上有一个新图标?如果是这样,究竟如何?

  2. 即使使用根,也有办法做到这一点吗?并通过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
}

最新更新