Android:下载后如何打开apk文件进行自动更新?
我有一个应用程序,我想添加自动更新功能(它不在市场上)。我已经准备好了所有代码,可以检查是否有可用的更新,然后我调用如下代码:Android:下载后如何打开apk文件进行自动更新?,android,Android,我有一个应用程序,我想添加自动更新功能(它不在市场上)。我已经准备好了所有代码,可以检查是否有可用的更新,然后我调用如下代码: Intent intent = new Intent(Intent.ACTION_VIEW ,Uri.parse(Configuration.APK_URL)); c.startActivity(intent); 开始下载文件。有没有一种方法可以让我通过编程告诉它“打开”文件以开始安装过程,而用户不必去下载并单击它?这将启动安装过程 File apkFile =
Intent intent = new Intent(Intent.ACTION_VIEW ,Uri.parse(Configuration.APK_URL));
c.startActivity(intent);
开始下载文件。有没有一种方法可以让我通过编程告诉它“打开”文件以开始安装过程,而用户不必去下载并单击它?这将启动安装过程
File apkFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/packageName.apk");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
startActivity(intent);
以上答案适用于API-24之前的版本 如果您的应用程序以API 24或更高版本为目标(而且应该如此),则需要使用其他应用程序(否则您将获得FileUriExposedException,如上所述): 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>
其中,你的包名是你的应用程序包名
舱单:
<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>
如果您的应用程序针对API 24或更高版本,这将不再有效。见下面我的答案
<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>