DEV Community

HarmonyOS
HarmonyOS

Posted on

Single framework - FA - The wearengine.ping() method blocks application startup. A black screen occurs for about 2s during application startup

Read the original article:Single framework - FA - The wearengine.ping() method blocks application startup. A black screen occurs for about 2s during application startup

Question

The wearengine.ping() method blocks application startup. A black screen occurs for about 2s during application startup.

Short Answer

Invoke the wearengine.ping() method in onShow and create a transition interface during loading. The transition interface during loading is displayed first, and then the ping result interface is displayed after onPingResult is called back.

<stack style="width: 100%; height: 100%;" onswipe="touchMove">
    <div if="{{ welcome }}">
        <image style="width: 100%; height: 100%;" src="/common/loading.jpg"></image>
    </div>
    <div if="{{ !welcome }}" class="container">
           ......
    </div>
</stack>

onShow(){
        this.openWearEngine();
    }
openWearEngine() {
    this.p2pClient = new P2pClient();
    this.p2pClient.setPeerPkgName(PHONE_COMPANION_APP_PACKAGE);
    this.p2pClient.setPeerFingerPrint(PHONE_COMPANION_APP_FINGERPRINT);
    let that = this    
    this.p2pClient.ping({
            onSuccess: function() {
            },
            onFailure: function() {
            },
            onPingResult: function(resultCode) {
                console.info("onPingResult() result: " + resultCode.code);
                that.welcome = false
                switch (resultCode.code) {
                    case 205: // 205 the app has been installed on the phone

                        break;
                    case 204: // 204 phone app has NOT been installed

                    default:
                }
            },
        });

this.weReceiver = {
    onSuccess: function () {
        // Process the callback function returned when messages or files have been received from the phone during registration.
    },
    onFailure: function () {
        // Process the callback function returned when messages or files fail to be received from the phone during registration.
    },
    onReceiveMessage: function (data) {

        if (data) {
            if (data.isFileType) {

            } else {
                     // parse text message
            }
        }
    },
};
this.p2pClient.registerReceiver(this.weReceiver);
}
Enter fullscreen mode Exit fullscreen mode

Applicable Scenarios

  • When developers integrate WearEngine into their HarmonyOS application and notice a startup delay caused by the wearengine.ping() method.
  • When applications show a black screen for 1–2 seconds during startup due to synchronous blocking calls.

Written by Aycanur Ucar

Top comments (0)