运用Handler.post()方法进行数据更新跟用Message对比(18)
生活随笔
收集整理的這篇文章主要介紹了
运用Handler.post()方法进行数据更新跟用Message对比(18)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
2019獨角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
//運用Handler.post() public?class?SecondActivity?extends?Activity?{private?String?url?=?"http://litchiapi.jstv.com/Attachs/Top/11949/e650e0201de541d2ba91dca202b0fcfe.jpg";private?ImageView?imageview;private?Handler?handler?=?new?Handler();//?注意:這里用handler.post()方法,所以不用實現(xiàn)里面的方法//?這種方法是不用到Message(),只是利用handler.post(Runable)方法來更新,里面其實放的是代碼,注意:存放的代碼它屬于主線程;//?注意:這里用handler.post()方法,所以不用實現(xiàn)里面的方法@Overrideprotected?void?onCreate(Bundle?savedInstanceState)?{super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//?這個SecondActivity要用主布局記得在功能清單文件改配置imageview?=?(ImageView)?this.findViewById(R.id.imageview);//?子線程,下載操作new?Thread(new?Runnable()?{@Overridepublic?void?run()?{Log.i("MainActivity",?"first"+?Thread.currentThread().getName());byte[]?data?=?loadImage(url);final?Bitmap?bitmap?=?BitmapFactory.decodeByteArray(data,?0,data.length);handler.post(new?Runnable()?{//?注意:這里是主線程,只是一個代碼,因為它沒有啟動所以不算線程@Overridepublic?void?run()?{Log.i("MainActivity",?"second"+?Thread.currentThread().getName());imageview.setImageBitmap(bitmap);}});}}).start();}public?byte[]?loadImage(String?url)?{try?{HttpClient?client?=?new?DefaultHttpClient();HttpGet?get?=?new?HttpGet(url);HttpResponse?response?=?client.execute(get);if?(response.getStatusLine().getStatusCode()?==?200)?{return?EntityUtils.toByteArray(response.getEntity());}}?catch?(ClientProtocolException?e)?{e.printStackTrace();}?catch?(IOException?e)?{e.printStackTrace();}return?null;} }//使用handler.sendEmptyMessage()通知更新 //這是第一種方法:利用handler.sendEmptyMessage()通知更新 //另外一種方法是不用到Message(),只是利用handler.post(Runable)方法來更新,里面其實放的是代碼,注意:存放的代碼它屬于主線程;public?class?MainActivity?extends?Activity?{private?String?url?=?"http://litchiapi.jstv.com/Attachs/Top/11949/e650e0201de541d2ba91dca202b0fcfe.jpg";private?ImageView?imageview;private?ProgressDialog?dialog;Handler?handler?=?new?Handler()?{public?void?handleMessage(android.os.Message?msg)?{switch?(msg.what)?{case?0:dialog.show();break;case?1:imageview.setImageBitmap((Bitmap)?msg.obj);break;case?2:dialog.dismiss();break;}};};@Overrideprotected?void?onCreate(Bundle?savedInstanceState)?{super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);imageview?=?(ImageView)?this.findViewById(R.id.imageview);dialog?=?new?ProgressDialog(this);dialog.setTitle("提示");dialog.setIcon(R.drawable.ic_launcher);dialog.setMessage("正在下載數(shù)據(jù)!!!");new?Thread(new?Runnable()?{@Overridepublic?void?run()?{//?1.顯示對話框handler.sendEmptyMessage(0);//?通知主線程顯示對話框//?2.下載數(shù)據(jù)byte[]?data?=?loadImage(url);Bitmap?bitmap?=?BitmapFactory.decodeByteArray(data,?0,data.length);Message?message?=?Message.obtain();message.what?=?1;message.obj?=?bitmap;handler.sendMessage(message);//?發(fā)送主線程設(shè)置圖片顯示//?3.關(guān)閉對話框handler.sendEmptyMessage(2);}}).start();}public?byte[]?loadImage(String?url)?{try?{HttpClient?client?=?new?DefaultHttpClient();HttpGet?get?=?new?HttpGet(url);HttpResponse?response?=?client.execute(get);if?(response.getStatusLine().getStatusCode()?==?200)?{return?EntityUtils.toByteArray(response.getEntity());}}?catch?(ClientProtocolException?e)?{e.printStackTrace();}?catch?(IOException?e)?{e.printStackTrace();}return?null;} }轉(zhuǎn)載于:https://my.oschina.net/u/2541146/blog/625268
總結(jié)
以上是生活随笔為你收集整理的运用Handler.post()方法进行数据更新跟用Message对比(18)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《github一天一道算法题》:分治法求
- 下一篇: 一分钟了解阿里云产品:容器服务概述