美文网首页
Android异步更新UI的方式之使用Handler的post(

Android异步更新UI的方式之使用Handler的post(

作者: 读行游 | 来源:发表于2015-09-18 10:01 被阅读222次

    由于性能要求,android要求只能在UI线程中更新UI,要想在其他线程中更新UI,给大家介绍一种方式:使用Handler的post(Runnabel r)方法

    下面用这种方式更新一个TextView:

    1.packagecom.example.runonuithreadtest;

    2.importandroid.app.Activity;

    3.importandroid.os.Bundle;

    4.importandroid.os.Handler;

    5.importandroid.widget.TextView;

    6.publicclass MainActivity extends Activity {

    7.privateTextView tv;

    8.@Override

    9.protectedvoid onCreate(Bundle savedInstanceState) {

    10.super.onCreate(savedInstanceState);

    11.setContentView(R.layout.activity_main);

    12.tv = (TextView) findViewById(R.id.tv);

    13.Handler handler = new Handler();

    14.handler.post(new Runnable(){

    15.@Override

    16.public void run() {

    17.try {

    18.//延迟两秒更新

    19.Thread.sleep(2000);

    20.} catch (InterruptedException e) {

    21.e.printStackTrace();

    22.}

    23.tv.setText("更新后的TextView");

    24.}

    25.});

    26.}

    27.}

    当然对APP的性能测试,我比较常用的是这个平台:www.ineice.com

    相关文章

      网友评论

          本文标题:Android异步更新UI的方式之使用Handler的post(

          本文链接:https://www.haomeiwen.com/subject/eopbcttx.html