Android:下载自动更新后如何打开apk文件? [英] Android: how to open an apk file after downloading for auto-update?

查看:432
本文介绍了Android:下载自动更新后如何打开apk文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个想要添加自动更新功能的应用程序(它不在市场中).我已经准备好了所有代码,以检查是否有可用的更新,然后调用如下代码:

I have an application that I'd like to add auto-update functionality (it's not in the marketplace). I have all the code in place that would check to see if there is an update available and then I call something like this:

Intent intent = new Intent(Intent.ACTION_VIEW ,Uri.parse(Configuration.APK_URL));
c.startActivity(intent);  

开始下载文件.有没有一种方法可以使我以编程方式告诉它打开"文件以开始安装过程,而无需用户去下载并单击它?

which starts downloading the file. Is there a way that I can programmatically tell it to "open" the file to begin the installation process without the user having to go to the downloads and clicking on it?

推荐答案

以上答案适用于API-24之前的版本.

The answer above was for pre-API-24.

如果您的应用程序针对API 24或更高版本(并且应该如此),则需要使用其他方法(否则,您将获得FileUriExposedException,如 此处 ):

If your app targets API 24 or more (and it should), you need to use something else (otherwise you get FileUriExposedException, as described here) :

    File apkFile = new File(...);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri fileUri = android.support.v4.content.FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", apkFile);
    intent.setDataAndType(fileUri, "application/vnd.android.package-archive");
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(intent);

provider_paths.xml:

provider_paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <!--<external-path name="external_files" path="."/>-->
    <external-path path="Android/data/YOUR_PACKAGE_NAME" name="files_root" />
    <external-path path="." name="external_storage_root" />
</paths>

其中YOUR_PACKAGE_NAME是您应用的程序包名称.

where YOUR_PACKAGE_NAME is your app's package name.

清单:

    <provider
        android:name="android.support.v4.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>

这篇关于Android:下载自动更新后如何打开apk文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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