android 下载apk 安装程序,Android实现APK下载安装
网上有很多种写法,也有很多坑,当然这些坑不是说代码有问题,而是Android的碎片化和各个厂商的定制造成的,例如最简单的写法DownloadManager在三星等手机上无法使用,原因是他们不支持DownloadManager。使用Thread或者IntentService或者AnsyTask其实也都可以,各有优劣,例如使用IntentService的更新UI问题,使用AnsyTask的排队问题,等等还是要看具体的需求选择最合适的实现方法。
另一个问题是Android版本的问题,主要是7.0的兼容问题,不然以前的写法是要奔溃的;
兼容7.0的第一步:
在Manfist清单文件中声明一个内容提供器,当然是在Application节点内部;
这里的applicationId占位其实是buildgradle文件中applicationId,也就是项目的包名,可以直接用项目包名代替。
而这个文件@xml/provider_paths是不存在的,是需要在res下新建的;
接下来给出一个provider_paths.xml的范例:
最后是android调用Apk安装:
public void installApkFile( String filePath) {
//Log.e("JACK",filePath);
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(mContext, "com.caihongto.caihongtoforcustomer.fileprovider", new File(filePath));
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(new File(filePath)), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
mContext.startActivity(intent);
}
有一点需要注意的是:
这里的命名必须和清单文件的命名一致;
下面是下载与安装代码:
public class DownloadAsyncTask extends AsyncTask {
private Context mContext;
int per = 0;
private ProgressDialog perDialog = null;
private String fullPath = null;
//private File apkFile;
public DownloadAsyncTask(Context context){
mContext=context;
}
@Override
protected Boolean doInBackground(String... params) {
fullPath = params[1] + params[2];
try {
URL url = new URL(params[0]);
HttpURLConnection huc = (HttpURLConnection) url
.openConnection();
huc.setConnectTimeout(10 * 1000);
huc.connect();
if (huc.getResponseCode() == 200) {
perDialog.setMax(huc.getContentLength());
File path = new File(params[1]);
if (!path.exists()) {
path.mkdirs();
}
File apkFile = new File(path, params[2]);
if (!apkFile.exists()) {
apkFile.createNewFile();
}
InputStream is = huc.getInputStream();
FileOutputStream fos = new FileOutputStream(apkFile);
byte[] buf = new byte[1024];
int readSize;
while (true) {
readSize = is.read(buf);
if (readSize <= 0) {
break;
}
per += readSize;
this.publishProgress(per);
fos.write(buf, 0, readSize);
}
fos.close();
is.close();
return true;
} else {
return false;
}
} catch (MalformedURLException e) {
return false;
} catch (IOException e) {
return false;
}
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
perDialog.dismiss();
if (result) {
Toast.makeText(mContext, "下载完成", Toast.LENGTH_SHORT)
.show();
installApkFile(fullPath);
} else {
Toast.makeText(mContext, "下载失败", Toast.LENGTH_SHORT)
.show();
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
perDialog = new ProgressDialog(mContext);
perDialog.setMessage("正在下载...");
perDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置水平进度条
perDialog.setCancelable(true);// 设置是否可以通过点击Back键取消
perDialog.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
perDialog.show();
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
perDialog.setProgress(values[0]);
}
public void installApkFile( String filePath) {
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(mContext,
"com.caihongto.caihongtoforcustomer.fileprovider",
new File(filePath));
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(new File(filePath)), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
mContext.startActivity(intent);
}
}
最后看下怎么调用的吧;
String[] params = new String[] { "http://www.caihongto.com/public/androidapk/6cc043563be7528213bbfa585245a5f9.apk",Environment.getExternalStorageDirectory() + "/pistol/app/", "caihongto_.apk" };
new DownloadAsyncTask(MainActivity.this).execute(params);
就这样吧,如果有什么同类的问题,大家可以留言
android 下载apk 安装程序,Android实现APK下载安装相关推荐
- PB安装包,PB安装程序,PB开发工具,powerbuilder安装包,powerbuilder安装程序,powerbuilder开发工具下载地址
B安装包,PB安装程序,PB开发工具,powerbuilder安装包,powerbuilder安装程序,powerbuilder开发工具下载地址 如果不能打开请复制网址粘贴到浏览器中 http://p ...
- Windows8.1安装tools提示:安装程序无法验证是否已安装所需的Microsoft更新KB2919355的问题详解
虚拟机下安装Windows8.1安装tools提示:安装程序无法验证是否已安装所需的Microsoft更新KB2919355.如果存在此更新,请继续安装. 因为Windows2012 R2缺少两个更新 ...
- Windows安装程序初始化时出错。安装程序将立即关闭。(原)
安装Microsoft .Net Framework 1.1时出错:Windows安装程序初始化时出错.安装程序将立即关闭.错误如下图所示: [img]http://msdn.iteye.com/up ...
- “Windows Server 2012 R2安装VMwareTools提示“安装程序无法验证是否已安装所需的Microsoft更新KB2919355”的解决办法
Windows Server 2012 R2安装VMwareTools提示"安装程序无法验证是否已安装所需的Microsoft更新KB2919355 网上查询资料说是Windows Serv ...
- Qt发布安装程序(打包依赖库+生成安装程序)
目录 打包Qt依赖库 打包其它依赖库 生成安装程序 下载并安装Inno Setup 5 使用Inno Setup 5 Qt编译好的项目是不能直接被双击运行的,如下所示. 缺少了依赖的库,需要添加进去. ...
- 无法通过windows installer服务安装此安装程序包。您必须安装带有更新版本windows Installer服务的Windows
无法通过windows installer服务安装此安装程序包.您必须安装带有更新版本windows installer服务的Windows 出现这个问题不让安装程序,可以到微软网站更新Windows ...
- Win11提示无法安装程序怎么办 Win11提示无法安装程序的解决方法
现在已经有不少小伙伴已经安装了Windows11系统,但有一些小伙伴在使用Win11系统安装程序的时候提示无法安装此程序包,那么碰到这种问题应该怎么办呢?下面就和小编一起来看看有什么方法吧. Win1 ...
- sql正在安装html帮助,SQL2000安装卡在了“安装程序正在启动服务器并安装您选择的配置”...
今天电脑上(winxpsp3)安装sql2000个人版,出现了问题.SQL2000安装卡在了"安装程序正在启动服务器并安装您选择的配置", 停止不动.系统未死,程序未死. 先是出现 ...
- 安装mysql卡在启动服务器_SQL2000安装卡在了“安装程序正在启动服务器并安装您选择的配置”...
今天电脑上(winxpsp3)安装sql2000个人版,出现了问题.SQL2000安装卡在了"安装程序正在启动服务器并安装您选择的配置", 停止不动.系统未死,程序未死. 先是出现 ...
- android 7 apk 安装程序,Android安装apk文件并适配Android 7.0详解
Android安装apk文件并适配Android 7.0详解 首先在AndroidManifest.xml文件,activity同级节点注册provider: android:name="a ...
最新文章
- 玩转车联网1---初识OBD和行车助手
- 如何将风险应用加入白名单_将微信服务器、API接口的IP列表加入宝塔防火墙IP白名单...
- iOS程序健壮性笔记
- SQuAD文本理解挑战赛十大模型解读
- 简单分析@Configuration
- 何亚明:Facebook的工具文化和多媒体QoE
- C语言逻辑运算符 - C语言零基础入门教程
- C语言—sort函数比较大小的快捷使用--algorithm头文件下
- mysql inet addr_InetAddress类的使用
- 寻找设计独特标识LOGO的灵感?可编辑模板帮你轻松解决!
- require include php5中最新区别,百度上好多错的。
- 一点感想——还没学会跑,怎么能够想着飞
- oracle中distinct的正确应用
- 155.最小栈(力扣leetcode) 博主可答疑该问题
- MMKV 原理以及使用
- 4K视频质量分析 白皮书
- 亿图脑图-MindMaster-思维导图及亿图图示会员-史上超值活动
- 英语计算机的读音,井号'#'用英语怎么说(计算机字符 - 英文读音)
- 我们总是喜欢拿顺其自然来敷衍人生道路上的荆棘坎坷,却很少承认,真正的顺其自然是竭尽所能之后的不强求,而非两手一摊的不作为。
- matlab中screen函数,13 PTB Screen 函数