如何获取安装意图的结果? [英] How to get the result of installation intent?

查看:35
本文介绍了如何获取安装意图的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我从服务器下载了一个 apk 并将其保存在设备上.此 apk 用于更新应用程序.如果下载完成,系统会使用以下代码提示用户这样做:

In my application I download an apk from a server and save it on the device. This apk is used to update the app. If the download is finished the user is prompted to do so by using the following code:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    Uri apkUri = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID + ".provider", updateAPK);
    Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
    intent.setData(apkUri);
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(intent);

} else {
    Uri apkUri = Uri.fromFile(updateAPK);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

这是正常工作,但我的问题是:是否有可能获得意图的结果,以便我可以检查用户是否取消了安装?

This is working like it should, but my question is: Is ist possible to get the result of the intent, so I can check whether the user cancelled the installation or not?

推荐答案

将您的方案分成三个方案,而不是两个.

Divide your scenario into three scenarios, not two.

在 API 级别 14 之前的设备上,使用带有 file UriACTION_VIEW.请注意,您不需要 FLAG_ACTIVITY_NEW_TASK —或者,更准确地说,您应该在所有三种情况下都使用或不使用它.

On devices older than API Level 14, use ACTION_VIEW with a file Uri. Note that you do not need FLAG_ACTIVITY_NEW_TASK — or, more accurately, you should be consistent in either using it or not using it across all three scenarios.

在 API 级别 14-23 的设备上,使用 ACTION_INSTALL_PACKAGEfile Uri.EXTRA_RETURN_RESULT 设置为 true,并使用 startActivityForResult().

On devices that are API Level 14-23, use ACTION_INSTALL_PACKAGE with a file Uri. Set EXTRA_RETURN_RESULT to true, and use startActivityForResult().

在 API 级别 24+ 的设备上,使用带有 content UriACTION_INSTALL_PACKAGE,就像您正在做的那样.EXTRA_RETURN_RESULT 设置为 true,并使用 startActivityForResult().

On devices that are API Level 24+, use ACTION_INSTALL_PACKAGE with a content Uri, as you are doing. Set EXTRA_RETURN_RESULT to true, and use startActivityForResult().

在后两种情况下,onActivityResult() 将报告用户是否安装了应用程序 (RESULT_OK).

In those latter two scenarios, onActivityResult() will report whether the user installed the app (RESULT_OK) or not.

这篇关于如何获取安装意图的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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