我是靠谱客的博主 漂亮鸭子,最近开发中收集的这篇文章主要介绍Android版本更新下载apk文件到sd卡并自动安装,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

版本更新,需要注意的是android7.0和8.0的安装问题

android8.0以上得需要安装未知来源应用的权限

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

android7.0

1.android7.0需要在清单文件注册


<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.jetair.cuair.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>

2.在res文件下新建xml文件夹,编写file_paths:

<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="downloadApk"
path="cuair" />
</paths>

下载安装apk的代码:


private void downLoadApk(final String downURL, final String appName){
final ProgressDialog pd = new ProgressDialog(this);
//必须一直下载完,不可取消
pd.setCancelable(false);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("正在下载安装包");
//
pd.setTitle("版本更新");
pd.show();
Thread t = new Thread(){
@Override
public void run() {
try {
File file = downloadFile(downURL, appName, pd);
sleep(1000);
// 结束掉进度条对话框
pd.dismiss();
if (file!=null){
installApk(file);
} else {
Toast.makeText(BrowserActivity.this, "更新包无法下载", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
pd.dismiss();
}
}
};
t.start();
}
private File downloadFile(String path,String appName ,ProgressDialog pd) throws Exception {
// 如果相等的话表示当前的sdcard挂载在手机上并且是可用的
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
// 获取到文件的大小
pd.setMax(conn.getContentLength());
InputStream is = conn.getInputStream();
String fileName = Environment.getExternalStorageDirectory()+ "/cuair/" + appName+".apk";
File file = new File(fileName);
// 目录不存在创建目录
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
FileOutputStream fos = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
byte[] buffer = new byte[1024];
int len;
int total = 0;
while ((len = bis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
total += len;
// 获取当前下载量
pd.setProgress(total);
}
fos.close();
bis.close();
is.close();
return file;
} else {
return null;
}
}
/**
* 安装apk
*/
private void installApk(File file) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Log.d("installApk", "版本大于 N ,开始使用 fileProvider 进行安装");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(this, "com.jetair.cuair.fileprovider", file);
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
Log.d("installApk", "正常进行安装");
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
}
startActivity(intent);
}

 

最后

以上就是漂亮鸭子为你收集整理的Android版本更新下载apk文件到sd卡并自动安装的全部内容,希望文章能够帮你解决Android版本更新下载apk文件到sd卡并自动安装所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(70)

评论列表共有 0 条评论

立即
投稿
返回
顶部