android 下载apk安装后自动启动,下载apk并启动安装

这篇博客介绍了如何在Android应用中实现从网络下载APK文件,并在下载完成后自动启动安装过程。首先获取写入和网络权限,然后使用DownloadManager创建下载任务,设置目标文件路径和监听下载状态。当下载完成时,通过BroadcastReceiver触发安装操作。注意,静默安装通常需要设备已root。

本篇实现现在网络上的apk并启动安装程序。

#### 权限

写入权限和网络访问权限

~~~

~~~

#### 变量

~~~

private DownloadManager downloadManager = null; //下载管理器

private long mTaskId; // 任务id

private String fileName; //下载下来文件保存时候的文件名称

fileName = System.currentTimeMillis() + ".apk";

downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

String url = "http://111.202.99.13/imtt.dd.qq.com/16891/2A76B7A9A8E841F0D8C1E74AD65FCB3F.apk?mkey=57c3dd3fc5355f8e&f=6c25&c=0&fsname=com.tencent.mobileqq_6.5.3_398.apk&csr=4d5s&p=.apk";

downloadAPK(url);

~~~

#### 下载apk

上面代码调用了downloadAPK方法。

其中用到了文件保存位置,也可以使用缓存目录替换。

~~~

// 使用系统下载器下载

private void downloadAPK(String versionUrl) {

// 创建下载任务

DownloadManager.Request request = new DownloadManager.Request(

Uri.parse(versionUrl));

request.setAllowedOverRoaming(false);// 漫游网络是否可以下载

// 设置文件类型,可以在下载结束后自动打开该文件

MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();

String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap

.getFileExtensionFromUrl(versionUrl));

request.setMimeType(mimeString);

// 在通知栏中显示,默认就是显示的

request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);

request.setVisibleInDownloadsUi(true);

// sdcard的目录下的download文件夹,必须设置

request.setDestinationInExternalPublicDir("/download/", fileName);

// request.setDestinationInExternalFilesDir(),也可以自己制定下载路径

// 将下载请求加入下载队列

downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

// 加入下载队列后会给该任务返回一个long型的id,

// 通过该id可以取消任务,重启任务等等

mTaskId = downloadManager.enqueue(request);

// 注册广播接收者,监听下载状态

registerReceiver(receiver, new IntentFilter(

DownloadManager.ACTION_DOWNLOAD_COMPLETE));

}

~~~

#### 广播接收

上面代码用到了receiver

~~~

// 广播接受者,接收下载状态

private BroadcastReceiver receiver = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

checkDownloadStatus();// 检查下载状态

}

};

// 检查下载状态

private void checkDownloadStatus() {

DownloadManager.Query query = new DownloadManager.Query();

query.setFilterById(mTaskId);// 筛选下载任务,传入任务ID,可变参数

Cursor c = downloadManager.query(query);

if (c.moveToFirst()) {

int status = c.getInt(c

.getColumnIndex(DownloadManager.COLUMN_STATUS));

switch (status) {

case DownloadManager.STATUS_PAUSED:

Log.i("download", ">>>下载暂停");

case DownloadManager.STATUS_PENDING:

Log.i("download", ">>>下载延迟");

case DownloadManager.STATUS_RUNNING:

Log.i("download", ">>>正在下载");

break;

case DownloadManager.STATUS_SUCCESSFUL:

Log.i("download", ">>>下载完成");

// 下载完成安装APK

String downloadPath = Environment

.getExternalStoragePublicDirectory(

Environment.DIRECTORY_DOWNLOADS)

.getAbsolutePath()

+ File.separator + fileName;

installAPK(new File(downloadPath));

break;

case DownloadManager.STATUS_FAILED:

Log.e("download", ">>>下载失败");

break;

}

}

}

~~~

#### 下载完成

上面代码用到了installAPK,作用是下载完成后开始安装。

~~~

// 下载到本地后执行安装

protected void installAPK(File file) {

if (!file.exists())

return;

Intent intent = new Intent(Intent.ACTION_VIEW);

Uri uri = Uri.parse("file://" + file.toString());

intent.setDataAndType(uri, "application/vnd.android.package-archive");

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(intent);

}

~~~

如果要静默安装,上面的方法是不行的,设备必须root。可以参考[这里](https://blog.youkuaiyun.com/shinay/article/details/8465543)

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值