在Android 中如果请求网络,数据量小的时候,可以使用Volley 库,但是如果是下载数据非常大时,变的不可行了。这时我们可以使用Andoid 自带的 DownloadManager ,只要是api 9以上都可以用。网上也有很多例子,但是大部分都是放在Activity 中,这样带来很多的不便。当我们下载大文件时必须在这个界面。所以,我就根据网上的例子,自己改造了一下,把下载放在Service 中,用来下载apk 安装。


   1、首先,拿到启动Service 时传过来的 下载Url ,  DownloadManager 下载后会发出一个广播,所以我们同时注册下载完成的广播;

       

Intent intent = new Intent(MainActivity.this, DownloadRecommdAppService.class);intent.putExtra(DownloadRecommdAppService.DOWNLOAD_URL, url);startService(intent);

   2、因为 Service 也是运行在主线程中的,所以,我们把下载这些耗时的工作另开线程进行。

   3、在广播中,通过判断下载的状态,如果下载成功,就启动安装apk, 同时停止 Service.


  

public class DownloadRecommdAppService extends Service{public static final String DOWNLOAD_URL = "download_url";public static final String DOWNLOAD_FOLDER_NAME = "download";public  static final String DOWNLOAD_FILE_NAME = "yxhuang.apk";@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {if (intent != null) {String downloadUrl = intent.getExtras().getString(DOWNLOAD_URL);if (downloadUrl != null) {// 注册下载完成广播DownloadCompleteReceiver completeReceiver = new DownloadCompleteReceiver();registerReceiver(completeReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));// 下载new Thread(new DownloadRunable(downloadUrl)).start();}}return super.onStartCommand(intent, flags, startId);}// 下载class DownloadRunable implements Runnable{private String mDownloadUrl;public DownloadRunable(String url) {mDownloadUrl = url;}@Overridepublic void run() {startDownload();}// 开始下载private void startDownload(){File folder = Environment.getExternalStoragePublicDirectory(DOWNLOAD_FOLDER_NAME);if (!folder.exists() || !folder.isDirectory()) {folder.mkdirs();}DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);DownloadManager.Request request = new DownloadManager.Request(Uri.parse(mDownloadUrl));request.setMimeType("application/vnd.android.package-archive");// 存储的目录request.setDestinationInExternalPublicDir(DOWNLOAD_FOLDER_NAME, DOWNLOAD_FILE_NAME);// 设定只有在 WIFI 情况下才能下载request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);request.setTitle("下载应用");request.setVisibleInDownloadsUi(true);manager.enqueue(request);}}// 下载完成 class DownloadCompleteReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) { long completeDownloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);if (intent.getAction() == DownloadManager.ACTION_DOWNLOAD_COMPLETE) {checkStatus(context, completeDownloadId);}}private void checkStatus(Context context,Long completeDownloadId){    DownloadManager mManager  = (DownloadManager) context.getSystemService(Service.DOWNLOAD_SERVICE);DownloadManager.Query query = new DownloadManager.Query();query.setFilterById(completeDownloadId);Cursor cursor = mManager.query(query);if (cursor.moveToFirst()) {int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));switch (status) {case DownloadManager.STATUS_RUNNING:break;case DownloadManager.STATUS_SUCCESSFUL:Toast.makeText(context, "下载成功", Toast.LENGTH_SHORT).show(); String apkFilePath = new StringBuilder(Environment.getExternalStorageDirectory().getAbsolutePath())                 .append(File.separator).append(DownloadRecommdAppService.DOWNLOAD_FOLDER_NAME).append(File.separator)                 .append(DownloadRecommdAppService.DOWNLOAD_FILE_NAME).toString(); installApk(context, apkFilePath);  // 停止下载Service DownloadRecommdAppService.this.stopSelf();break;default:break;}}cursor.close();}// 安装APkprivate  void installApk(Context context, String file) {File apkFile = new File(file);Intent intent = new Intent(Intent.ACTION_VIEW);intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");context.startActivity(intent);}}}


 关于 DownloadManager 详细的介绍,移步至 http://www.trinea.cn/android/android-downloadmanager/

       






更多相关文章

  1. android:apk版本的的比对、下载、安装
  2. Android开发环境搭建:SDK在线离线安装+在Eclipse中配置ADT+第一个
  3. Android(安卓)APK文件在电脑(PC虚拟机)上面运行方法
  4. Android(安卓)Studio的使用 及 JDK环境配置(Window10 64位)
  5. Android(安卓)浏览器打开本地APK
  6. 使用Jenkins进行Android自动打包
  7. Android开发入门的正确姿势,你get到了吗?
  8. Retrofit+okHttp+RxJava打造一款简单易用的Android下载框架
  9. android中的通信机制

随机推荐

  1. Android(安卓)- Android(安卓)Studio 安
  2. Android(安卓)体系结构介绍
  3. android:shape的使用
  4. Android(安卓)触屏事件处理_手势识别
  5. android SQLite数据库存储数据
  6. 怎样成为一名Android开发者
  7. 线性布局和相对布局
  8. Android启动过程深入解析
  9. Android(安卓)线性布局(LinearLayout)内
  10. android客户端和servlet服务端的简单登录