打开 H5 离线包后,您可以对 H5 页面进行以下操作:
Bundle param = new Bundle();
// 要打开的离线包 URL /www/index.html,必须加 /
// 如果不传 URL,容器将默认打开离线包配置的 URL,建议不要传 URL,只传 appId,前端就可以自己配置打开的页面
param.putString(H5Param.LONG_URL,url);
LauncherApplicationAgent.getInstance().
getMicroApplicationContext().startApp(null, appId, param);
// 如果是单独接容器的客户端,才使用 h5Service.startPage 的方式
public static final void openH5(String url) {
if (TextUtils.isEmpty(url)) {
return;
}
H5Service h5Service = LauncherApplicationAgent.getInstance().getMicroApplicationContext()
.findServiceByInterface(H5Service.class.getName());
H5Bundle bundle = new H5Bundle();
Bundle param = new Bundle();
// 要打开的离线包 appId
param.putString(H5Param.APP_ID, appId);
// 要打开的离线包 URL /www/index.html,必须加 /
// 如果不传 URL,容器将默认打开离线包配置的 URL,建议不要传 URL,只传appId,前端就可以自己配置打开的页面
param.putString(H5Param.LONG_URL,url);
bundle.setParams(param);
if (h5Service != null) {
h5Service.startPage(AlipayApplication.getInstance().getMicroApplicationContext().findTopRunningApp(),
bundle);
}
}
根据您的实际情况选择以下方法将单个容器的视图(view)嵌入到原生页面中:
提示:使用异步方法不占用主线程,不会影响性能。
public static final void openH5(String url) {
if (TextUtils.isEmpty(url)) {
return;
}
H5Service h5Service = LauncherApplicationAgent.getInstance().getMicroApplicationContext()
.findServiceByInterface(H5Service.class.getName());
H5Bundle bundle = new H5Bundle();
Bundle param = new Bundle();
// 要打开的离线包 appId
param.putString(H5Param.APP_ID, appId);
// 要打开的离线包内的 URL /www/index.html,必须加 /
// 如果不传 URL,容器将默认打开离线包默认配置的 URL
param.putString(H5Param.LONG_URL,url);
bundle.setParams(param);
if (h5Service != null) {
H5Page h5Page=h5Service.createPage(activity,bundle);
View view=h5Page.getContentView(),
// view 最后添加到自己的页面中就行
}
}
H5Service h5Service = LauncherApplicationAgent.getInstance().getMicroApplicationContext()
.findServiceByInterface(H5Service.class.getName());
H5Bundle bundle = new H5Bundle();
Bundle param = new Bundle();
param.putString(H5Param.APP_ID, appId);
param.putString(H5Param.LONG_URL, url);
bundle.setParams(param);
if (h5Service != null) {
h5Service.createPageAsync(activity, bundle, h5PageReadyListener);
}
在文档使用中是否遇到以下问题
更多建议
匿名提交