Android 调用手机自带的下载器下载

亲测有用,原文下载地址:

原文地址:https://blog.csdn.net/weixin_36554045/article/details/79108796

 

下面是原文:

创建一个广播类

public class UpdataBroadcastReceiver extends BroadcastReceiver {

    @SuppressLint("NewApi")
    public void onReceive(Context context, Intent intent) {
        long myDwonloadID = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
        SharedPreferences sPreferences = context.getSharedPreferences("downloadcomplete", 0);
        long refernece = sPreferences.getLong("refernece", 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);
        }
    }

}

在AndroidManifest.xml中去注册广播

 

<receiver android:name=".updata.UpdataBroadcastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
    </intent-filter>
</receiver>

最后就是我们的下载方法

1.下载APP更新时

 

private void downloadAPK(String url) {
    DownloadManager dManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    Uri uri = Uri.parse(url);
    DownloadManager.Request request = new DownloadManager.Request(uri);
    // 设置下载路径和文件名
    request.setDestinationInExternalPublicDir("xxx(路径)", "xxx.apk");
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setMimeType("application/vnd.android.package-archive");
    // 设置为可被媒体扫描器找到
    request.allowScanningByMediaScanner();
    // 设置为可见和可管理
    request.setVisibleInDownloadsUi(true);
    long refernece = dManager.enqueue(request);
    // 把当前下载的ID保存起来
    SharedPreferences sPreferences = getSharedPreferences("downloadcomplete", 0);
    sPreferences.edit().putLong("refernece", refernece).commit();
}

2.下载其他文件时

 

private void imageDownload(String downloadUrl) {
    DownloadManager dManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    Uri uri = Uri.parse(downloadUrl);
    DownloadManager.Request request = new DownloadManager.Request(uri);
    String[] urlname = downloadUrl.split("/");
    // 设置下载路径和文件名
    request.setDestinationInExternalPublicDir("xxx(路径)", urlname[urlname.length - 1]);
    // request.setDescription("DOTA2资料库新版本下载");
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    //request.setMimeType("application/vnd.android.package-archive");
    // 设置为可被媒体扫描器找到
    request.allowScanningByMediaScanner();
    // 设置为可见和可管理
    request.setVisibleInDownloadsUi(true);
    long refernece = dManager.enqueue(request);
}

 

转载于:https://my.oschina.net/u/1462828/blog/1858803

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值