优化文本乱码问题 2541/2 2541/head 2542/target/1
authorxue <2144439972@qq.com>
Thu, 16 Oct 2025 09:23:01 +0000 (17:23 +0800)
committerxue <2144439972@qq.com>
Thu, 16 Oct 2025 09:23:01 +0000 (17:23 +0800)
code/sdk/application/wearable/ln/nativeapp/nativeui/uiservice/src/ui_service.cpp

index 0449d59..d8e4c80 100644 (file)
@@ -570,9 +570,39 @@ void HandBindAndUnbindNotify(Request *request)
 // 更新文本内容通知
 void HandTextUpdateNotify(Request *request)
 {
-    std::string value(static_cast<char*>(request->data), request->len);
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_UI_SERVICE, "HandTextUpdateNotify msg_id = %d, len = %d", request->msgId, request->len);
+    
+    // 检查数据有效性
+    if (request->data == nullptr || request->len <= 0) {
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_UI_SERVICE, "HandTextUpdateNotify: invalid data or length");
+        return;
+    }
+    
+    // 确保字符串以null结尾,避免乱码
+    std::string value;
+    if (request->len > 0) {
+        const char* data = static_cast<const char*>(request->data);
+        if (data[request->len - 1] == '\0') {
+            value = std::string(data, request->len - 1);
+        } else {
+            value = std::string(data, request->len);
+        }
+    }
+    
+    // 验证字符串是否包含有效字符
+    if (value.empty() || value.find_first_not_of(" \t\n\r") == std::string::npos) {
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_UI_SERVICE, "HandTextUpdateNotify: text string is empty or contains only whitespace");
+        return;
+    }
+    
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_UI_SERVICE, "HandTextUpdateNotify text = %s", value.c_str());
+    
+    // 创建字符串副本,避免const_cast问题
+    std::string textCopy = value;
     GraphicService::GetInstance()->PostGraphicEvent(
-        std::bind(&OHOS::ThunderBirdModel::SetTextStr,OHOS::ThunderBirdModel::GetInstance(),const_cast<char*>(value.c_str())));
+        [textCopy]() {
+            OHOS::ThunderBirdModel::GetInstance()->SetTextStr(const_cast<char*>(textCopy.c_str()));
+        });
 }
 
 // 下载文件通知