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

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

问题描述

我正在尝试桥接Android功能,以保持屏幕显示在React Native上.我以为我可以用一个简单的模块来做到这一点,但是我不知道如何从所述模块访问当前的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.

我需要活动参考,因此可以在其上调用.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();


更新本机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天全站免登陆