android4.4中关于不能再主线程下载和在主线程中更新UI的矛盾
本人每次在使用android时总是喜欢用最新的sdk(其实是做系统就删了),这样就发现不知从什么时候起android就不让在主线程中下载东西,即使是很小的东西东不行。这也算是对程序的规范吧,因为如果在一段时间中不能下载完成,主线程一直处于假死状态,系统会强制关闭的。然而,我们经常需要在下载数据以后进行UI的更新。这又造成两个不在一个线程中。下面贴出一些代码来解决这个问题。
package com.yx.mp3player;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import com.yx.download.HttpDownload;
import com.yx.model.Mp3Info;
import com.yx.xml.Mp3ListContentHandler;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SimpleAdapter;
public class Mp3ListActivity extends ListActivity {
private static final int UPDATE = 1;
private static final int ABOUT = 1;
private HandlerThread handlerThread;
private Handler handler;
private List<Mp3Info> mp3Infos = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mp3_list);
handler = new Handler();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mp3_list, menu);
menu.add(0, UPDATE, 1, "更新");
menu.add(0, ABOUT, 2, "关于");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(1==item.getItemId()){
//用户点击更新按钮
new Thread(new Runnable() {
@Override
public void run() {
//实现下载
String xmlStr;
xmlStr = downLoadXML("http://192.168.0.103:8080/mp3/resouces.xml");
mp3Infos = parse(xmlStr);
handler.post(new Runnable(){
@Override
public void run() {
//更新UI
List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
for (Iterator iterator = mp3Infos.iterator(); iterator.hasNext();) {
Mp3Info mp3Info = (Mp3Info) iterator.next();
HashMap<String,String> map = new HashMap<String, String>();
map.put("mp3_name", mp3Info.getMp3Name());
map.put("mp3_size", mp3Info.getMp3Size());
list.add(map);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(Mp3ListActivity.this, list,
R.layout.mp3info_item,
new String[]{"mp3_name","mp3_size"},
new int[]{R.id.mp3_name,R.id.mp3_size});
setListAdapter(simpleAdapter);
System.out.println("=================");
}});
}
}).start();
}else if(ABOUT==item.getItemId()){
//用户点击了关于按钮
}
return super.onOptionsItemSelected(item);
}
public String downLoadXML(String urlstr){
HttpDownload httpDownloader = new HttpDownload();
String result = httpDownloader.downLoad(urlstr);
System.out.println("XML--->"+result);
return result;
}
private List<Mp3Info> parse(String xmlStr){
List<Mp3Info> infos = new ArrayList<Mp3Info>();
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
try {
XMLReader xmlReader= saxParserFactory.newSAXParser().getXMLReader();
Mp3ListContentHandler mp3ListContentHandler = new Mp3ListContentHandler(infos);
xmlReader.setContentHandler(mp3ListContentHandler);
xmlReader.parse(new InputSource(new StringReader(xmlStr)));
} catch (Exception e) {
e.printStackTrace();
}
return infos;
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。