HarmonyOS鸿蒙Next中RN侧调用原生方法禁止截屏

HarmonyOS鸿蒙Next中RN侧调用原生方法禁止截屏 RN侧调用鸿蒙通讯已经实现。

如何实现调用鸿蒙的原生方法实现RN侧调用页面禁止截屏。

或者给出这个示例的DEMO

3 回复
  1. RN侧通过TurboModule来调用鸿蒙的方法
    https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/use-napi-process-V5

  2. 鸿蒙页面截屏:

    先参考以下步骤和demo:

    1. 在module.json5文件中声明需要使用的 ohos.permission.PRIVACY_WINDOW 权限,参考文档:
      https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/declare-permissions-V5#%E5%9C%A8%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E4%B8%AD%E5%A3%B0%E6%98%8E%E6%9D%83%E9%99%90
"requestPermissions": [
  {
    "name" : "ohos.permission.PRIVACY_WINDOW"
  }
]
2. 示例代码
// windowUtils.ets
import window from '@ohos.window';
import common from '@ohos.app.ability.common';
export class windowUtils {
  static setWindowPrivacyModeInPage(context: common.UIAbilityContext,isFlag: boolean) {
    window.getLastWindow(context).then((lastWindow)=>{
      lastWindow.setWindowPrivacyMode(isFlag);
    })
  }
}
//页面
import common from '@ohos.app.ability.common';
import { windowUtils } from '../common/windowUtils';
@Entry
@Component
struct Index3 {
  @State message: string = 'Hello World';
  onPageShow(): void {
    windowUtils.setWindowPrivacyModeInPage(getContext(this) as common.UIAbilityContext, true);
  }
  onPageHide() {
    windowUtils.setWindowPrivacyModeInPage(getContext(this) as common.UIAbilityContext,false);
  }
  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .height('100%')
  }
}

更多关于HarmonyOS鸿蒙Next中RN侧调用原生方法禁止截屏的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,React Native(RN)侧调用原生方法实现禁止截屏功能,可以通过以下步骤实现:

  1. 创建原生模块:首先,在原生代码中创建一个模块,用于控制截屏功能。在鸿蒙OS中,可以使用AbilityService来实现。通过调用Window类的setFlags方法,设置WindowManager.LayoutParams.FLAG_SECURE标志,可以禁止截屏。

  2. 暴露方法给RN:通过@ReactMethod注解,将原生模块中的方法暴露给React Native。例如,创建一个名为disableScreenshot的方法,供RN调用。

  3. 在RN中调用原生方法:在React Native中,使用NativeModules来调用暴露的原生方法。通过NativeModules.YourModuleName.disableScreenshot(),即可触发原生代码中的禁止截屏逻辑。

  4. 权限处理:确保在config.json中声明必要的权限,如ohos.permission.SCREEN_CAPTURE,以确保应用能够正常执行禁止截屏的操作。

通过以上步骤,可以在HarmonyOS鸿蒙Next中实现RN侧调用原生方法禁止截屏的功能。

在HarmonyOS鸿蒙Next中,React Native(RN)侧调用原生方法禁止截屏,可以通过以下步骤实现:

  1. 创建原生模块:在原生代码中创建一个模块,使用ScreenCaptureAbility类来禁止截屏。
  2. 暴露方法给RN:通过@ReactMethod注解暴露一个方法给RN侧调用。
  3. RN侧调用:在RN侧通过NativeModules调用暴露的原生方法。

示例代码:

// 原生模块
public class ScreenCaptureModule extends ReactContextBaseJavaModule {
    public ScreenCaptureModule(ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    public String getName() {
        return "ScreenCaptureModule";
    }

    @ReactMethod
    public void disableScreenCapture() {
        getCurrentActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
    }
}
// RN侧调用
import { NativeModules } from 'react-native';
NativeModules.ScreenCaptureModule.disableScreenCapture();

通过这种方式,RN侧可以调用原生方法实现禁止截屏功能。

回到顶部