Handler小例子(非常简单的handler例子,适合初学者,高手勿喷)

例子一:

使用Handler循环打印

package djx.android; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class HandlerTest extends Activity { private Button button_begin=null; private Button button_end=null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button_begin=(Button)findViewById(R.id.button_begin); button_begin.setOnClickListener(new beginOnClickListener()); button_end=(Button)findViewById(R.id.button_end); button_end.setOnClickListener(new endOnClickListener()); } class beginOnClickListener implements OnClickListener{ @Override public void onClick(View arg0) { handler.post(runnable); } } class endOnClickListener implements OnClickListener{ @Override public void onClick(View arg0) { handler.removeCallbacks(runnable); } } Handler handler=new Handler(); Runnable runnable=new Runnable(){ @Override public void run() { System.out.println("thread Update......"); handler.postDelayed(runnable, 3000); } }; }

例子二:

handler更新progressBar

执行过程分析:

点击按钮->progressBar变为可见->handler将runnable对象放入队列等待执行,由于当前队列中仅有这一个对象,故而立即得到执行->执行Runnable,控制台打印Thread is Beging!->i自增10,并赋值给msg的变量arg1->当前线程休眠1s->handler.sendMessage(msg)即handler向消息队列发送msg,在handler的 handleMessage(Message msg)方法中会得到msg对象,progressBar设置当前值。->如果i还未达到最大值100的话继续handler将runnab对象放入队列,否则移出队列结束。

package djx.handler; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; public class ProgressBarHandler extends Activity { private Button button_begin=null; private ProgressBar pb=null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button_begin=(Button)findViewById(R.id.button_begin); button_begin.setOnClickListener(new beginOnClickListener()); pb=(ProgressBar)findViewById(R.id.progressBar); pb.setMax(100); } class beginOnClickListener implements OnClickListener{ @Override public void onClick(View v) { pb.setVisibility(View.VISIBLE); handler.post(runnable); } } Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); pb.setProgress(msg.arg1); if(msg.arg1==100){ handler.removeCallbacks(runnable); } else handler.post(runnable); } }; Runnable runnable=new Runnable(){ int i=0; @Override public void run() { System.out.println("Thread is Beging!"); i=i+10; Message msg=handler.obtainMessage(); msg.arg1=i; try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } handler.sendMessage(msg); System.out.println("**********:"+i); } }; }

你可能感兴趣的:(Handler小例子(非常简单的handler例子,适合初学者,高手勿喷))