如何从 React Native Android 模块访问 Activity? [英] How to access Activity from a React Native Android module?

查看:24
本文介绍了如何从 React Native Android 模块访问 Activity?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试桥接将屏幕保持在 React Native 上的 Android 功能.我想我可以用一个简单的模块来做到这一点,但是我不知道如何从该模块访问当前的 Android Activity.

I'm attempting to bridge over the Android functionality of keeping the screen on to React Native. I figured I could do this with a simple module, however I don't know how to get access to the current Android Activity from said module.

我需要 Activity 引用以便我可以调用 .getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

I need the Activity reference so I can call .getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); on it

我试图通过像这样的转换来获取活动 ((Activity)getReactApplicationContext().getBaseContext()),但这会引发无法转换为 Android.app.Activity"错误

I tried to get the activity via casting like so ((Activity)getReactApplicationContext().getBaseContext()), but this throws a "cannot be cast to Android.app.Activity" error

推荐答案

CustomReactPackage.java:

public class CustomReactPackage implements ReactPackage {

    private Activity mActivity = null;

    public CustomReactPackage(Activity activity) {
        mActivity = activity;
    }

    @Override
    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
        List<NativeModule> modules = new ArrayList<>();
        // Add native modules here
        return modules;
    }

    public List<Class<? extends JavaScriptModule>> createJSModules() {
        return Collections.emptyList();
    }

    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
        List<ViewManager> modules = new ArrayList<>();
        // Add native UI components here
        modules.add(new LSPlayerManager(mActivity));
        return modules;
    }
}

LSPlayerManager 是我的原生 UI 组件.我定义了一个构造函数,以便我可以传入活动:

LSPlayerManager is my native UI component. I define a constructor so that I can pass in the activity:

public LSPlayerManager(Activity activity) {
    mActivity = activity;
}

最后在定义了 ReactInstanceManagerMainActivity.java 中,我们可以将活动传递给我们的自定义 React 包:

And finally in MainActivity.java where the ReactInstanceManager is defined, we can pass the activity to our custom React package:

mReactInstanceManager = ReactInstanceManager.builder()
        .setApplication(getApplication())
        .setBundleAssetName("index.android.bundle")
        .setJSMainModuleName("src/index.android")
        .addPackage(new MainReactPackage())
        .addPackage(new CustomReactPackage(this)) // <--- LIKE THIS!
        .setUseDeveloperSupport(BuildConfig.DEBUG)
        .setInitialLifecycleState(LifecycleState.RESUMED)
        .build();

<小时>

React NATIVE 0.29.0 的更新

这不再是您访问本机模块中的活动的方式.见 https://github.com/facebook/react-native/releases/tag/v0.29.0 迁移说明

This is no longer how you access activity in a native module. See https://github.com/facebook/react-native/releases/tag/v0.29.0 for migration instructions

这篇关于如何从 React Native Android 模块访问 Activity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆