android异步下载mp3文件

android的异步下载文件,即在非ui主线程之外,用新的线程下载mp3文件。达到ui主线程正常运行的状态下,异步下载文件。

考虑的实现方式有两种:1.在使用HandlerThread绑定handler,并在handler处理消息队列的(onhandlerMessage())方法中下载mp3文件,下边是代码。

public class Mp3ListActivity extends ListActivity {

	private List<Map<String,String>> mplist=null;
	private Handler handle=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mp3list);
		Intent intent=getIntent();
		
		List<Mp3Bean> list=(ArrayList<Mp3Bean>) intent.getSerializableExtra("list");
		mplist=new ArrayList<Map<String,String>>();
		Map<String,String> map=null;
		for(Mp3Bean bean:list){
			map=new HashMap<String,String>();
			map.put("id", bean.getId());
			map.put("name", bean.getName());
			map.put("ip", bean.getIp());
			System.out.println("id"+bean.getId()+"=====name:"+bean.getName());
			mplist.add(map);
		}
		ListAdapter adapter=new SimpleAdapter(this, mplist, R.layout.mp3_item, new String[]{"id","name","ip"}, new int[]{R.id.id,R.id.name,R.id.ip});
		setListAdapter(adapter);
		HandlerThread thread=new HandlerThread("down");
		thread.start();
		handle=new MyHandler(thread.getLooper());
	}

	/* 
	 * 列表被选中
	 */
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Map<String,String> map=this.mplist.get(position);
		Message msg=handle.obtainMessage();
		msg.obj=map;
		msg.sendToTarget();
		//通过handlerThread来下载文件
	}

	private class MyHandler extends Handler{

		public MyHandler(Looper looper) {
			super(looper);
		}

		/* (non-Javadoc)
		 * @see android.os.Handler#handleMessage(android.os.Message)
		 */
		@Override
		public void handleMessage(Message msg) {
			Map<String,String> map=(Map<String, String>) msg.obj;
			String urlStr=map.get("ip");
			//在线程中下载文件
			HttpDownUtil down=new HttpDownUtil(urlStr);
			int a=down.downURLMp3("ova/", map.get("name"));
			if(a==0){
				Toast.makeText(Mp3ListActivity.this, "下载成功", Toast.LENGTH_LONG).show();
			}else if(a==1){
				Toast.makeText(Mp3ListActivity.this, "文件已经下载", Toast.LENGTH_LONG).show();
			}else{
				Toast.makeText(Mp3ListActivity.this, "下载错误", Toast.LENGTH_LONG).show();
			}
		}
		
	}

 2.第二中实现方式是在service中启动新线程并用notification来通知用户更改ui界面的显示:

public class Mp3DownService extends Service {

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Mp3Bean bean=(Mp3Bean) intent.getSerializableExtra("bean");
		//创建下载文件的线程
		Thread t=new Thread(new DownThread(bean));
		t.start();
		return super.onStartCommand(intent, flags, startId);
	}
	
	private class DownThread implements Runnable{
		Mp3Bean bean=null;
		public DownThread(Mp3Bean bean){
			this.bean=bean;
		}
		public void run() {
			String mp3UrlStr="http://192.168.1.2:8888/download/"+bean.getMp3name();
			HttpDown down=new HttpDown(mp3UrlStr);
			int a=down.downMp3("ova/", bean.getMp3name());
			String resault="";
			if(a==0){
				resault="文件下载成功!";
			}else if(a==1){
				resault="文件已经存在!";
			}else{
				resault="下载出错!";
			}
			//Notification来通知用户修改ui界面
		}
		
	}
 

新手水品,有任何问题,欢迎留言。

你可能感兴趣的:(android)