Android DownloadManager 下载完成并安装

DownloadManager 下载完成并安装


话说blog还是要坚持写的。仅仅是一个态度的问题 …….

DownloadManager 是Android系统提供的一个很好用的下载类。通过此类可以很方便的下载文件并在通知栏显示进度,不用再重写通知栏。所以记录一下使用方法与一些技巧。望批判

直接上代码吧
public void download(String downloadUrl) {
        DownloadManager manager = (DownloadManager) mContext
                .getSystemService(Context.DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(
                Uri.parse(downloadUrl));
        request.setDescription("更新APP");
        request.allowScanningByMediaScanner();// 设置可以被扫描到
        request.setVisibleInDownloadsUi(true);// 设置下载可见
        request.setNotificationVisibility(
                DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);//下载完成后通知栏任然可见
        String fileName = downloadUrl.substring(downloadUrl.lastIndexOf("/"));// 解析fileName
        request.setDestinationInExternalPublicDir(
                Environment.DIRECTORY_DOWNLOADS, fileName);// 设置下载位置,sdcard/Download/fileName
        long refernece = manager.enqueue(request);// 加入下载并取得下载ID  
        SharedPreferences sPreferences = mContext.getSharedPreferences(
                "downloadplato", 0);
        sPreferences.edit().putLong("plato", refernece).commit();//保存此次下载ID

    }

获取并保存此次下载ID为了方便监听下载完成,并处理相关下载后的事情,比如下载一个app,为了提高用户体验就要自动弹出安装界面。这时候我们就要进行下载监听

DownLoadBroadcastReceiver.java

package com.android.browser;

import android.annotation.SuppressLint;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;

import com.android.browser.core.LogUtils;
/**
 * 下载完成广播监听:比如下载APP
 * 
 * */
public class DownLoadBroadcastReceiver extends BroadcastReceiver {

    @SuppressLint("NewApi")
    public void onReceive(Context context, Intent intent) {
        long myDwonloadID = intent.getLongExtra(
                DownloadManager.EXTRA_DOWNLOAD_ID, -1);
        LogUtils.i("下载完成 ID = " + myDwonloadID);
        SharedPreferences sPreferences = context.getSharedPreferences(
                "downloadplato", 0);
        long refernece = sPreferences.getLong("plato", 0);
        if (refernece == myDwonloadID) {
            String serviceString = Context.DOWNLOAD_SERVICE;
            DownloadManager dManager = (DownloadManager) context
                    .getSystemService(serviceString);
            Intent install = new Intent(Intent.ACTION_VIEW);
            Uri downloadFileUri = dManager
                    .getUriForDownloadedFile(myDwonloadID);
            install.setDataAndType(downloadFileUri,
                    "application/vnd.android.package-archive");
            install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(install);
        }

    }

}

 // 记得注册广播

完成 Thank you . 谢谢

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值