Activity的显示

1.Activity的显示

Activity的显示原理如下图所示
在这里插入图片描述
在Activity启动的时候会创建一个PhoneWindow,而这个PhoneWindow会包含一个DecorView,DecorView就是Activity的rootview,其中包含一个bar和contentView,我们自己写的布局会放到contentView当中。对于每个DecorView都有一个对应的ViewRootImpl对象,能够和WMS进行双向通信,负责DecorView的绘制。
接着我们来看下Activity的显示流程图
在这里插入图片描述
有以下几个关键步骤需要我们关注一下
1.setContentView
以下为PhoneWindow当中的setContentView,可以看到我们自己定义的layout被夹在到了mContentParent,所以我们的方法名称才叫做setContentView,而不叫setView。

//PhoneWindow.java
public void setContentView(int layoutResID) {
    if (mContentParent == null) {
        //1.确保decorView被创建
        installDecor();
    } else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
        mContentParent.removeAllViews();
    }

    if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
        final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,
                getContext());
        transitionTo(newScene);
    } else {
        //2.将我们自己定义的layout加载到contentview当中
        mLayoutInflater.inflate(layoutResID, mContentParent);
    }
......
}

2.handleResumeActivity
如下代码为ActivityThread的handleResumeActivity方法,会触发activity的onResume周期函数。此方法中会将decorView添加到windowmanager当中,并创建ViewRootImpl对象,负责管理view的绘制,因此Activity在onResume周期函数之后才能够可见。

public void handleResumeActivity(IBinder token, boolean finalStateRequest, boolean isForward,
        String reason) {
        //1.执行activity的onresume周期函数
		final ActivityClientRecord r = performResumeActivity(token, finalStateRequest, reason);
		......
		ViewManager wm = a.getWindowManager();
		//2.将decorview添加到windowManager当中
		wm.addView(decor, l);
		 r.activity.makeVisible();
		 ......
}

3.setView
如下代码为ViewRootImpl的setView方法,如注释1所示,会先出发重绘制,然后会调用mWindowSession.addToDisplay方法,将window添加到wms,wms将会给应用分配surface,并掌管surface的尺寸和显示顺序。

ViewRootImpl.java
public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
    ......
    //1.触发重绘
	requestLayout();
	......
	//2.binder调用,wms通信
	mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
	                            getHostVisibility(), mDisplay.getDisplayId(), mWinFrame,
	                            mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
	                            mAttachInfo.mOutsets, mAttachInfo.mDisplayCutout, mInputChannel);
	......
}
要在 Android Activity显示 Service 数据,您可以使用以下步骤: 1. 创建 Service 类并在其中实现您的逻辑。 2. 在 Service 类中创建一个公共方法,该方法返回您想要在 Activity显示的数据。 3. 在 Activity 中创建一个服务连接器类,该类实现 ServiceConnection 接口并在其中实现 onServiceConnected() 和 onServiceDisconnected() 方法。 4. 在 Activity 中使用 bindService() 方法将 Activity 绑定到 Service 上。 5. 在 Activity 中实现 onActivityResult() 方法并在其中处理从 Service 返回的数据。 6. 在 Activity 中使用 TextView 或其他 UI 控件来显示从 Service 返回的数据。 以下是一个简单的示例: 在 Service 类中: ```java public class MyService extends Service { public String getData() { return "Hello from the service!"; } } ``` 在 Activity 类中: ```java public class MainActivity extends AppCompatActivity implements ServiceConnection { private MyService myService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bindService(new Intent(this, MyService.class), this, BIND_AUTO_CREATE); } @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { MyService.MyBinder binder = (MyService.MyBinder) iBinder; myService = binder.getService(); String data = myService.getData(); TextView textView = findViewById(R.id.text_view); textView.setText(data); } @Override public void onServiceDisconnected(ComponentName componentName) { myService = null; } } ``` 在 ServiceConnection 类中: ```java public class MyServiceConnection implements ServiceConnection { private MainActivity activity; public MyServiceConnection(MainActivity activity) { this.activity = activity; } @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { MyService.MyBinder binder = (MyService.MyBinder) iBinder; MyService myService = binder.getService(); String data = myService.getData(); activity.showData(data); } @Override public void onServiceDisconnected(ComponentName componentName) { activity.showData(null); } } ``` 在 Activity 中使用 bindService() 方法将 Activity 绑定到 Service 上: ```java MyServiceConnection connection = new MyServiceConnection(this); bindService(new Intent(this, MyService.class), connection, BIND_AUTO_CREATE); ``` 在 Activity 中实现 onActivityResult() 方法并在其中处理从 Service 返回的数据: ```java public void showData(String data) { TextView textView = findViewById(R.id.text_view); if (data != null) { textView.setText(data); } else { textView.setText("No data received from Service"); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值