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 imageView = new ImageView(Homepage.this);                        imageView.setImageBitmap(bit[j]);                        imageView.setScaleType(ScaleType.FIT_XY);                        imageViews.add(msg.arg1, imageView);                    }                }                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();

大家可能已经在第一个代码段中看到了上面这部分代码,这是因为我要在读取到网页文字信息的同时来开辟新的下载线程下载这些图片,总之就这样。。。


更多相关文章

  1. RxJava(九)zip 操作符在 Android(安卓)中的实际使用场景
  2. android TextView空间的setTextSize()方法在真机上运行大小问题
  3. Android(安卓)studio 快捷键小计
  4. Android(安卓)应用性能优化
  5. Android开发环境的配置与源代码的导入
  6. 提高代码质量-工具篇
  7. android 4.4 以上沉浸式状态栏和沉浸式导航栏管理,一句代码轻松实
  8. Android动态调试--jeb调试apk
  9. Android实现高斯模糊(也叫毛玻璃效果)

随机推荐

  1. Android:Galaxy Nexus升级到4.1.2,并root(设
  2. Android(安卓)Shape的使用
  3. Android学习之线性布局管理器
  4. 浅析Android中的消息机制
  5. android 获取图片
  6. android灵活布局
  7. android典型代码系列(一)------android调
  8. Android(安卓)多媒体扫描过程(Android(安
  9. Android中获取短信的内容
  10. Android(安卓)微信SDK分享功能中的最全过