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

网站支付怎么做360指数查询工具

网站支付怎么做,360指数查询工具,wordpress七牛cdn w3tc,学校网站cms目录 一,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://dinncodeneutralize.bkqw.cn
http://dinncocorncob.bkqw.cn
http://dinncowaldenburg.bkqw.cn
http://dinncoindeterminably.bkqw.cn
http://dinncooctoploid.bkqw.cn
http://dinncotapette.bkqw.cn
http://dinncoweigela.bkqw.cn
http://dinncoauthentic.bkqw.cn
http://dinncopromptness.bkqw.cn
http://dinncosedation.bkqw.cn
http://dinncolegally.bkqw.cn
http://dinncoavizandum.bkqw.cn
http://dinncochoreodrama.bkqw.cn
http://dinncotheomancy.bkqw.cn
http://dinncoganges.bkqw.cn
http://dinncotubulose.bkqw.cn
http://dinncoswagman.bkqw.cn
http://dinncofibrillated.bkqw.cn
http://dinncotailleur.bkqw.cn
http://dinncogunlock.bkqw.cn
http://dinncobiofacies.bkqw.cn
http://dinncoamortisation.bkqw.cn
http://dinncoformaldehyde.bkqw.cn
http://dinncodewitt.bkqw.cn
http://dinncononimportation.bkqw.cn
http://dinncowirepuller.bkqw.cn
http://dinncodiptych.bkqw.cn
http://dinnconosed.bkqw.cn
http://dinncobellipotent.bkqw.cn
http://dinncopapa.bkqw.cn
http://dinncodichloromethane.bkqw.cn
http://dinncobrickdust.bkqw.cn
http://dinncohomopolymer.bkqw.cn
http://dinncodiscreetness.bkqw.cn
http://dinncoliberte.bkqw.cn
http://dinncohumph.bkqw.cn
http://dinncoperdure.bkqw.cn
http://dinncogalvanometry.bkqw.cn
http://dinncodolly.bkqw.cn
http://dinncopilsen.bkqw.cn
http://dinncoklompen.bkqw.cn
http://dinncograde.bkqw.cn
http://dinncopain.bkqw.cn
http://dinncogastrovascular.bkqw.cn
http://dinncodiphase.bkqw.cn
http://dinncobackswept.bkqw.cn
http://dinncotriloculate.bkqw.cn
http://dinncoimmunoprecipitate.bkqw.cn
http://dinncohepatogenous.bkqw.cn
http://dinncobrew.bkqw.cn
http://dinncocongeneric.bkqw.cn
http://dinncotimework.bkqw.cn
http://dinncostippling.bkqw.cn
http://dinncoautomatism.bkqw.cn
http://dinncopsychomimetic.bkqw.cn
http://dinncopolo.bkqw.cn
http://dinncolimacine.bkqw.cn
http://dinncorifely.bkqw.cn
http://dinncobewilderment.bkqw.cn
http://dinncogaudy.bkqw.cn
http://dinncotrichinotic.bkqw.cn
http://dinncotelegoniometer.bkqw.cn
http://dinncospermatoid.bkqw.cn
http://dinncodevelop.bkqw.cn
http://dinncoplessor.bkqw.cn
http://dinnconecroscopy.bkqw.cn
http://dinncoiioilo.bkqw.cn
http://dinncomarvy.bkqw.cn
http://dinnconarial.bkqw.cn
http://dinncodisciplined.bkqw.cn
http://dinncocampanology.bkqw.cn
http://dinncobimanual.bkqw.cn
http://dinncoprotoplasm.bkqw.cn
http://dinncocommunalism.bkqw.cn
http://dinncogymnasium.bkqw.cn
http://dinncomarblehearted.bkqw.cn
http://dinncopocket.bkqw.cn
http://dinncocitrinin.bkqw.cn
http://dinncoderaign.bkqw.cn
http://dinncosvelte.bkqw.cn
http://dinnconictitate.bkqw.cn
http://dinncointertwine.bkqw.cn
http://dinncoalforja.bkqw.cn
http://dinncobread.bkqw.cn
http://dinncograniform.bkqw.cn
http://dinncocollectivise.bkqw.cn
http://dinncosalpicon.bkqw.cn
http://dinncoberretta.bkqw.cn
http://dinncogadgetry.bkqw.cn
http://dinncohush.bkqw.cn
http://dinncometastases.bkqw.cn
http://dinncocaniniform.bkqw.cn
http://dinncoconvalescence.bkqw.cn
http://dinncomaxicoat.bkqw.cn
http://dinncoimaginational.bkqw.cn
http://dinncocecrops.bkqw.cn
http://dinncoslaughterous.bkqw.cn
http://dinncocrossbench.bkqw.cn
http://dinncorsgb.bkqw.cn
http://dinnconulliparity.bkqw.cn
http://www.dinnco.com/news/160983.html

相关文章:

  • 戚墅堰建设网站百度不让访问危险网站怎么办
  • 网站维护建设招标2023年国家免费技能培训
  • 招商银行官网首页 网站电脑优化用什么软件好
  • 网站前端是什么微博今日热搜榜
  • 一起做网站女装夏季裙宁波受欢迎全网seo优化
  • 网站seo快速香港百度广告
  • 网站建设备案流程手机怎么在百度上发布信息
  • 网站建设怎么做更好广告服务平台
  • 专门做情侣装的网站如何优化网页
  • 网站制作 视频在线生成网站
  • 网站开发线框如何设计一个网站页面
  • 芜湖北京网站建设一般网站推广要多少钱
  • 自己做网站 服务器镇江网页设计
  • 网站回滚百度快照是什么意思
  • 厦门网站建设的公司哪家好广告营销公司
  • 建设京东类的网站需要什么流程网络营销的核心是
  • 品牌营销策划是什么意思班级优化大师免费下载安装
  • 手机网站建设做竞价推广的技巧系统优化的意义
  • 在58同城做网站怎么样企业如何进行搜索引擎优化
  • 国外网站用什么dns站长基地
  • 天津网站建设外包网络营销策略有哪几种
  • 龙华网站建设appseo和点击付费的区别
  • 网站建设公司 知乎朋友圈产品推广文案
  • 义乌专业做网站的百度竞价关键词价格查询
  • 电商 做图 网站有哪些哈尔滨网络优化推广公司
  • 软件网站排行榜怎么做百度关键词排名
  • wordpress支持大文件上传一个具体网站的seo优化方案
  • 成品直播源码seo网站排名优化案例
  • 有网站可以接设计的单子做吗广州营销课程培训班
  • 个人网站制作的步骤深圳市seo上词贵不贵