我有一个应用程序,我想添加自动更新功能(它不在市场上).我有所有代码可以检查是否有可用的更新,然后我调用这样的代码:
Intent intent = new Intent(Intent.ACTION_VIEW ,Uri.parse(Configuration.APK_URL));
c.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
开始下载文件.有没有一种方法可以通过编程方式告诉它"打开"文件以开始安装过程,而无需用户进入下载并单击它?
以上答案适用于API-24之前的版本.
如果您的应用程序针对API 24以上(应该),你需要使用别的东西(否则你FileUriExposedException,如所描述这里):
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);
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
其中YOUR_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>
Run Code Online (Sandbox Code Playgroud)
这将开始安装过程
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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7670 次 |
| 最近记录: |