因爲性能要求,android要求只能在UI線程中更新UI,要想在其餘線程中更新UI,給你們介紹一種方式:使用Handler消息傳遞機制。java
下面用這種方式更新一個TextView:android
package com.example.runonuithreadtest; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; public class MainActivity extends Activity { private TextView tv; Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { if(msg.what==0x123) { tv.setText("更新後的TextView"); } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); new MyThread().start(); } class MyThread extends Thread { @Override public void run() { //延遲兩秒更新 try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } handler.sendEmptyMessage(0x123); } } }
固然對APP的性能測試,我比較經常使用的是這個平臺:www.ineice.comapp