Skip to content

Android View 方式集成 Runtime SDK

runtime sdk集成可以使开发者自有APK快速实现runtime运行环境的能力

第1步:引⼊SDK

点击查看源码
js
# ⼯程根gradle增加sdk的maven仓库
allprojects {
 repositories {
 maven { url 'https://nexus.extscreen.com/repository/maven-public/' }
 } 
}
 
# app gradle增加引⽤
dependencies {
 implementation 'com.extscreen.runtime:official:1.0.31'
}

第2步:初始化

Application调⽤初始化⽅法

点击查看源码
javascript
import EsKitInitHelper;

public class App extends Application {
    @Override
    public void onCreate() {
        EsKitInitHelper.init(this, InitConfig.getDefault() 
        .setDebug(BuildConfig.DEBUG) // 是否打印log
        .addFlags(InitConfig.FLAG_DYNAMIC_SO) // 为了减⼩sdk体积,开启动态SO,如需内置so,请联系开发者
        );
    } 
}

第3步:Android 根布局添加 ES View的承载布局

android:id="@+id/es_browser_nav_host"id是固定值,必须定义为es_browser_nav_host

点击查看源码
html

<FrameLayout
        android:id="@+id/es_browser_root_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"/>

<androidx.fragment.app.FragmentContainerView
        android:id="@+id/es_browser_nav_host"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"/>

第4步:初始化ES快应用信息,并加载

点击查看源码
js
    private void initES() {
        //初始化快应用信息
        EsData data = new EsData();
        //设置快应用的包名
        data.setAppPackage(esAppPackageName);
        
        EsMap exp = new EsMap();
        //必须配置from字段信息
        EsMap from = new EsMap();
        from.pushObject(BaseEvent.ES_REFERER, BaseEvent.FROM_OUTER);
        from.pushObject(BaseEvent.ES_REFERER1, getPackageName());
        //Activity的名称
        from.pushObject(BaseEvent.ES_REFERER2, "XXXXXX");
        exp.pushMap(K_FROM, from);
        
        data.setExp(exp);
        data.isHomePage(true);

        //从网络加载快应用数据
        loadFromNetwork(data);
    }

    private void loadFromNetwork(EsData data) {
        EsManager.get().load(this, data, new IEsActivityProxy.LoadCallback() {
            @Override
            public void onStartLoad(IEsActivityProxy proxy) {
                //保存mActivityProxy
                mActivityProxy = proxy;
            }

            @Override
            public void onEsViewCreated(View view) {
                //添加ES View到根布局
                rootView.addView(view);
            }

            @Override
            public void onError(EsException e) {
            }

            @Override
            public void requestFinish() {
            }
        });
    }

第5步:配置ES View调用接口

点击查看源码
js
   @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (mActivityProxy != null)
            mActivityProxy.dispatchKeyEvent(event);
        return super.dispatchKeyEvent(event);
    }

    @Override
    public void onBackPressed() {
        if (mActivityProxy != null) {
            if (mActivityProxy.onBackPressed(super::onBackPressed)) {
                return;
            }
        }
        super.onBackPressed();
    }

    @Override
    protected void onResume() {
        if (mActivityProxy != null) mActivityProxy.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        if (mActivityProxy != null) mActivityProxy.onPause();
        super.onPause();
    }

    @Override
    protected void onStart() {
        if (mActivityProxy != null) mActivityProxy.onStart();
        super.onStart();
    }

    @Override
    protected void onStop() {
        if (mActivityProxy != null) mActivityProxy.onStop();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        if (mActivityProxy != null) mActivityProxy.onDestroy();
        mActivityProxy = null;
        super.onDestroy();
    }

完整代码示例:

activity_es_view.xml

点击查看源码
xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:context=".ESViewActivity">

    <FrameLayout
            android:id="@+id/es_browser_root_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"/>

    <androidx.fragment.app.FragmentContainerView
            android:id="@+id/es_browser_nav_host"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"/>

</FrameLayout>

Activity.java

点击查看源码
javascript
package eskit.sdk.sample;

import static eskit.sdk.core.protocol.EsProtocolDispatcher.K_FROM;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;

import eskit.sdk.core.EsData;
import eskit.sdk.core.EsManager;
import eskit.sdk.core.count.BaseEvent;
import eskit.sdk.core.internal.IEsActivityProxy;
import eskit.sdk.support.EsException;
import eskit.sdk.support.args.EsMap;

public class ESViewActivity extends AppCompatActivity {

    private IEsActivityProxy mActivityProxy;
    private FrameLayout rootView;
    //快应用的包名
    private String esAppPackageName = "es.hello.world";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_es_view);
        rootView = this.findViewById(R.id.es_browser_root_view);
        initES();
    }

    private void initES() {
        EsData data = new EsData();
        data.setAppPackage(esAppPackageName);
        EsMap exp = new EsMap();
        EsMap from = new EsMap();
        from.pushObject(BaseEvent.ES_REFERER, BaseEvent.FROM_OUTER);
        from.pushObject(BaseEvent.ES_REFERER1, getPackageName());
        
        //Activity的名称
        from.pushObject(BaseEvent.ES_REFERER2, "XXXXXX");
        
        exp.pushMap(K_FROM, from);
        data.setExp(exp);
        data.isHomePage(true);
        //
        loadFromNetwork(data);
    }

    private void loadFromNetwork(EsData data) {
        EsManager.get().load(this, data, new IEsActivityProxy.LoadCallback() {
            @Override
            public void onStartLoad(IEsActivityProxy proxy) {
                mActivityProxy = proxy;
            }

            @Override
            public void onEsViewCreated(View view) {
                rootView.addView(view);
            }

            @Override
            public void onError(EsException e) {
            }

            @Override
            public void requestFinish() {
                finish();
            }
        });
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (mActivityProxy != null)
            mActivityProxy.dispatchKeyEvent(event);
        return super.dispatchKeyEvent(event);
    }

    @Override
    public void onBackPressed() {
        if (mActivityProxy != null) {
            if (mActivityProxy.onBackPressed(super::onBackPressed)) {
                return;
            }
        }
        super.onBackPressed();
    }

    @Override
    protected void onResume() {
        if (mActivityProxy != null) mActivityProxy.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        if (mActivityProxy != null) mActivityProxy.onPause();
        super.onPause();
    }

    @Override
    protected void onStart() {
        if (mActivityProxy != null) mActivityProxy.onStart();
        super.onStart();
    }

    @Override
    protected void onStop() {
        if (mActivityProxy != null) mActivityProxy.onStop();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        if (mActivityProxy != null) mActivityProxy.onDestroy();
        mActivityProxy = null;
        super.onDestroy();
    }
}