博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ANDROID_MARS学习笔记_S01原始版_021_MP3PLAYER001_下载mp3文件
阅读量:4314 次
发布时间:2019-06-06

本文共 2563 字,大约阅读时间需要 8 分钟。

一、简介

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

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

二、代码

1.xml
(1)AndroidManifest.xml

增加

1 

 

2.java

(1)Mp3ListActivity.java

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

 

(2)DownloadService.java

1 package tony.mp3player.service; 2  3 import tony.download.HttpDownloader; 4 import tony.model.Mp3Info; 5 import android.app.Service; 6 import android.content.Intent; 7 import android.os.IBinder; 8  9 public class DownloadService extends Service{10 11     @Override12     public IBinder onBind(Intent intent) {13         return null;14     }15     16     //每次用户点击ListActivity当中的一个条目时,就会调用该方法17     @Override18     public int onStartCommand(Intent intent, int flags, int startId) {19         //从Intent对象当中将Mp3Info对象取出20         Mp3Info info = (Mp3Info) intent.getSerializableExtra("mp3Info");21         //生成一个下载线程,并将Mp3Info对象作为参数传递到线程对象当中22         DownloadThread thread = new DownloadThread(info);23         //启动新线程24         new Thread(thread).start();25         return super.onStartCommand(intent, flags, startId);26     }27 28     class DownloadThread implements Runnable {29         private Mp3Info info;30         31         public DownloadThread(Mp3Info info) {32             super();33             this.info = info;34         }35 36         @Override37         public void run() {38             //根据MP3文件的名字,生成下载地址39             String urlStr = "http://192.168.1.104:8080/mp3/" + info.getMp3Name();40             HttpDownloader downloader = new HttpDownloader();41             int result = downloader.downFile(urlStr, "mp3", info.getMp3Name());42             String msg = null;43             switch (result) {44             // -1:代表下载文件出错 0:代表下载文件成功 1:代表文件已经存在45             case -1:46                 msg = "下载失败";break;47             case 0:48                 msg = "文件下载成功";break;49             case 1:50                 msg = "文件已经存在,不需要重复下载";break;51             }52             //使用Notification提示客户下载结果53         }54         55     }56 }

 

转载于:https://www.cnblogs.com/shamgod/p/5195530.html

你可能感兴趣的文章
Oracle_创建和管理表
查看>>
Retry Pattern
查看>>
字符串反转---指针
查看>>
SyntaxError: keyword can't be an expression解决方法
查看>>
高级特性(2)-迭代
查看>>
Android上PhoneGap打包本地网站和在线网站
查看>>
HDU-2052(字符画图问题)
查看>>
jython学习笔记3
查看>>
Web测试
查看>>
模型搭建练习2_实现nn模块、optim、two_layer、dynamic_net
查看>>
使用jQuery开发datatable分页表格插件
查看>>
C语言笔记(枚举)
查看>>
coreseek安装使用
查看>>
苹果电脑提示打不开 因为它来自身份不明的开发者 不能安装下载的苹果软件解决方法...
查看>>
收发ICMP封包,实现ping
查看>>
MySql command line client 命令系列
查看>>
内置函数2
查看>>
扩展 IEnumerable<T>,让它根据另一个集合的顺序来排列
查看>>
mvc4.0添加EF4.0时发生编译时错误
查看>>
第16月第12天 CABasicAnimation 旋转加速
查看>>