2012年5月20日 星期日

Handler 01-- MainThread


MainHandler






package com.cq.practice.handler01;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.res.Resources;
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;


public class Cq_Handler01Activity extends Activity {
    /** Called when the activity is first created. */

private Button mButton;
private ProgressDialog mProgressDialog;

private Handler handler = new Handler(){

@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
mProgressDialog.dismiss(); //close
}

};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
        Resources res = this.getResources();
        mButton = (Button) findViewById(R.id.button1);
        mButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
processThread();
}
});
     
    }
 
 
 
    private void processThread() {
    mProgressDialog = ProgressDialog.show(this, "文件下載", "正在下載...");
   
    new Thread() {
   
   
    @Override
public void run() {
// TODO Auto-generated method stub
//super.run();
    prolongedActionMethod();
    handler.sendEmptyMessage(0);
}


    }.start();
    }
 
 
    private void prolongedActionMethod() {
    try {
    Thread.sleep(10000);
    }catch (Exception e){
    e.printStackTrace();
    }
    }
 
}

參考:
http://android.yaohuiji.com/archives/770

沒有留言:

張貼留言