Android DownloadManager 下载完成并安装
2016-06-23 15:09
489 查看
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 . 谢谢
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories