一、简介

1.在onListItemClick()中new Intent,Intent以存储序列化后的mp2Info对象作为参数,启动serivce

2.DownloadService在onStartCommand()中通过intent 获取mp3info,开启新线程,利用HttpDownloader下载文件到sdcard

二、代码
1.xml
(1)AndroidManifest.xml

增加

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

2.java
(1)Mp3ListActivity.java

     @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//根据用户点击列表当中的位置来得到响应的Mp3Info对象
Mp3Info info = infos.get(position);
//生成Intent对象
Intent intent = new Intent();
//将Mp3Info对象存入到Intent对象当中
intent.putExtra("mp3Info", info);
intent.setClass(this, DownloadService.class);
//启动Service
startService(intent);
super.onListItemClick(l, v, position, id);
}

(2)DownloadService.java

 package tony.mp3player.service;

 import tony.download.HttpDownloader;
import tony.model.Mp3Info;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder; public class DownloadService extends Service{ @Override
public IBinder onBind(Intent intent) {
return null;
} //每次用户点击ListActivity当中的一个条目时,就会调用该方法
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//从Intent对象当中将Mp3Info对象取出
Mp3Info info = (Mp3Info) intent.getSerializableExtra("mp3Info");
//生成一个下载线程,并将Mp3Info对象作为参数传递到线程对象当中
DownloadThread thread = new DownloadThread(info);
//启动新线程
new Thread(thread).start();
return super.onStartCommand(intent, flags, startId);
} class DownloadThread implements Runnable {
private Mp3Info info; public DownloadThread(Mp3Info info) {
super();
this.info = info;
} @Override
public void run() {
//根据MP3文件的名字,生成下载地址
String urlStr = "http://192.168.1.104:8080/mp3/" + info.getMp3Name();
HttpDownloader downloader = new HttpDownloader();
int result = downloader.downFile(urlStr, "mp3", info.getMp3Name());
String msg = null;
switch (result) {
// -1:代表下载文件出错 0:代表下载文件成功 1:代表文件已经存在
case -1:
msg = "下载失败";break;
case 0:
msg = "文件下载成功";break;
case 1:
msg = "文件已经存在,不需要重复下载";break;
}
//使用Notification提示客户下载结果
} }
}

ANDROID_MARS学习笔记_S01原始版_021_MP3PLAYER001_下载mp3文件的更多相关文章

  1. ANDROID_MARS学习笔记_S01原始版_009_下载文件

    一.代码1.xml(1)main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...

  2. ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER003_播放mp3

    一.简介 1.在onListItemClick中实现点击条目时,跳转到PlayerActivity,mp3info通过Intent传给PlayerActivity 2.PlayerActivity通过 ...

  3. ANDROID_MARS学习笔记_S01原始版_020_Mp3player001_歌曲列表

    一.项目设计 二.歌曲列表简介 1.利用java.net.HttpURLConnection以流的形式下载xml文件为String 2.自定义ContentHandler-->Mp3ListCo ...

  4. ANDROID_MARS学习笔记_S01原始版_011_XML

    一.代码 1.xml(1)main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLay ...

  5. ANDROID_MARS学习笔记_S01原始版_005_RadioGroup\CheckBox\Toast

    一.代码 1.xml(1)radio.xml <?xml version="1.0" encoding="utf-8"?> <LinearLa ...

  6. ANDROID_MARS学习笔记_S01原始版_004_TableLayout

    1.xml <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android ...

  7. ANDROID_MARS学习笔记_S01原始版_003_对话框

    1.AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest ...

  8. ANDROID_MARS学习笔记_S01原始版_002_实现计算乘积及menu应用

    一.代码 1.xml(1)activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk ...

  9. ANDROID_MARS学习笔记_S01原始版_001_Intent

    一.Intent简介 二.代码 1.activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.co ...

随机推荐

  1. 可发布指定的ASP.NET页面的插件:LimusicAddin

    涉及到的技术点 VS插件开发.推荐阅读:Visual Studio 2008 可扩展性开发 asp.net 预编译.使用aspnet_comlier.exe(在目录:C:\Windows\Micros ...

  2. effective c++(07)之为多态基类声明virtual析构函数

    class TimeKeeper { public: TimeKeeper() ; ~TimeKepper() ; ... } ; class AtomicClock:public TimeKeepe ...

  3. glassfish 日志输出配置

    asadmin set-log-levels javax.enterprise.system.tools.deployment=WARNING

  4. [转]十年前的老文:以 Linux 的名义

    一.灰姑娘的狂欢 今年初,林纳斯·托瓦兹承认:“如果在12年前,有人告诉我Linux会发展到今天的模样,我肯定会惊得目瞪口呆.” 托瓦兹说的是实话.1991年,这名21岁的芬兰赫尔辛基大学的学生,偶然 ...

  5. 用Python进行语音信号处理

    1.语音信号处理之时域分析-音高追踪及其Python实现 2.语音信号处理之时域分析-音高及其Python实现 参考: 1.NumPy

  6. .NET和JSON

    JSON(JavaScript Object Notation)已经成为了现在大多数程序间数据交换的存储格式,在很多地方取代了XML文件的位置.JSON实际就是JavaScript的对象数组. .NE ...

  7. Unable to make the session state request to the session state server处理方法

    Server Error in '/' Application. Unable to make the session state request to the session state serve ...

  8. debian 学习记录-2 -账户 -关机

    linux考虑系统安全设定了root账号和user账号 权限较低的user账号下,连关机命令都执行不了…… 用户切换... 用户切换1 命令su(在user账号下,即可开启root账号模式) 用户切换 ...

  9. HTMLImageElement类型的简便利用

    这个是我在复习书籍的时候看见的,当时一个同学想通过页面发送请求,但是数据量不是太大,所以用的get方式,但是页面用表单提交请求的话会让页面进行跳转,当时我在网上查了一点资料,发现基本上都是通过ajax ...

  10. 64位Win8系统下安装Oracle12c

    经过3个小时的折腾,终于在64位win8系统下成功安装了Oracle 12c.这篇文章主要把安装过程中遇到的一些问题总结一下,以便帮助后来人参考. 首先我把我的机器的主要配制情况列举出来: 1. 系统 ...