Thread&Handle 施用
Thread&Handle 使用
Thread&Handle
本次以进度提示为例 讲解Thread&Handle的使用
[前提]
1. Thread * 启动:start() * 停止:stop() * 定制化: 填充 void run() 2. Handler * 使用: sendMessage() / sendEmptyMessage() * 接受&处理:定制化 void handleMessage(Message message)
典型用法:
public class ThreadUsage extends Activity { public final static int MESSAGE_SAMPLE = 19; TextView text; ProgressBar pBar; Handler sHandler; Thread sThread; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); text = (TextView)findViewById(R.id.text); sThread = new Thread(){ public void run(){ while(true){ try { Thread.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Message m = new Message(); m.what = MESSAGE_SAMPLE; sHandler.sendMessage(m); } } }; sThread.start(); sHandler = new Handler(){ public void handleMessage(Message msg) { int i = msg.what; if(i == MESSAGE_SAMPLE){ String s = text.getText().toString(); text.setText(s+"."); } else { //un-support message type } } }; } }
[代码 步骤]
1. 定义 running.xml 布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text" android:paddingLeft="10dip" android:text="is running" /> <TextView android:layout_width="100dip" android:layout_height="wrap_content" android:id="@+id/progress" android:text="................." /> <Button android:layout_width="100dip" android:layout_height="wrap_content" android:id="@+id/buttonOK" android:text="OK!" /> </LinearLayout>
2. 定义 RunningDialog 构造函数 并根据参数 创建目标AlertDialog
public RunningDialog(Activity a,String title,Drawable d){ activity = a; initialView(activity); aBuilder = new AlertDialog.Builder(activity); aBuilder.setTitle(title).setIcon(d).setView(runningView); aDialog = aBuilder.create(); installUpdate(); }
private void initialView(Activity a){ runningView = a.getLayoutInflater().inflate(R.layout.running, null); progress = (TextView)runningView.findViewById(R.id.progress); btnOK = (Button)runningView.findViewById(R.id.buttonOK); btnOK.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub end(); } }); }
3. 定义Thread&Handle 定时刷新TextView显示
private void installUpdate(){ updateHandler = new Handler(){ public void handleMessage(Message msg) { int i = msg.what; if(i == MESSAGE_TOGO){ updateProgress(); } else { //un-support message } } }; updateThread = new Thread(){ public void run(){ while(true){ try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Message m = new Message(); m.what = MESSAGE_TOGO; updateHandler.sendMessage(m); } } }; } private void updateProgress(){ count++; String string = progress.getText().toString(); if(count >= 10){ progress.setText("."); count = 0; } else { progress.setText(string+"."); } progress.setTextSize(20); }
4. 定义2个函数 分别用于显示/取消该AlertDialog
public void start(){ if(isResume){ updateThread.resume(); } else { updateThread.start(); isResume = true; } aDialog.show(); } public void end(){ updateThread.stop(); aDialog.dismiss(); updateThread.resume(); }
5. 如何使用:
public class RunningDialogTest extends Activity { RunningDialog rDialog; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Drawable d = this.getResources().getDrawable(R.drawable.icon); rDialog = new RunningDialog(this,"HelloRunning!",d); findViewById(R.id.button).setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub rDialog.start(); } }); } }
6. emulator 运行截图:
done! ~~~