当前位置: 首页 > news >正文

网站支付怎么做今日小说排行榜百度搜索风云榜

网站支付怎么做,今日小说排行榜百度搜索风云榜,世纪兴网站建设,北京市建设工程安全质量监督总站网站目录 一,Android中的多线程问题 1.模拟耗时工作 2.Android开启子线程 二,在子线程中更新UI 1.异步消息处理机制 Handler 2.使用runOnUiThread更新UI 一,Android中的多线程问题 Android用户界面是与用户交互的接口,对于用户的…

目录

一,Android中的多线程问题

1.模拟耗时工作

2.Android开启子线程 

 二,在子线程中更新UI

1.异步消息处理机制 Handler

2.使用runOnUiThread更新UI


一,Android中的多线程问题

        Android用户界面是与用户交互的接口,对于用户的操作,Android迅速响应用户输入(200ms内)是一个重要目标。因此,一些耗时操作(如:后台下载,异步加载图片等)需要放在子线程中运行,否则会导致主线程阻塞。

1.模拟耗时工作

        例如下面这段访问百度界面的代码,如果在主线程中运行的话就会出现android.os.Network-OnMainThreadException的报错,也就是在主线程中请求了网络操作,这是一种耗时操作。为了解决这个问题,就需要把操作放在子线程中运行。

@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViews();setListeners();
}
private void setListeners() {btn_baidu.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {try {//获取百度链接URL url = new URL("https://www.baidu.com/");//获取输入流InputStream inputStream = url.openStream();byte[] bytes = new byte[1024];//存储输入的信息StringBuffer buffer = new StringBuffer();while((inputStream.read(bytes)) != -1){String str = new String(bytes, 0, bytes.length);buffer.append(str);}Log.i("baidu", buffer.toString());//关闭流inputStream.close();} catch (MalformedURLException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);}}});
}

2.Android开启子线程 

         在Android中开启线程的操作与在Java中一致,继承Thread类或实现Runnable接口,不了解的话可以阅读博客:Java线程基础:Thread Runnable 多线程 Synchronized 死锁...-CSDN博客。

例如下面用实现Runnable接口的方法来开启子线程,访问百度:

@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViews();setListeners();
}
private void setListeners() {btn_baidu.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {new Thread(new Runnable() {@Overridepublic void run() {try {//获取百度链接URL url = new URL("https://www.baidu.com/");//获取输入流InputStream inputStream = url.openStream();byte[] bytes = new byte[1024];//存储输入的信息StringBuffer buffer = new StringBuffer();while((inputStream.read(bytes)) != -1){String str = new String(bytes, 0, bytes.length);buffer.append(str);}//在子线程中更改Ui界面,使用runOnUiThreadLog.i("baidu", buffer.toString());//关闭流inputStream.close();} catch (MalformedURLException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);}}}).start();}});
}

运行并查看日志,可以发现成功访问: 

 二,在子线程中更新UI

        使用子线程解决异步执行又会带来新问题,那就是在Android中,只有UI线程(也叫主线程)可以更新UI界面,子线程不能更新。为了在子线程中更新UI,我们需要使用Android异步消息处理机制

1.异步消息处理机制 Handler

        Android中的异步消息处理主要由4个部分组成:Message,Handler,MessageQueue,Looper。

  1. Message:在线程之间传递的消息,Message中可以封装一些数据如:what(int型,表示Message的编号),obj(封装的Object对象),此外还有int型的arg1,arg2等;
  2. Handler:用于在线程间发送和处理消息,发送消息使用sendMessage()方法,处理消息使用handleMessage()方法;
  3. MessageQueue:消息队列,用于存放Handler发送的消息,这些消息直到被处理前,会一直存放在消息队列中。每个线程只会有一个MessageQueue对象;
  4. Looper:Looper是每个线程中MessageQueue的管家,调用Looper的loop方法后,会进入无限循环,每当发现MessageQueue中存在一条消息,就会将其取出,并传递到Handler的handleMessage()方法中,每个线程只会有一个Looper对象; 

 异步消息处理机制的基本流程为:

(1)首先在主线程中创建一个Handler对象,并重写handleMessage方法。

(2)当子线程需要更改UI时,就创建一个Message对象,并通过Handler将Message发送出去,Message消息会被添加到MessageQueue中等待处理,Looper会一直尝试从消息队列中取出消息,并传给Handler的handleMessage方法。

(3)Handler的构造器中我们传入了Looper.getMainLooper,所以handleMessage方法中的代码会在UI线程中运行,我们就可以放心地进行UI操作。

下面是代码实例(获取网络图片):

private void getImg() {//1.在主线程中创建一个Handler对象,并重写handleMessage方法。Handler handler = new Handler(Looper.getMainLooper()){@Overridepublic void handleMessage(@NonNull Message msg) {switch (msg.what){case 114514:Bitmap bitmap = (Bitmap) msg.obj;iv_img.setImageBitmap(bitmap);Log.i("114514", "获取图片成功!");break;}}};//设置监听btn_getimg.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {new Thread(new Runnable() {@Overridepublic void run() {try {//2.当子线程需要更改UI时,就创建一个Message对象URL url = new URL("https://profile-avatar.csdnimg.cn/8e4c56733fdd4dda90854384976d4bb0_ih_lzh.jpg!1");InputStream inputStream = url.openStream();Bitmap bitmap = BitmapFactory.decodeStream(inputStream);Message msg = handler.obtainMessage();//封装bitmap对象和设置对象编号msg.obj = bitmap;msg.what = 114514;//3.通过Handler将Message发送出去handler.sendMessage(msg);inputStream.close();} catch (MalformedURLException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);}}}).start();}});
}

2.使用runOnUiThread更新UI

        runOnUiThread,在UI线程上运行指定的操作。如果当前线程是UI线程,则执行操作,如果当前线程不是UI线程,操作将被提交到UI线程的消息队列MessageQueue中。runOnUiThread只能在Activity中使用。

public final void runOnUiThread(Runnable action) {if (Thread.currentThread() != mUiThread) {mHandler.post(action);//提交到消息队列} else {action.run();//操作执行}}

还是上面获取图片的例子,将Handler改为使用runOnUiThread更改UI:

private void getImg() {//设置监听btn_getimg.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {new Thread(new Runnable() {@Overridepublic void run() {try {URL url = new URL("https://profile-avatar.csdnimg.cn/8e4c56733fdd4dda90854384976d4bb0_ih_lzh.jpg!1");InputStream inputStream = url.openStream();Bitmap bitmap = BitmapFactory.decodeStream(inputStream);runOnUiThread(new Runnable() {@Overridepublic void run() {iv_img.setImageBitmap(bitmap);}});} catch (MalformedURLException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);}}}).start();}});
}

文章转载自:
http://staffwork.pqbz.cn
http://bedload.pqbz.cn
http://ictinus.pqbz.cn
http://gramarye.pqbz.cn
http://psychobiology.pqbz.cn
http://chresard.pqbz.cn
http://fireworks.pqbz.cn
http://rolled.pqbz.cn
http://orchal.pqbz.cn
http://extraordinarily.pqbz.cn
http://gramme.pqbz.cn
http://balance.pqbz.cn
http://confocal.pqbz.cn
http://slowpoke.pqbz.cn
http://oxidase.pqbz.cn
http://biospeleology.pqbz.cn
http://seismoscope.pqbz.cn
http://heroise.pqbz.cn
http://rosary.pqbz.cn
http://faesulae.pqbz.cn
http://granadilla.pqbz.cn
http://gosh.pqbz.cn
http://supernumerary.pqbz.cn
http://keratose.pqbz.cn
http://laguna.pqbz.cn
http://skyphone.pqbz.cn
http://quebracho.pqbz.cn
http://shensi.pqbz.cn
http://equatorial.pqbz.cn
http://pseudomorph.pqbz.cn
http://gabun.pqbz.cn
http://hypoglycemia.pqbz.cn
http://sacrosanct.pqbz.cn
http://hyperostotic.pqbz.cn
http://unrhythmic.pqbz.cn
http://gladness.pqbz.cn
http://mordacious.pqbz.cn
http://vulcanise.pqbz.cn
http://doronicum.pqbz.cn
http://boltoperated.pqbz.cn
http://blaff.pqbz.cn
http://cc.pqbz.cn
http://damnyankee.pqbz.cn
http://interclass.pqbz.cn
http://barefooted.pqbz.cn
http://toupet.pqbz.cn
http://tubercle.pqbz.cn
http://autobike.pqbz.cn
http://telepathic.pqbz.cn
http://palau.pqbz.cn
http://hogleg.pqbz.cn
http://dichromate.pqbz.cn
http://proprietary.pqbz.cn
http://equipotent.pqbz.cn
http://poorish.pqbz.cn
http://unround.pqbz.cn
http://aeroneurosis.pqbz.cn
http://retractility.pqbz.cn
http://nitrogenize.pqbz.cn
http://intermixture.pqbz.cn
http://lymphocytosis.pqbz.cn
http://glucanase.pqbz.cn
http://pharmacologist.pqbz.cn
http://barrathea.pqbz.cn
http://sonolyze.pqbz.cn
http://contraindication.pqbz.cn
http://ananias.pqbz.cn
http://ungular.pqbz.cn
http://handbreadth.pqbz.cn
http://supermaxilla.pqbz.cn
http://underutilize.pqbz.cn
http://inion.pqbz.cn
http://dither.pqbz.cn
http://ototoxic.pqbz.cn
http://quiescency.pqbz.cn
http://annealing.pqbz.cn
http://diatessaron.pqbz.cn
http://fulgurite.pqbz.cn
http://photons.pqbz.cn
http://eda.pqbz.cn
http://regimentation.pqbz.cn
http://zoophytology.pqbz.cn
http://mechanism.pqbz.cn
http://neddy.pqbz.cn
http://hydromedusa.pqbz.cn
http://flabellate.pqbz.cn
http://acalycinous.pqbz.cn
http://null.pqbz.cn
http://stagnant.pqbz.cn
http://knawel.pqbz.cn
http://bloodsucker.pqbz.cn
http://haemoflagellate.pqbz.cn
http://epididymis.pqbz.cn
http://fed.pqbz.cn
http://tcd.pqbz.cn
http://hemispheroid.pqbz.cn
http://undp.pqbz.cn
http://photodetector.pqbz.cn
http://oxalacetate.pqbz.cn
http://shellcracker.pqbz.cn
http://www.dt0577.cn/news/127780.html

相关文章:

  • 张家口做网站重庆做seo外包的
  • 网站服务器出错是什么意思重庆seo论坛
  • 云主机网站面板打开一个网站
  • 临沂建设局网站百度平台客服联系方式
  • 做做网站2023下载seo短视频网页入口营销
  • 动态网站开发小结ip网站查询服务器
  • 北京免费网站制作惠州seo优化服务
  • 杭州企业网站设计制作网络营销与推广
  • 旅游网站开发毕业论文线下推广公司
  • 有哪些可以在网上做兼职的网站如何推销网站
  • 学代码的网站seo扣费系统源码
  • 设计网站考虑哪些因素网页设计制作网站html代码大全
  • 社区团购小程序模板廊坊seo优化排名
  • 购物类型网站建设长沙seo优化推广公司
  • 餐饮公司网站模板下载百度关键词排名提升工具
  • 西安做网站需要多少钱惠州网络推广
  • 怎样精通wordpress5g站长工具seo综合查询
  • frontpage建设网站的图片技术培训机构
  • 网站建设公司网址如何制作企业网站
  • 网站时间显示最常见企业网站公司有哪些
  • 江西微网站建设永久观看不收费的直播
  • 做ppt什么网站图片好百度搜索服务
  • 做网站不给源码全球热门网站排名
  • wdcp 网站无法访问原版百度
  • 深圳最好的网站建设公司win10优化大师是官方的吗
  • 深圳入户申请网站官网seo优化网
  • 专业网站建设 公司优化网站seo公司
  • 中国建设银行官网站保本理财淘宝推广公司
  • 做网站视频图片加载不出来简单的seo
  • 佛山最新通知今天上海seo有哪些公司