Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 總結Android中多線程更新應用的頁面信息的方式

總結Android中多線程更新應用的頁面信息的方式

編輯:關於Android編程

一、runOnUiThread的用法
runOnUiThread是Activity的內部方法,使用時最好指定當前的環境變量(Context)。

new Thread(new Runnable() {

    @Override
    public void run() {
      runOnUiThread(new Runnable() {
        public void run() {
          Toast.makeText(mainActivity.this,"UI操作。。。",0).show();
        }
      });

    }
  }).start();

執行runOnUiThread這個方法會調用父類中的

public final void runOnUiThread(Runnable action){
 if(Thread.currentThread()!=mUiThread){
 mHandler.post(action);
 }else{
  action.run();
 }
}

二、新線程中View直接在UI線程中更新的方法

textView.postDelayed(new Runnable() {

    @Override
    public void run() {
      textView.setText("Test View.post(Runnable)");

    }
  }, 1000);
-

 textView.post(new Runnable() {

    @Override
    public void run() {
      textView.setText("Test View.postDelay(Runnable,long)");

    }
  });

三、Handler(消息傳遞機制)使用

Handler myHandler = new Handler(){
 public void handleMessage(Message msg){
  super.handleMessage(msg); 
  }
};

也可以繼承handler

class MyHandler extends handler{
  public MyHandler(){

 }
 @Override
 public void handleMessage(Message msg){
  super.handleMessage(msg);
 }
}

分發Message或者Runnable對象到handler所在的線程中一般handler在主線程中。

handler中一些分發消息的方法:

  • post(Runnable)
  • postAtTime(Runnable,long)
  • postDelay(Runnable,long)
  • sendEmptyMessage(int what)
  • sendMessage(Message)
  • senMessageAtTime(Message,long)
  • sendMessageDelayed(Message,long)

post方式添加一個實現Runnable接口的匿名對象到消息對列中,在目標收到消息後就可以以回調的方式在自己的線程中執行

Message對象所具有的屬性:
屬性 類型 描述 arg1 int 用來存放整型數據 arg2 int 用來存放整型數據 obj Object 用來存放發送給接收器的Object任意對象 replyTo Messager 用來指定此Message發送到何處的可選Message對象 what int 用於指定用戶自定義的消息代碼這樣接受者就可以了解這個消息的信息

Message message = Message.obtain();
message.arg1 = 1;
message.arg2 = 2;
message.obj = "Demo";
message.what = 3;
Bundle bundle = new Bundle();
bundle.putString( "name","Lucy");
message.setData(bundle);

下面貼上一段示例代碼(開啟新線程實現電子廣告牌)

public class MainActivity extends Activity implements Runnable {

  private ImageView iv;
  private TextView tv;

  private Handler handler;
  private int[] path = new int[]{R.drawable.img01,R.drawable.img02,R.drawable.img03,R.drawable.img04,
      R.drawable.img05,R.drawable.img06};
  private String[] title = new String[]{"編程詞典系列","高效開發","快樂分享","用戶人群","快速學習","全方位查詢"};
  private int index =0;
  @Override
  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    iv = (ImageView) findViewById(R.id.imageView1);
    tv = (TextView) findViewById(R.id.textView1);


    Thread t = new Thread(this);
    t.start();
    handler = new Handler(){
      public void handleMessage(Message msg){
        if (msg.what ==1) {
          tv.setText(msg.getData().getString("title"));
          iv.setImageResource(path[msg.arg1]);
        }
        super.handleMessage(msg);
      }
    };


  }

  @Override
  public void run() {

    while(!Thread.currentThread().isInterrupted()){
      index = new Random().nextInt(path.length);
      Message m = handler.obtainMessage();
      m.arg1 = index;
      Bundle bundle = new Bundle();
      m.what = 1;
      bundle.putString("title", title[index]);
      m.setData(bundle);
      handler.sendMessage(m);
      try {
        Thread.sleep(2000);
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }      
    }    

  }

  }

四、AsyncTask異步任務的用法

  • AsyncTask實際上是一個線程池,在代碼上比handler要輕量級但是實際上要比Handler要耗資源,Handler僅僅發送了一個消息隊列,連線程池對沒有開。
  • onPreExecute(),(可選方法)最新用戶調用excute時的接口,任務執行之前調用該方法,可以在這裡顯示進度對話框。
  • doInBackground(Params...),後台執行比較好使的操作,不能直接操縱UI。在該方法中使用publishProgress(progress...)來更新任務的進度。
  • onProgressUpdate(Progress...),在主線程中執行,顯示進度條
  • onPostExecute(Result),此方法可以從doinbackground得到的結果來操作UI,在主線程中執行,執行的結果作為參數返回。
  • onCancelled(Object)調用此方法可以隨時取消操作。

AsyncTask定義的三種泛型

  • params: 啟動任務執行的輸入參數,如:http請求的URL
  • progress:後台任務執行的百分比
  • result:返回結果,如:String、list集合等
 private class MyTask extends AsyncTask<params, progress, result> { ... }

示例代碼:

  private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
   protected Long doInBackground(URL... urls) {
     int count = urls.length;
     long totalSize = 0;
     for (int i = 0; i < count; i++) {
       totalSize += Downloader.downloadFile(urls[i]);
       publishProgress((int) ((i / (float) count) * 100));
       // Escape early if cancel() is called
       if (isCancelled()) break;
     }
     return totalSize;
   }

   protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
   }

   protected void onPostExecute(Long result) {
     showDialog("Downloaded " + result + " bytes");
   }
  }

獲取網絡圖片的示例代碼:

 public class MainActivity extends ActionBarActivity {
  private ImageView iv;
  private Button bt;
  private String imagePath = "http://192.168.1.1/sa";
  private ProgressDialog dialog;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    iv = (ImageView) findViewById(R.id.imageView1);
    bt = (Button) findViewById(R.id.button1);
    dialog = new ProgressDialog(this);
    dialog.setTitle("提示信息:");
    dialog.setMessage("正在下載。。。");
    bt.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        new MyTask().execute(imagePath);

      }
    });
  }
  public class MyTask extends AsyncTask<String, Void, Bitmap>{

    @Override
    protected Bitmap doInBackground(String... params) {
      HttpClient httpClient = new DefaultHttpClient();
      HttpGet httpGet = new HttpGet(params[0]);
      Bitmap bitmap = null;
      try {
        HttpResponse httpResponse = httpClient.execute(httpGet);
        if (httpResponse.getStatusLine().getStatusCode()==200) {
          HttpEntity httpEntity = httpResponse.getEntity();
          byte[] data = EntityUtils.toByteArray(httpEntity);
          bitmap= BitmapFactory.decodeByteArray(data, 0, data.length);
        }
      } catch (Exception e) {

        e.printStackTrace();
      }

      return bitmap;
    }

    @Override
    protected void onPreExecute() {      
      super.onPreExecute();
      dialog.show();
    }

    @Override
    protected void onPostExecute(Bitmap result) {      
      super.onPostExecute(result);
      iv.setImageBitmap(result);
      dialog.dismiss();
    }    
   }  
  }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved