# 创建Activity

创建一个Activity在生命周期里调用一些接口用来运行JSView。

# Activity

public class JSVActivity extends AppCompatActivity {
    private MainPageProxy mMainPageProxy;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mMainPageProxy=new MainPageProxy(this);
        mMainPageProxy.onCreate(savedInstanceState,null);
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        mMainPageProxy.onNewIntent(intent);
    }
    @Override
    protected void onStart() {
        super.onStart();
        mMainPageProxy.onStart();
    }
    @Override
    protected void onStop() {
        super.onStop();
        mMainPageProxy.onStop();
    }
    @Override
    protected void onResume() {
        super.onResume();
        mMainPageProxy.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        mMainPageProxy.onPause();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMainPageProxy.onDestroy();
        // 直接kill 快速释放内存
        Process.killProcess(Process.myPid());
    }
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        return mMainPageProxy.dispatchKeyEvent(event) || super.dispatchKeyEvent(event);
    }
    
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

# Manifest

JsView希望运行在独立进程里(但不是必须),要在ManiFest中声明 android:processandroid:launchMode="singleInstance",其他随意。

启动action与进程名建议自己重新定义,别照抄例子原文。

<activity
    android:name=".render.activity.JSVActivity"
    android:launchMode="singleInstance"
    android:screenOrientation="sensorLandscape"
    android:theme="@style/ActivityTheme"
    android:exported="true"
    android:process=":demojsv">
    <intent-filter>
        <category android:name="android.intent.category.DEFAULT" />
        <action android:name="qcode.app.action.start.DEMO_JSV" />
    </intent-filter>
</activity>
1
2
3
4
5
6
7
8
9
10
11
12

# 使用自定义布局(可选)

Activity的onCreate()也可设置自己的布局文件,只要 MainPageProxy的构造函数传入ViewGroup用于显示JsView即可

public class JSVActivity extends AppCompatActivity {
    private MainPageProxy mMainPageProxy;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMainPageProxy=new MainPageProxy(this);
        mMainPageProxy.onCreate(savedInstanceState, (ViewGroup) findViewById(R.id.mainRootView));
    }
    ...
}
1
2
3
4
5
6
7
8
9
10
11

# 注册小程序监听(可选)

需要在mMainPageProxy.onCreate()之前去调用

mMainPageProxy.setMiniAppObserver(new MiniAppObserver() {
    @Override
    public void onMiniAppStartLoad(MiniApp miniApp) {
        // 小程序开始加载, 传入URL时被回调(从js端发起的openWindow开启的同Core的JsView也会触发此回调)
    }

    @Override
    public void onMiniAppLoadSuccess(MiniApp miniApp) {
        // 小程序加载成功,对应js端小程序调用notifyPageLoaded时机
    }

    @Override
    public void onMiniAppLoadFail(MiniApp miniApp, int errorCode) {
        // 小程序加载失败,对应内核加载失败或者小程序js加载超时的时机
    }
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

点击查看 MiniAppObserver

Last Updated: 8/30/2023, 3:43:38 AM