安卓开发中Handler的使用 实例讲解Android多线程应用开发中Handler的使用
张钊 人气:0想了解实例讲解Android多线程应用开发中Handler的使用的相关内容吗,张钊在本文为您仔细讲解安卓开发中Handler的使用的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:安卓,Handler,Android,下面大家一起来学习吧。
其实可以理解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(); } }
效果:
加载全部内容