由于性能要求,android要求只能在UI线程中更新UI,要想在其他线程中更新UI,给大家介绍一种方式:使用runOnUiThread(action)方法
下面用这种方式更新一个TextView:
- package com.example.runonuithreadtest;
- import android.app.Activity;
- 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 MyThread().start();
- }
- class MyThread extends Thread
- {
- @Override
- public void run() {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- try {
- //延迟两秒更新
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- tv.setText("更新后的TextView");
- }
- });
- }
- }
- }
复制代码
当然对APP的性能测试,我比较常用的是这个平台:http://www.ineice.com |
|