当前位置: 移动技术网 > 移动技术>移动开发>Android > Android 基于IntentService的文件下载的示例代码

Android 基于IntentService的文件下载的示例代码

2019年07月24日  | 移动技术网移动技术  | 我要评论

文件下载这种事情是很耗时的。之前使用asynctask这样的异步类来做下载,然后切到后台就被干掉。所以打算试试service。(不过按目前那些系统的尿性,其实service也分分钟被干掉)

不过,这里并不是直接使用service类,而是使用的是继承自service的intentservice。

这个东西有三大好处:

1.他有个任务队列;
2.任务队列执行完后会自动停止;
3.他会起一个独立的线程,耗时操作不会影响你app的主线程。

这么自动化的东西简直省心。

话不多说,开始撸代码。

首先,要建个应用,主文件如下(布局什么的代码就不贴了):

package net.codepig.servicedownloaderdemo;

import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.widget.button;
import android.widget.edittext;

public class mainactivity extends appcompatactivity {
  private string _url="http://www.boosj.com/apk/boosjdance.apk";
  private edittext urltext;
  private button gobtn;

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    gobtn=(button) findviewbyid(r.id.gobtn);
    urltext=(edittext) findviewbyid(r.id.urltext);
    urltext.settext(_url);
    gobtn.setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view view) {
        _url=urltext.gettext().tostring();
        //start download
        start_service();
      }
    });
  }

  public void start_service(){
    //等会再填
  }
}

以上代码不重要,嗯。

接下来是重点。创建一个intentservice 类,然后重写他的onhandleintent 。

需要执行的任务就写在onhandleintent 里

这里先用thread.sleep模拟一下耗时任务,试跑一下就可以看到主app关闭后service还在跑,跑完后就自己destroy了。

package net.codepig.servicedownloaderdemo;

import android.app.intentservice;
import android.content.intent;

/**
 * 下载服务
 * created by qzd on 2017/9/20.
 */

public class downloadservice extends intentservice {
  public downloadservice() {
    super("downloadservice");//这就是个name
  }

  @override
  public void oncreate() {
    super.oncreate();
  }

  protected void onhandleintent(intent intent) {
    bundle bundle = intent.getextras();
    string downloadurl = bundle.getstring("download_url");

    log.d(tag,"下载启动:"+downloadurl);
    thread.sleep(1_000);
    int count=0;
    while(count<20){
      count++;
      log.d(tag,"下载运行中--"+count);
      thread.sleep(1000);
    }
    log.d(tag,"下载结束");
  }

  @override
  public void ondestroy() {
    log.d(tag, "ondestroy");
    super.ondestroy();
  }
}

通过intent接收任务,这里在mainactivity中通过startservice启动服务

public void start_service(){
    intent intent=new intent(this,downloadservice.class);
    intent.putextra("download_url",_url);
    startservice(intent);
  }

当然,androidmanifest.xml里也得注册上

复制代码 代码如下:

<service android:name="net.codepig.servicedownloaderdemo.downloadservice"></service>

接下来我们看看怎么下载文件

首先别忘了添加权限:

<uses-permission android:name="android.permission.internet" />
<uses-permission android:name="android.permission.write_external_storage" />
<uses-permission android:name="android.permission.read_external_storage" />
<uses-permission android:name="android.permission.mount_unmount_filesystems" />

添加downloadfile方法管理下载。

下载相关说明都在注释里。

  /**
   * 文件下载
   * @param downloadurl
   * @param file
   */
  private void downloadfile(string downloadurl, file file){
    fileoutputstream _outputstream;//文件输出流
    try {
      _outputstream = new fileoutputstream(file);
    } catch (filenotfoundexception e) {
      log.e(tag, "找不到目录!");
      e.printstacktrace();
      return;
    }
    inputstream _inputstream = null;//文件输入流
    try {
      url url = new url(downloadurl);
      httpurlconnection _downloadcon = (httpurlconnection) url.openconnection();
      _downloadcon.setrequestmethod("get");
      filelength = integer.valueof(_downloadcon.getheaderfield("content-length"));//文件大小
      _inputstream = _downloadcon.getinputstream();
      int respondcode = _downloadcon.getresponsecode();//服务器返回的响应码
      if (respondcode == 200) {
        byte[] buffer = new byte[1024*8];// 数据块,等下把读取到的数据储存在这个数组,这个东西的大小看需要定,不要太小。
        int len;
        while ((len = _inputstream.read(buffer)) != -1) {
          _outputstream.write(buffer, 0, len);
          downloadlength = downloadlength + len;
          log.d(tag, downloadlength + "/" + filelength );
        }
      } else {
        log.d(tag, "respondcode:" + respondcode);
      }
    } catch (exception e) {
      e.printstacktrace();
    } finally {
      try {//别忘了关闭流
        if (_outputstream != null) {
          _outputstream.close();
        }
        if (_inputstream != null) {
          _inputstream.close();
        }
      } catch (ioexception e) {
        e.printstacktrace();
      }
    }
  }

放入onhandleintent执行:

protected void onhandleintent(intent intent) {
    try {
      bundle bundle = intent.getextras();
      string downloadurl = bundle.getstring("download_url");

      file dirs = new file(environment.getexternalstoragedirectory().getabsolutepath() + "/download");//文件保存地址
      if (!dirs.exists()) {// 检查文件夹是否存在,不存在则创建
        dirs.mkdir();
      }

      file file = new file(dirs, "boosj.apk");//输出文件名
      log.d(tag,"下载启动:"+downloadurl+" --to-- "+ file.getpath());
      // 开始下载
      downloadfile(downloadurl, file);
      // 下载结束
      log.d(tag,"下载结束");
    } catch (exception e) {
      e.printstacktrace();
    }
  }

跑一下,嗯,默默的下载完了。

但是,作为一个负责的app,当然要给用户反馈,所以我们要显示一下进度。

我们用notification来显示进度。(需要注意的是,如果有必要调用主ui线程来显示进度的话,要充分考虑到service运行过程中,你的app未必是一直活动着的,可能早就destroy了。)(当然用绑定来启动service的另说,那是另一种使用场景。)
下载前(也就是执行downloadfile方法前)先创建并对通知进行相关设置。

这里使用了notificationcompat.builder()这个方法。如果不考虑旧版本的兼容,可以使用notification.builder()方法。

private notificationcompat.builder builder;
private notificationmanager manager;
public void initnotification(){
    builder = new notificationcompat.builder(this);
    builder.setsmallicon(r.mipmap.ic_launcher).setcontenttitle("下载文件").setcontenttext("下载中……");//图标、标题、内容这三个设置是必须要有的。
    manager = (notificationmanager) getsystemservice(context.notification_service);
  }

然后使用notificationmanager.notify()方法将通知发送给系统。需要更新的话再次notify()给同一个id的通知,如果该通知已存在则会更新,不存在就新建。

private int _notificationid= 1024;//嗯,这是一个十分绅士的id
manager.notify(_notificationid,builder.build());

为了显示进度,使用handler和runnable来定时刷新,并通过setprogress方法显示进度条。

private handler handler = new handler();
private runnable run = new runnable() {
    public void run() {
      int _pec=(int) (downloadlength*100 / filelength);
      builder.setcontenttext("下载中……"+_pec+"%");
      builder.setprogress(100, _pec, false);//显示进度条,参数分别是最大值、当前值、是否显示具体进度(false显示具体进度,true就只显示一个滚动色带)
      manager.notify(_notificationid,builder.build());
      handler.postdelayed(run, 1000);
    }
  };

完事了以后如果需要清除通知可以使用manager.cancelall();或者manager.cancel(int );

完整代码再来一遍

package net.codepig.servicedownloaderdemo;

import android.app.intentservice;
import android.app.notificationmanager;
import android.content.context;
import android.content.intent;
import android.os.bundle;
import android.os.environment;
import android.os.handler;
import android.support.v4.app.notificationcompat;
import android.util.log;

import java.io.file;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.net.httpurlconnection;
import java.net.url;

/**
 * 下载服务
 * created by qzd on 2017/9/20.
 */

public class downloadservice extends intentservice {
  private final string tag="logcat";
  private int filelength, downloadlength;//文件大小
  private handler handler = new handler();
  private notificationcompat.builder builder;
  private notificationmanager manager;
  private int _notificationid = 1024;
  public downloadservice() {
    super("downloadservice");//这就是个name
  }

  @override
  public void oncreate() {
    super.oncreate();
  }

  protected void onhandleintent(intent intent) {
    try {
      initnotification();

      bundle bundle = intent.getextras();
      string downloadurl = bundle.getstring("download_url");

      file dirs = new file(environment.getexternalstoragedirectory().getabsolutepath() + "/download");//文件保存地址
      if (!dirs.exists()) {// 检查文件夹是否存在,不存在则创建
        dirs.mkdir();
      }

      file file = new file(dirs, "boosj.apk");//输出文件名
      log.d(tag,"下载启动:"+downloadurl+" --to-- "+ file.getpath());
      manager.notify(_notificationid,builder.build());
      // 开始下载
      downloadfile(downloadurl, file);
      // 下载结束
      builder.setprogress(0,0,false);//移除进度条
      builder.setcontenttext("下载结束");
      manager.notify(_notificationid,builder.build());
//      manager.cancelall();
//      manager.cancel(_notificationid);

      // 广播下载完成事件,通过广播调起对文件的处理。(就不多说了,在实际需要的地方接收广播就好了。)
      intent sendintent = new intent("downloadcomplete");
      sendintent.putextra("downloadfile", file.getpath());
      sendbroadcast(sendintent);
      log.d(tag,"下载结束");
    } catch (exception e) {
      e.printstacktrace();
    }
  }

  /**
   * 文件下载
   * @param downloadurl
   * @param file
   */
  private void downloadfile(string downloadurl, file file){
    fileoutputstream _outputstream;//文件输出流
    try {
      _outputstream = new fileoutputstream(file);
    } catch (filenotfoundexception e) {
      log.e(tag, "找不到目录!");
      e.printstacktrace();
      return;
    }
    inputstream _inputstream = null;//文件输入流
    try {
      url url = new url(downloadurl);
      httpurlconnection _downloadcon = (httpurlconnection) url.openconnection();
      _downloadcon.setrequestmethod("get");
      filelength = integer.valueof(_downloadcon.getheaderfield("content-length"));//文件大小
      _inputstream = _downloadcon.getinputstream();
      int respondcode = _downloadcon.getresponsecode();//服务器返回的响应码
      if (respondcode == 200) {
        handler.post(run);//更新下载进度
        byte[] buffer = new byte[1024*8];// 数据块,等下把读取到的数据储存在这个数组,这个东西的大小看需要定,不要太小。
        int len;
        while ((len = _inputstream.read(buffer)) != -1) {
          _outputstream.write(buffer, 0, len);
          downloadlength = downloadlength + len;
//          log.d(tag, downloadlength + "/" + filelength );
        }
      } else {
        log.d(tag, "respondcode:" + respondcode);
      }
    } catch (exception e) {
      e.printstacktrace();
    } finally {
      try {//别忘了关闭流
        if (_outputstream != null) {
          _outputstream.close();
        }
        if (_inputstream != null) {
          _inputstream.close();
        }
      } catch (ioexception e) {
        e.printstacktrace();
      }
    }
  }

  private runnable run = new runnable() {
    public void run() {
      int _pec=(int) (downloadlength*100 / filelength);
      builder.setcontenttext("下载中……"+_pec+"%");
      builder.setprogress(100, _pec, false);//显示进度条,参数分别是最大值、当前值、是否显示具体进度(false显示具体进度,true就只显示一个滚动色带)
      manager.notify(_notificationid,builder.build());
      handler.postdelayed(run, 1000);
    }
  };

  @override
  public void ondestroy() {
    log.d(tag, "ondestroy");
    handler.removecallbacks(run);
    super.ondestroy();
  }

  public void initnotification(){
    builder = new notificationcompat.builder(this);
    builder.setsmallicon(r.mipmap.ic_launcher).setcontenttitle("下载文件").setcontenttext("下载中……");//图标、标题、内容这三个设置是必须要有的。
    manager = (notificationmanager) getsystemservice(context.notification_service);
  }
}

相关github项目地址:servicedownloaderdemo

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网