设为首页 加入收藏

TOP

Android子线程在没有ViewRoot的情况下能刷新UI吗?(一)
2014-11-24 12:42:41 来源: 作者: 【 】 浏览:0
Tags:Android 线程 没有 ViewRoot 情况 刷新

让我们看个实例吧:


package com.david.test.helloworld;



import android.os.Bundle;


import android.widget.Button;



public class TestActivity extends Activity {


Button btn = null;



/** Called when the activity is first created. */


public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);


setContentView(R.layout.main);



btn = (Button) findViewById(R.id.Button01);



TestThread2 t = new TestThread2(btn);


t.start();


}



class TestThread2 extends Thread {


Button btn = null;



public TestThread2(Button btn) {


this.btn = btn;


}



@Override


public void run() {


btn.setText("TestThread2.run");


}


}


}


建立一个工程,将上述代码拷贝进去,运行看看吧! Btn的文本一定改变为"TestThread2.run"了。


那么这到底是怎么回事呢?当我发现这个问题时,也困惑了。经过一番调查后,真相大白。现在和大家分享一下。奥秘在于ViewRoot的建立时间,它是在ActivityThread.java的final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward)里创建的。


看看代码吧:


final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {


// If we are getting ready to gc after going to the background, well


// we are back active so skip it.


unscheduleGcIdler();



ActivityRecord r = performResumeActivity(token, clearHide);



if (r != null) {


final Activity a = r.activity;



if (localLOGV) Slog.v(


TAG, "Resume " + r + " started activity: " +


a.mStartedActivity + ", hideForNow: " + r.hideForNow


+ ", finished: " + a.mFinished);



final int forwardBit = isForward


WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;



// If the window hasn't yet been added to the window manager,


// and this guy didn't finish itself or start another activity,


// then go ahead and add the window.


boolean willBeVisible = !a.mStartedActivity;


if (!willBeVisible) {


try {


willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(


a.getActivityToken());


} catch (RemoteException e) {


}


}


if (r.window == null && !a.mFinished && willBeVisible) {


r.window = r.activity.getWindow();


View decor = r.window.getDecorView();


decor.setVisibility(View.INVISIBLE);


ViewManager wm = a.getWindowManager();


WindowManager.LayoutParams l = r.window.getAttributes();


a.mDecor = decor;


l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;


l.softInputMode |= forwardBit;


if (a.mVisibleFromClient) {


a.mWindowAdded = true;


wm.addView(decor, l);


}



// If the window has already been added, but during resume


// we started another activity, then don't yet make the


// window visible.


} else if (!willBeVisible) {


if (localLOGV) Slog.v(


TAG, "Launch " + r + " mStartedActivity set");


r.hideForNow = true;


}



// The window is now visible if it has been added, we are not


// simply finishing, and we are not start

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android里子线程真的不能刷新UI吗.. 下一篇在桌面红旗Linux6上编写并运行第..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·常用meta整理 | 菜鸟 (2025-12-25 01:21:52)
·SQL HAVING 子句:深 (2025-12-25 01:21:47)
·SQL CREATE INDEX 语 (2025-12-25 01:21:45)
·Shell 传递参数 (2025-12-25 00:50:45)
·Linux echo 命令 - (2025-12-25 00:50:43)