因爲性能要求,android要求只能在UI線程中更新UI,要想在其餘線程中更新UI,給你們介紹一種方式:使用AsyncTask異步任務。java
下面用這種方式更新一個TextView:android
注:更新UI的操做只能在onPostExecute(String result)方法中。app
package com.example.runonuithreadtest; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends Activity { private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); new Yibu().execute(); } class Yibu extends AsyncTask<String, String, String> { @Override protected String doInBackground(String... params) { try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub tv.setText("更新後的TextView"); } } }
固然對APP的性能測試,我比較經常使用的是這個平臺:www.ineice.com異步