日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

运用Handler.post()方法进行数据更新跟用Message对比(18)

發布時間:2025/5/22 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 运用Handler.post()方法进行数据更新跟用Message对比(18) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

//運用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()方法,所以不用實現里面的方法//?這種方法是不用到Message(),只是利用handler.post(Runable)方法來更新,里面其實放的是代碼,注意:存放的代碼它屬于主線程;//?注意:這里用handler.post()方法,所以不用實現里面的方法@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("正在下載數據!!!");new?Thread(new?Runnable()?{@Overridepublic?void?run()?{//?1.顯示對話框handler.sendEmptyMessage(0);//?通知主線程顯示對話框//?2.下載數據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);//?發送主線程設置圖片顯示//?3.關閉對話框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;} }


轉載于:https://my.oschina.net/u/2541146/blog/625268

總結

以上是生活随笔為你收集整理的运用Handler.post()方法进行数据更新跟用Message对比(18)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。