点击按钮加载初始化 uni-app sdk, 之后打开小程序。以下是基于官方demo的打开小程序的代码片段,改动点为延迟初始化 uni-app sdk:
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
// 延迟加载 Uni-App sdk, 这里是点击 button1 的时候,在打开 uni-app 前初始化 sdk
initUniAppSDK();
if (!DCUniMPSDK.getInstance().isInitialize()) {
Toast.makeText(mContext, "uni-app sdk 未初始化", Toast.LENGTH_SHORT).show();
return;
}
UniMPOpenConfiguration uniMPOpenConfiguration = new UniMPOpenConfiguration();
// 为了方便观察白屏现象,不设置 SplashView
// uniMPOpenConfiguration.splashClass = MySplashView.class;
uniMPOpenConfiguration.extraData.put("darkmode", "light");
IUniMP uniMP = DCUniMPSDK.getInstance().openUniMP(mContext,"__UNI__F743940", uniMPOpenConfiguration);
mUniMPCaches.put(uniMP.getAppid(), uniMP);
} catch (Exception e) {
e.printStackTrace();
}
}
});
延迟加载小程序代码片段,以下是官方 demo 的初始化 uni-sdk 的代码,在此基础上的改动点 setEnableBackground 改为 false:
public void initUniAppSDK() {
if (DCUniMPSDK.getInstance().isInitialize()) {
Log.i("unimp","uni-app sdk 已初始化过,不再初始化");
return;
}
try {
WXSDKEngine.registerModule("TestModule", TestModule.class);
WXSDKEngine.registerComponent("myText", TestText.class);
} catch (Exception e) {
e.printStackTrace();
}
//初始化 uni小程序SDK ----start----------
MenuActionSheetItem item = new MenuActionSheetItem("关于", "gy");
MenuActionSheetItem item1 = new MenuActionSheetItem("获取当前页面url", "hqdqym");
MenuActionSheetItem item2 = new MenuActionSheetItem("跳转到宿主原生测试页面", "gotoTestPage");
List<MenuActionSheetItem> sheetItems = new ArrayList<>();
sheetItems.add(item);
sheetItems.add(item1);
sheetItems.add(item2);
Log.i("unimp","onCreate----");
DCSDKInitConfig config = new DCSDKInitConfig.Builder()
.setCapsule(false)
.setMenuDefFontSize("16px")
.setMenuDefFontColor("#ff00ff")
.setMenuDefFontWeight("normal")
.setMenuActionSheetItems(sheetItems)
// 改动点: 禁用在后台运行
.setEnableBackground(false)
.setUniMPFromRecents(false)
.build();
DCUniMPSDK.getInstance().initialize(this, config, new IDCUniMPPreInitCallback() {
@Override
public void onInitFinished(boolean b) {
Log.d("unimpaa","onInitFinished----"+b);
}
});
//初始化 uni小程序SDK ----end----------
}
tatak (作者)
好的,麻烦了
2024-11-22 13:54