android必须使用子线程才能够做耗时操作,这点虽然比较符合优秀应用的特点,但是多线程真是让人有点头疼,不管怎么样,那几个runable,handler什么的,我真心记不住它里面有些什么,所以我写篇博文,把我以前的android多线程代码贴上来,等忘掉的时候好来看看。
private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case 1: viewPager.setCurrentItem(currentItem);// 切换当前显示的图片 break; case 2: if(title!=null) { ListView newsList = (ListView)findViewById(R.id.homepage_newslist); ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>(); HashMap<String, String> item; for(int i =0;i<title.length;i++) { item=new HashMap<String, String>(); item.put("titile", title[i]); item.put("time", time[i]); list.add(item); } SimpleAdapter adapter = new SimpleAdapter(Homepage.this,list,R.layout.homepagelistitem,new String[]{"titile","time"},new int[]{R.id.homepage_newstitle,R.id.homepage_newstime}); newsList.setAdapter(adapter); ////////////////////////////////////// for(int i =0;i<bitstr.length;i++) { titles[i] = bitstr[i]; } if(bitUrl!=null) { bit = new Bitmap[5]; Runnable thread = new Runnable(){ @Override public void run() { //这里下载数据 try{ for(int m=0;m<bitUrl.length;m++) { URL url= new URL(bitUrl[m]); HttpURLConnection conn= (HttpURLConnection)url.openConnection(); conn.setDoInput(true); conn.connect(); InputStream inputStream=conn.getInputStream(); bit[m]= BitmapFactory.decodeStream(inputStream); } Message msg=new Message(); msg.what=3; handler.sendMessage(msg); } catch(MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace(); } } }; new Thread(thread).start(); } } break; case 3: if(bit!=null) { for(int j=0;j<bit.length;j++) { ImageView p_w_picpathView = new ImageView(Homepage.this); p_w_picpathView.setImageBitmap(bit[j]); p_w_picpathView.setScaleType(ScaleType.FIT_XY); p_w_picpathViews.add(msg.arg1, p_w_picpathView); } } else { Toast.makeText(getApplicationContext(), "加载图片失败,请检查网络",Toast.LENGTH_SHORT).show(); } break; } }; };
以上为主线程的Ui更改代码,msg.what == 1的时候是一个图片切换的消息,大家忽略它
msg.what == 2表示从网络中已经获取到了文字信息,可以更新了文字显示了,我将文字放在了一个listview里面,所以用了SimpleAdapter还有数组。。
msg.what == 3表示图片下载好了,可以装载图片了,也是用来更新UI的代码。。
网页文字下载函数,同时获取图片地址:
Runnable thread = new Runnable(){ @Override public void run() { //这里下载数据 try{ URL url= new URL("http://xx.xx.xx.xx/MobileApp/QueryShopInfo.aspx?userid=EMHHBMDTAw4="); HttpURLConnection conn= (HttpURLConnection)url.openConnection(); conn.setDoInput(true); conn.connect(); InputStream inputStream=conn.getInputStream(); if(inputStream!=null) { int length = conn.getContentLength(); byte [] buffer = new byte[1024]; inputStream.read(buffer); for (int j = 0; j < buffer.length; j++) { if (buffer[j] == 0) { length = j; break; } } byte [] buf=new byte[length]; System.arraycopy(buffer, 0, buf, 0, length); String temp1= EncodingUtils.getString(buf, "GB2312"); temp1 = temp1.trim(); String[] temp2 =temp1.split("&"); String[] temp21 = temp2[0].split("#"); String[] temp22 = temp2[1].split("#"); int i; title = new String[temp22.length]; time = new String[temp22.length];; URL =new String[temp22.length]; for(i=0;i<temp22.length;i++) { String[] temp = temp22[i].split("\\|"); title[i] = temp[0]; time[i] = temp[1]; URL[i] = temp[2]; } bitstr = new String[temp21.length]; bitUrl = new String[temp21.length]; for(i=0;i<temp21.length;i++) { String[] temp = temp21[i].split("\\|"); bitstr[i] = temp[0]; bitUrl[i] = temp[1]; } Message msg=new Message(); msg.what=2; handler.sendMessage(msg); } } catch(MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }; new Thread(thread).start(); }
下面是图片读取函数:
Runnable thread = new Runnable(){ @Override public void run() { //这里下载数据 try{ for(int m=0;m<bitUrl.length;m++) { URL url= new URL(bitUrl[m]); HttpURLConnection conn= (HttpURLConnection)url.openConnection(); conn.setDoInput(true); conn.connect(); InputStream inputStream=conn.getInputStream(); bit[m]= BitmapFactory.decodeStream(inputStream); } Message msg=new Message(); msg.what=3; handler.sendMessage(msg); } catch(MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace(); } } }; new Thread(thread).start();
大家可能已经在第一个代码段中看到了上面这部分代码,这是因为我要在读取到网页文字信息的同时来开辟新的下载线程下载这些图片,总之就这样。。。