六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 47|回复: 0

各种线程总结

[复制链接]

升级  82%

292

主题

292

主题

292

主题

进士

Rank: 4

积分
910
 楼主| 发表于 2013-1-15 02:19:49 | 显示全部楼层 |阅读模式
1.public void onClick(View v) {
    Thread t = new Thread(){
    public void run(){
 // Long time comsuming operation
   }
   };
   t.start();
}
public void onClick(View v) {
 Thread t = new Thread(){
  public void run(){
   // Long time consuming operation
   _activity.runOnUiThread(new Runnable() {

    @Override
    public void run() {
     _activity.setStatus("Long Operation Completed");

    }
   });
  }
 };
 t.start();
}
2.

public void onClick(View v) {
 // TODO Auto-generated method stub
 Thread t = new Thread(){
  @Override
  public void run() {
   // Long time consuming operation
   status.post(new Runnable() {

    @Override
    public void run() {
     status.setText("Long Operstion Completed");

    }
   });
  }
 };
 t.start();
}
3.

public void onClick(View v) {
 // TODO Auto-generated method stub
 Thread t = new Thread(){
  @Override
  public void run() {
   // Long time consuming operation
   status.postDelayed(new Runnable() {

    @Override
    public void run() {
     status.setText("Long Operstion Completed");

    }
   }, 1000);
  }
 };
 t.start();
}

4.
private Handler handler = new Handler() {
@Override
 public void handleMessage(Message msg) {
  // Code to process the response and update UI.
 }
};

public void onClick(View v) {
 Thread t = new Thread(){
  public void run(){
   // Long time comsuming operation
   Message myMessage=new Message();
   Bundle resBundle = new Bundle();
   resBundle.putString("status", "SUCCESS");
   myMessage.obj=resBundle;
   handler.sendMessage(myMessage);
  }
 };
 t.start();
}

5.
public class LongTimeConsumingOperation extends AsyncTask<String, Void, String> {
 @Override
 protected String doInBackground(String... params) {
  // perform Long time consuming operation
  return null;
 }

 /* (non-Javadoc)
  * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
  */
 @Override
 protected void onPostExecute(String result) {
  // TODO Auto-generated method stub
  super.onPostExecute(result);
 }

 /* (non-Javadoc)
  * @see android.os.AsyncTask#onPreExecute()
  */
 @Override
 protected void onPreExecute() {
  // TODO Auto-generated method stub
  super.onPreExecute();
 }

 /* (non-Javadoc)
  * @see android.os.AsyncTask#onProgressUpdate(Progress[])
  */
 @Override
 protected void onProgressUpdate(Void... values) {
  // TODO Auto-generated method stub
  super.onProgressUpdate(values);
 }

}


public void onClick(View v) {
new LongTimeConsumingOperation().execute("");
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表