Android 线程中更新UI

安卓在线程中更新UI的方法就是发消息到主线程

private Thread mThread;
public void runThread() {
    mThread = new Thread(runnable);
    mThread.start();//线程启动
}
Runnable runnable = new Runnable() {
    @Override
    public void run() {
      //todo
    mHandler.obtainMessage(0).sendToTarget();
    };
}
private Handler mHandler = new Handler() {
    public void handleMessage(Message msg) {//此方法在ui线程运行
        switch (msg.what) {
            case 0:
                  //todo
                  break;
        }
    }
};
不要忘记在Activity销毁是,结束线程。

@Override
protected void onDestroy() {
    super.onDestroy();
    mHandler.removeCallbacks(runnable);
}

你可能感兴趣的:(java,android,解决方案,YaphetZhao,Yaphet赵勇)