android 下载apk 安装程序,Android实现APK下载安装

本文介绍了在Android中实现APK下载安装的各种方法及其遇到的兼容性问题,特别是在Android 7.0上的调整。通过使用DownloadAsyncTask进行后台下载,并在下载完成后调用installApkFile方法安装文件。涉及知识点包括ContentProvider、FileProvider、ProgressDialog和Intent的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网上有很多种写法,也有很多坑,当然这些坑不是说代码有问题,而是Android的碎片化和各个厂商的定制造成的,例如最简单的写法DownloadManager在三星等手机上无法使用,原因是他们不支持DownloadManager。使用Thread或者IntentService或者AnsyTask其实也都可以,各有优劣,例如使用IntentService的更新UI问题,使用AnsyTask的排队问题,等等还是要看具体的需求选择最合适的实现方法。

另一个问题是Android版本的问题,主要是7.0的兼容问题,不然以前的写法是要奔溃的;

兼容7.0的第一步:

在Manfist清单文件中声明一个内容提供器,当然是在Application节点内部;

这里的applicationId占位其实是buildgradle文件中applicationId,也就是项目的包名,可以直接用项目包名代替。

而这个文件@xml/provider_paths是不存在的,是需要在res下新建的;

0818b9ca8b590ca3270a3433284dd417.png

接下来给出一个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);

}

有一点需要注意的是:

0818b9ca8b590ca3270a3433284dd417.png

这里的命名必须和清单文件的命名一致;

下面是下载与安装代码:

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);

就这样吧,如果有什么同类的问题,大家可以留言

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值