欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现Service下载文件,Notification显示下载进度的示例

程序员文章站 2024-02-18 18:47:58
先放个gif。。最终效果如果: 主要演示了android从服务器下载文件,调用notification显示下载进度,并且在下载完毕以后点击通知会跳转到安装apk...

先放个gif。。最终效果如果:

Android实现Service下载文件,Notification显示下载进度的示例

主要演示了android从服务器下载文件,调用notification显示下载进度,并且在下载完毕以后点击通知会跳转到安装apk的界面,演示是在真实的网络环境中使用真实的url进行演示,来看看代码:

mainactivity代码非常简单,就是启动一个service:

public class mainactivity extends appcompatactivity {
 string download_url="http://shouji.360tpcdn.com/160329/a9037075b8d3aa98fbf6115c54a5b895/com.alensw.picfolder_4722404.apk";

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);

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

downloadservice里面,在onstartcommand方法里面是关键代码,调用notifyutil这个工具类的“notify_progress”方法去显示一个通知,与此同时开始下载apk文件,downloadservice代码如下:

public class downloadservice extends service {
 string download_url;
 string savepath= environment.getexternalstoragedirectory()+"/liulan.apk";
 private int requestcode = (int) systemclock.uptimemillis();
 private notifyutil currentnotify;
 file mfile;
 @nullable
 @override
 public ibinder onbind(intent intent) {
  return null;
 }

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


 }

 @override
 public int onstartcommand(intent intent, int flags, int startid) {
  mfile=new file(savepath);
  download_url=intent.getstringextra("download_url");
  log.e("test","执行onstartcommand");
  //设置想要展示的数据内容
  intent intent_noti = new intent();
  intent_noti.setaction(intent.action_view);
  //文件的类型,从tomcat里面找
  intent_noti.setdataandtype(uri.fromfile(mfile), "application/vnd.android.package-archive");
  pendingintent rightpendintent = pendingintent.getactivity(this,
    requestcode, intent_noti, pendingintent.flag_update_current);
  int smallicon = r.drawable.xc_smaillicon;
  string ticker = "正在更新快图浏览";
  //实例化工具类,并且调用接口
  notifyutil notify7 = new notifyutil(this, 7);
  notify7.notify_progress(rightpendintent, smallicon, ticker, "快图浏览升级程序", "正在下载中",
    false, false, false, download_url, savepath, new notifyutil.downloadlistener() {
     @override
     public void onsuccess(file file) {
      mfile=file;
      downloadservice.this.stopself();
     }

     @override
     public void onfailure(throwable t, int errorno, string strmsg) {

     }
    });
  currentnotify = notify7;
  return super.onstartcommand(intent, flags, startid);

 }
}

在调用“notify_progress”方法的时候,已经开始下载文件了,那么下载的代码是什么呢?如下:

public void notify_progress(pendingintent pendingintent, int smallicon,
        string ticker, string title, string content,
        boolean sound, boolean vibrate, boolean lights,
        string download_url, string savepath, final downloadlistener listener) {

  setcompatbuilder(pendingintent, smallicon, ticker, title, content, sound, vibrate, lights);
  /*
   * 因为进度条要实时更新通知栏也就说要不断的发送新的提示,所以这里不建议开启通知声音。
   * 这里是作为范例,给大家讲解下原理。所以发送通知后会听到多次的通知声音。
   */
  finalhttp fh = new finalhttp();
  httphandler<file> httphandler=fh.download(download_url, savepath, new ajaxcallback<file>() {
   @override
   public void onloading(long count, long current) {
    super.onloading(count, current);
    double a=count;
    double b=current;
    double currentpro=(double)((b/a)*100);
    cbuilder.setprogress(100, (int)currentpro, false);
    sent();
   }

   @override
   public void onsuccess(file file) {
    super.onsuccess(file);
    cbuilder.setcontenttext("下载完成").setprogress(0, 0, false);
    sent();
    listener.onsuccess(file);
   }

   @override
   public void onfailure(throwable t, int errorno, string strmsg) {
    super.onfailure(t, errorno, strmsg);
    listener.onfailure(t,errorno,strmsg);
   }

  });


 }

这里用到了

这个jar已经封装好下载的工具类,我们直接拿来用就行。下载成功之后会通过downloadlistener这个接口回调到downloadservice里面,最终运行效果就如最上面那个gif动态图运行效果一样。

项目下载地址:点击下载

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