我们专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

Android多线程,让耗时的操作去后台运行吧

在android程序中,会有一些耗时的操作,比如从网上抓取图片,下载文件,批量更新数据库等,这些操作对于手机而言会需要很长的时间,而应用程序界面又不能等到这些操作完成后再显示,所以要让界面各这些耗时的操作并行处理,用多线程可以解决这个问题。当然还有其它解决方案,比如用Service.

创新互联-云计算及IDC服务提供商,涵盖公有云、IDC机房租用、成都西云数据中心、等保安全、私有云建设等企业级互联网基础服务,服务电话:028-86922220

我们先作一个例子吧,大概是这样的:有一个列表,每行显示的一个图片,图片是存放在网上的。如果不用多线程,也是可以的,但是要等到所有图片下载完了才能展示出来。这种方式对用户体验很不友好,所以我们采用多线程的方式,对每一个图片开启一个线程,当其下载完数据后,在主线程中显示出来。

主Activity

 
 
 
  1. public class TestListActivity extends ListActivity { 
  2. private ImageListAdapter imageListAdapter = null; 
  3. /** Called when the activity is first created. */ 
  4. @Override 
  5. public void onCreate(Bundle savedInstanceState) { 
  6. super.onCreate(savedInstanceState); 
  7. setContentView(R.layout.imagelist); 
  8. String[] images = {"http://image.baidu.com/image1.jpg","http://image.baidu.com/image2.jpg"}; 
  9. imageListAdapter = new ImageListAdapter(getApplicationContext(), images); 
  10. setListAdapter(imageListAdapter); 
  11. } 
  12. } 

适配器

 
 
 
  1. import java.util.ArrayList; 
  2. import java.util.List; 
  3. import android.content.Context; 
  4. import android.graphics.Bitmap; 
  5. import android.os.Handler; 
  6. import android.os.Message; 
  7. import android.view.View; 
  8. import android.view.ViewGroup; 
  9. import android.widget.BaseAdapter; 
  10. import android.widget.ImageView; 
  11. import android.widget.TextView; 
  12.  
  13. public class ImageListAdapter extends BaseAdapter { 
  14. private Context context; 
  15. private String[] myImages = null; 
  16. public ImageListAdapter(Context context, String[] myImages){ 
  17. this.context = context; 
  18. this.myImages = myImages; 
  19. } 
  20. @Override 
  21. public int getCount() { 
  22. if(myImages == null){ 
  23. return 0; 
  24. } 
  25. return myImages.length; 
  26. } 
  27. @Override 
  28. public String getItem(int position) { 
  29. if(position < 0 || myImages == null || position>myImages.length){ 
  30. return null; 
  31. } 
  32. return myImages[position]; 
  33. } 
  34. @Override 
  35. public long getItemId(int position) { 
  36. return position; 
  37. } 
  38. @Override 
  39. public View getView(int position, View convertView, ViewGroup parent) { 
  40. View item = null; 
  41. if(convertView != null){ 
  42. item = convertView; 
  43. } else { 
  44. item = View.inflate(context, R.layout.image_item, null); 
  45. } 
  46. final ImageView imageView = (ImageView)item.findViewById(R.id.image); 
  47. final String image = getItem(position); 
  48. if(image == null){ 
  49. return item; 
  50. } 
  51. //对每个图片地址创建一个线程, 
  52. new Thread(){ 
  53. public void run(){ 
  54. Message msg = new Message(); 
  55. msg.what = 0; 
  56. //获得图片的Bitmap对象。getBitmap省略了,就是从网上通过http下载图片然后转化成一个Bitmap 
  57. Bitmap bitmap = getBitmap(image); 
  58. List list = new ArrayList();//将bitmap和imageView包装成一个List传到线程外 
  59. list.add(bitmap); 
  60. list.add(imageView); 
  61. msg.obj = list; 
  62. handler.sendMessage(msg); 
  63. } 
  64. }.start(); 
  65. return item; 
  66. } 
  67. private Handler handler = new Handler() { 
  68. @Override 
  69. public void handleMessage(Message msg) { 
  70. switch (msg.what) { 
  71. case 0://接到从线程内传来的图片bitmap和imageView. 
  72. //这里只是将bitmap传到imageView中就行了。只所以不在线程中做是考虑到线程的安全性。 
  73. List list = (List)msg.obj; 
  74. Bitmap bitmap = (Bitmap)list.get(0); 
  75. ImageView iv = (ImageView)list.get(1); 
  76. iv.setImageBitmap(bitmap); 
  77. break; 
  78. default: 
  79. super.handleMessage(msg); 
  80. } 
  81. } 
  82. }; 
  83. }

布局xml
imagelist.xml

 
 
 
  1. android:orientation="vertical" 
  2. android:layout_width="fill_parent" 
  3. android:layout_height="fill_parent" 
  4. android:padding = "10px" 
  5. android:gravity="center_horizontal" 
  6. android:background="#ffffff"> 
  7. android:layout_width="fill_parent" 
  8. android:layout_height="fill_parent" /> 
  9. android:layout_width="wrap_content" 
  10. android:layout_height="wrap_content" /> 
  11. image_item.xml 
  12. android:layout_width="fill_parent" 
  13. android:layout_height="wrap_content"> 
  14. android:id="@+id/image" 
  15. android:layout_width="70px" 
  16. android:layout_height="50px" 
  17. android:paddingRight="5px"/> 

转载请标明出处:3G Study :http://blog.3gstdy.com/archives/27


网站题目:Android多线程,让耗时的操作去后台运行吧
文章分享:http://www.swkodf.com/article/dppojid.html

其他资讯