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

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

问题描述

在我的应用程序中,我从服务器下载了一个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之前的设备上,将ACTION_VIEWfile Uri一起使用.请注意,您不需要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,并使用.

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+的设备上,将ACTION_INSTALL_PACKAGEcontent Uri一起使用. EXTRA_RETURN_RESULT 设置为true,并使用.

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天全站免登陆