实例讲解Android多线程应用开发中Handler的使用
2019/7/7 19:59:30
本文主要是介绍实例讲解Android多线程应用开发中Handler的使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
其实可以理解Handler为主线程和另外的线程之间进行数据更新的东东,并且Handler在主线程中,并在Handler直接调用线程的run方法
package com.Handler02; import android.app.Activity; import android.os.Bundle; import android.os.Handler; public class Handler02Activity extends Activity { /** Called when the activity is first created. */ private Handler handler=new Handler(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); handler.post(thread1); setContentView(R.layout.main); System.out.println("================Main==============="+Thread.currentThread().getId()); System.out.println("============Main========="+Thread.currentThread().getName()); } Runnable thread1=new Runnable() { @Override public void run() { System.out.println("======thread1==============="+Thread.currentThread().getId()); System.out.println("========thread1============"+Thread.currentThread().getName()); try { Thread.sleep(10000); } catch (Exception e) { // TODO: handle exception } } }; }
结果是主线程等待10s后才显示出来,并且线程ID
package com.Handler4; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.TextView; public class Handler4Activity extends Activity { /** Called when the activity is first created. */ private TextView textView; private MyHandler myHandler=new MyHandler(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textView=(TextView)this.findViewById(R.id.textView1); } class MyHandler extends Handler{ //从消息队列中取出并handleMessage处理消息 @Override public void handleMessage(Message msg) { textView.setText((String)msg.obj); } } public void download(View view){ Thread thread=new Thread(new Runnable() { @Override public void run() { try { String s="zhangzhao"; Thread.sleep(5000); //数据发送出来 //textView.setText(s); Message message=Message.obtain();//最好不要new,这个obtain会节省资源 message.obj=s; myHandler.sendMessage(message);//没有指定looper那么就会发送给主线程中的looper } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); thread.start();//会出错原因是在非UI线程里面直接操作UI //主线程维护了一个消息对列,当其他的线程有数据需要传递给主线程的时候,你就把数据封装成一个Message对象,然后放在主线程的 //消息队列中去,对应消息的处理由Looper,子线程通过Handler把下载完的数据封装到Message里面,然后把消息取出来交给Handler进行处理 //通过Handle和Message实现两个线程之间达到共享数据 } }
package com.Handler5; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; public class Handler5Activity extends Activity { /** Called when the activity is first created. */ private ProgressBar progressBar; private Button button; private TextView textView; private MyHandler myHandler=new MyHandler(); int i=0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); progressBar = (ProgressBar)this.findViewById(R.id.progressBar1); textView=(TextView)this.findViewById(R.id.textView1); } class MyHandler extends Handler{ @Override public void handleMessage(Message msg) { int pos =(Integer)msg.obj; progressBar.setProgress(pos); textView.setText(pos+"%"); } } public void downLoad(View view){ new Thread(new Runnable() { @Override public void run() { while(i<=100){ try { Thread.sleep(300); i+=10; Message message=Message.obtain(); message.obj=i; myHandler.sendMessage(message); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }).start(); } }
效果:
这篇关于实例讲解Android多线程应用开发中Handler的使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-01-18android.permission.read_media_video
- 2024-01-18android_getaddrinfo failed eai_nodata
- 2024-01-18androidmo
- 2024-01-15Android下三种离屏渲染技术
- 2024-01-09Android 蓝牙使用
- 2024-01-06Android对接华为AI - 文本识别
- 2023-11-15代码安全之代码混淆及加固(Android)
- 2023-11-10简述Android语音播报TTS
- 2023-11-06Android WiFi工具类
- 2023-07-22Android开发未来的出路