1***@163.com
1***@163.com
  • 发布:2024-05-27 17:22
  • 更新:2024-05-30 13:46
  • 阅读:344

安卓释放远程wgt后打开小程序白屏,uniSDK

分类:uni小程序sdk

!![```javascript
安卓释放远程wgt后打开小程序白屏


```javascript  
package com.lucas.flutter_uni_test;  

import io.flutter.embedding.android.FlutterActivity;  
import io.flutter.embedding.engine.FlutterEngine;  
import io.flutter.plugin.common.MethodChannel;  
import io.flutter.plugin.common.MethodCall;  

import java.util.ArrayList;  
import java.util.List;  
import java.util.HashMap;  
import java.util.Objects;  

import androidx.annotation.NonNull;  
import android.util.Log;  
import android.content.Context;  
import android.os.Handler;  

import org.json.JSONObject;  

import io.dcloud.feature.sdk.DCSDKInitConfig;  
import io.dcloud.feature.sdk.DCUniMPSDK;  
import io.dcloud.feature.sdk.Interface.IDCUniMPPreInitCallback;  
import io.dcloud.feature.sdk.MenuActionSheetItem;  
import io.dcloud.feature.sdk.Interface.IUniMP;  
import io.dcloud.feature.unimp.config.IUniMPReleaseCallBack;  
import io.dcloud.feature.unimp.config.UniMPReleaseConfiguration;  
import io.dcloud.feature.unimp.config.UniMPOpenConfiguration;  

public class MainActivity extends FlutterActivity {  

    Context mContext;  

    /** unimp小程序实例缓存**/  
    HashMap<String, IUniMP> mUniMPCaches = new HashMap<>();  

    private static final String TAG = "MainActivity"; // 定义一个  
    private static final String CHANNEL = "com.lucas.flutter_uni";  

    @Override  
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {  
    super.configureFlutterEngine(flutterEngine);  
    mContext = this;  
        new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)  
            .setMethodCallHandler(  
                (@NonNull MethodCall call, @NonNull MethodChannel.Result result) -> {  
                // This method is invoked on the main thread.  
                // TODO  

                String callMethod = call.method;  

                switch (callMethod) {  

                    case "initialize":  
                        // 执行相关代码  
                        initialize(call, result);  
                        break;  
                    case "installMP":  
                        // 执行相关代码  
                        installMP(call, result);  
                        break;  
                    case "openMP":  
                        // 执行相关代码  
                        openMP(call, result);  
                        break;  
                    case "isExistsApp":  
                        // 执行相关代码  
                        isExistsApp(call, result);  
                        break;  
                    case "getUniMPVersionInfoWithAppid":  
                        // 执行相关代码  
                        getUniMPVersionInfoWithAppid(call, result);  
                        break;  
                    // 添加其他case语句...  
                    default:  
                        // 如果没有匹配的case,执行默认代码  

                        break;  
                }  

            });  
    }  

    /**  
     * 初始化小程序引擎  
     *  
     * @param call     调用该方法的参数,不可为空  
     * @param result   方法调用的结果回调,不可为空  
     * @return         无返回值  
     */  
    private void initialize(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {  

        // 执行相关代码  
        Log.i(TAG, "初始化");  
        //初始化 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(true)//开启后台运行  
                .setUniMPFromRecents(false)  
                .build();  
        DCUniMPSDK.getInstance().initialize(this, config, new IDCUniMPPreInitCallback() {  
            @Override  
            public void onInitFinished(boolean b) {  
                Log.d("unimpaa","onInitFinished----"+b);  
                result.success(b);  
            }  
        });  
        //初始化 uni小程序SDK ----end----------  
    }  

    /**  
     * 初始化安装远程小程序  
     *  
     * @param call 方法调用对象,不能为空  
     * @param result 方法通道返回结果对象,不能为空  
     * @return 无返回值  
     */  

    private void installMP(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {  

        try {  
            // 执行相关代码  
            // 记录一条Verbose级别的日志  
            Log.v(TAG, "初始化小程序");  

            String wgtPath = call.argument("path");  
            String appid = call.argument("appid");  
            Handler uiHandler = new Handler();  
            Log.v(TAG, "初始化小程序,wgtPath:"+wgtPath + "----appid:"+ appid);  

            UniMPReleaseConfiguration uniMPReleaseConfiguration = new UniMPReleaseConfiguration();  
            uniMPReleaseConfiguration.wgtPath = wgtPath;  
            // uniMPReleaseConfiguration.password = "789456123";  

            uiHandler.post(new Runnable() {  
                @Override  
                public void run() {  
                    DCUniMPSDK.getInstance().releaseWgtToRunPath(appid, uniMPReleaseConfiguration, new IUniMPReleaseCallBack() {  
                        @Override  
                        public void onCallBack(int code, Object pArgs) {  
                            if(code == 1) {  
                                //释放wgt完成  
                                Log.i(TAG,"释放wgt完成");  
                                result.success(true);  

                            } else{  
                                //释放wgt失败  
                                Log.e(TAG,"释放wgt失败");  
                                result.success(false);  

                            }  
                        }  
                    });  
                }  
            });  

            // DCUniMPSDK.getInstance().releaseWgtToRunPath(appid, uniMPReleaseConfiguration, new IUniMPReleaseCallBack() {  
            //     @Override  
            //     public void onCallBack(int code, Object pArgs) {  
            //         if(code ==1) {  
            //             //释放wgt完成  
            //             Log.i(TAG,"释放wgt完成");  
            //             result.success(true);  

            //         } else{  
            //             //释放wgt失败  
            //             Log.e(TAG,"释放wgt失败");  
            //             result.success(false);  

            //         }  
            //     }  
            // });  
        } catch (Exception e) {  
            e.printStackTrace();  
            Log.e(TAG,"释放wgt失败2222");  
        }  

    }  
    /**  
     * 打开小程序  
     *  
     * @param call 方法调用对象,不能为空  
     * @param result 方法通道返回结果对象,不能为空  
     * @return 无返回值  
     */  

     private void openMP(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {  
        try {  
            // 执行相关代码  
            // 记录一条Verbose级别的日志  
            Log.v(TAG, "打开小程序");  

            String path = call.argument("path");  
            String appid = call.argument("appid");  
            HashMap extraData = call.argument("extraData");  

            Log.v(TAG, "打开小程序-------" + "path" + path + "----appid:"+ appid + "----extraData:"+ extraData);  

            UniMPOpenConfiguration uniMPOpenConfiguration = new UniMPOpenConfiguration();  

            // if (Objects.nonNull(extraData)) {  
            //     JSONObject par = new JSONObject(extraData);  
            //     uniMPOpenConfiguration.extraData = par;  
            // }  

            // uniMPOpenConfiguration.path = path;  
            // uniMPOpenConfiguration.redirectPath = "pages/index/index";  

            IUniMP uniMP = DCUniMPSDK.getInstance().openUniMP(mContext,appid, uniMPOpenConfiguration);  
            mUniMPCaches.put(uniMP.getAppid(), uniMP);  

            result.success(true);  

        } catch (Exception e) {  
            e.printStackTrace();  
            Log.e(TAG,"释放wgt失败2222");  
            result.success(false);  
        }  

    }  
    /**  
     * 运行目录中是否已经存在小程序资源   
     *  
     * @param call 方法调用对象,不能为空  
     * @param result 方法通道返回结果对象,不能为空  
     * @return 无返回值  
     */  

     private void isExistsApp(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {  
        // 执行相关代码  
        // 记录一条Verbose级别的日志  
        Log.v(TAG, "运行目录中是否已经存在小程序资源 ");  
        result.success(DCUniMPSDK.getInstance().isExistsApp(call.argument("appid")));  
        // result.success(null);  
    }  
    /**  
     * 获取已经部署的小程序应用资源版本信息  
     *  
     * @param call 方法调用对象,不能为空  
     * @param result 方法通道返回结果对象,不能为空  
     * @return 无返回值  
     */  

     private void getUniMPVersionInfoWithAppid(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {  
            // 执行相关代码  
        // 记录一条Verbose级别的日志  
        Log.v(TAG, "获取已经部署的小程序应用资源版本信息");  

        result.success(null);  
    }  
}  
2024-05-27 17:22 负责人:DCloud_App_Array 分享
已邀请:
oko

oko -

只能加个启动页。Android启动远程wgt包白屏

  • 1***@163.com (作者)

    我这边是打开小程序就是白屏,不是白屏一会就正常了

    2024-05-29 18:56

1***@163.com

1***@163.com (作者)

!验证视频

要回复问题请先登录注册