首先在Activity中声明Intent对象,启动Service:

    //生成Intent对象
    Intent intent = new Intent();
    //将文件名对象存入到intent对象当中
    intent.putExtra("name", filename);
    intent.setClass(this, DownloadService.class);
    //启动Service
    startService(intent);

  DownloadService定义如下:

    public class DownloadService extends Service{

      @Override
      public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
      }

      //每次用户点击ListActivity当中的一个条目时,就会调用该方法
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
        //从intent对象中将文件名取出
        String name = intent.getExtra("name");
             //生成一个下载线程,并将文件名对象作为参数传递到线程对象当中
        DownloadThread downloadThread = new DownloadThread(name);
        //启动新线程
        Thread thread = new Thread(downloadThread);
        thread.start();
        return super.onStartCommand(intent, flags, startId);
      }

      class DownloadThread implements Runnable{
        private String name = null;
        public DownloadThread(String name){
          this.name = name;
        }
        @Override
        public void run() {
          //下载地址是http://192.168.1.105:8080/mp3/forever.mp3
          //根据MP3文件的名字生成下载地址
          String fileUrl = "http://192.168.1.105:8080/mp3/" + name;
                            //生成下载文件所用的对象
          HttpDownloader httpDownloader = new HttpDownloader();   //此类的定义见上一节文件下载和存入SD卡
          //将文件下载并存储到SDCard当中
          int fileresult = httpDownloader.downFile(fileUrl, "/mp3", name);
                            String resultMessage = null;
          if(fileresult == -1){
            resultMessage = "下载失败";
          }
          else if(fileresult == 0){
            resultMessage = "文件已经存在,不需要重复下载";
          }
          else if(fileresult == 1){
            resultMessage = "文件下载成功";
          }
          //使用Notifications提示客户下载结果
          System.out.println(resultMessage);
        }
      }
    }

Service实现文件下载的更多相关文章

  1. Web Service 的创建简单编码、发布和部署

    最近,老大准备将已有的C/S架构项目中的通信部分做成通用,需要将其支持WebService为以后项目向着B/S架构升级做好铺垫,为此身为屌丝的我去各种百度WebService是个什么卵玩意,然后逐渐搭 ...

  2. [转载]http协议 文件下载原理及多线程断点续传

    最近研究了一下关于文件下载的相关内容,觉得还是写些东西记下来比较好.起初只是想研究研究,但后来发现写个可重用性比较高的模块还是很有必要的,我想这也是大多数开发人员的习惯吧.对于HTTP协议,向服务器请 ...

  3. 开源安卓Http文件下载框架file-downloader的使用

    file-downloader FileDownloader(https://github.com/wlfcolin/file-downloader)是本人开源的一个安卓Http文件下载框架,是根据自 ...

  4. 解决springmvc中文件下载功能中使用javax.servlet.ServletOutputStream out = response.getOutputStream();后运行出异常但结果正确的问题

    问题描述: 在springmvc中实现文件下载功能一般都会使用javax.servlet.ServletOutputStream out = response.getOutputStream();封装 ...

  5. Spring4 MVC文件下载实例

    这篇文章将向您展示如何使用Spring MVC4执行文件下载,我们将看到应用程序从文件系统内部以及外部文件下载文件. 本教程的主要亮点: 下载文件是相当简单的,涉及以下步骤. 创建一个InputStr ...

  6. Android 服务类Service 的详细学习

    http://blog.csdn.net/vipzjyno1/article/details/26004831 Android服务类Service学习四大组建   目录(?)[+] 什么是服务 服务有 ...

  7. spring4 文件下载功能

    需要准备的工具和框架 Spring 4.2.0.RELEASE Bootstrap v3.3.2 Maven 3 JDK 1.7 Tomcat 8.0.21 Eclipse JUNO Service ...

  8. Java Axis2 1.6.3+JDK1.7.0_13+Tomcat7.0.65+eclipse搭建web service

    安装文件下载: jdk1.7.0_13 安装步骤参考文章:http://jingyan.baidu.com/article/6dad5075d1dc40a123e36ea3.html tomcat7. ...

  9. SOAP: java+xfire(web service) + php客户端

    作者: 吴俊杰 web service这项技术暂不说它有多落伍,但是项目中用到了,没法逃避!    xml和json各有各的好处,但是JSON无疑是当今数据交互的主流了.客户soap服务器端用的是 j ...

随机推荐

  1. C# Just want 入门

    1. 终于明白为什么以前的程序在结尾部分会经常出现 input any key to exit! using System; namespace ConsoleApplication1 { class ...

  2. SQL Server 求结果

    ;with cte as ( select  CONVERT(DATE, DATEADD(DAY, -9, GETDATE())) as paytime union all select datead ...

  3. SDOI 2016 游戏

    树链剖分 线段树维护区间最小值,区间最大值 更新,对于每一个区间,找到当前区间的最小值的最大值,和要更新的值比较,如果比最大值还大,则此数对于以后的询问无任何贡献,直接返回即可,若有贡献,则一直递归到 ...

  4. wpf之mvvm基类

    当我们用MVVM设计模式的时候要实现INotifyPropertyChanged,每次都要实现这个接口比较麻烦,所以基类的作用就体现出来了.代码如下:   1 2 3 4 5 6 7 8 9 10 1 ...

  5. SQL Server通过File Header Page来进行Crash Recovery

    SQL Server通过File Header Page来进行Crash Recovery 看了盖总的一篇文章 http://www.eygle.com/archives/2008/11/oracle ...

  6. Android知识杂记(四)

    1.完整退出activity的设计思路 1.1 封装一个基础activity类 public abstract class RootActivity extends FragmentActivity{ ...

  7. ASP.NET Web API路由系统:路由系统的几个核心类型

    虽然ASP.NET Web API框架采用与ASP.NET MVC框架类似的管道式设计,但是ASP.NET Web API管道的核心部分(定义在程序集System.Web.Http.dll中)已经移除 ...

  8. [译]如何防止elasticsearch的脑裂问题

    本文翻译自blog.trifork.com的博文 地址是http://blog.trifork.com/2013/10/24/how-to-avoid-the-split-brain-problem- ...

  9. xamarin UWP自定义圆角按钮

    uwp自带的button本身不支持圆角属性,所以要通过自定义控件实现. 通过设置Button的Background=“{x:Null}”设置为Null使背景为空,再设置Button.Content中的 ...

  10. Android RatingBar 自定义样式

    Android RatingBar 自定义样式 1.先定义Style: <style name="RadingStyle" parent="@android:sty ...