react-native-orientation Android原生实现原理:OrientationModule源码深度剖析
react-native-orientation Android原生实现原理OrientationModule源码深度剖析【免费下载链接】react-native-orientationListen to device orientation changes in react-native and set preferred orientation on screen to screen basis.项目地址: https://gitcode.com/gh_mirrors/re/react-native-orientationreact-native-orientation是一个功能强大的React Native库它允许开发者监听设备方向变化并在不同屏幕上设置首选方向。本文将深入剖析其Android原生实现的核心组件——OrientationModule帮助开发者理解其工作原理和实现细节。OrientationModule类结构解析OrientationModule是react-native-orientation的核心模块位于android/src/main/java/com/github/yamill/orientation/OrientationModule.java。它继承自ReactContextBaseJavaModule并实现了LifecycleEventListener接口这使其能够与React Native框架深度集成并响应应用生命周期事件。构造函数与BroadcastReceiver在构造函数中模块创建了一个BroadcastReceiver实例来监听方向变化public OrientationModule(ReactApplicationContext reactContext) { super(reactContext); final ReactApplicationContext ctx reactContext; receiver new BroadcastReceiver() { Override public void onReceive(Context context, Intent intent) { Configuration newConfig intent.getParcelableExtra(newConfig); Log.d(receiver, String.valueOf(newConfig.orientation)); String orientationValue newConfig.orientation 1 ? PORTRAIT : LANDSCAPE; WritableMap params Arguments.createMap(); params.putString(orientation, orientationValue); if (ctx.hasActiveCatalystInstance()) { ctx .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) .emit(orientationDidChange, params); } } }; ctx.addLifecycleEventListener(this); }这个接收器会在接收到onConfigurationChanged广播时触发将新的方向信息转换为字符串PORTRAIT或LANDSCAPE并通过React Native的事件发射器发送到JavaScript层。核心功能实现方向锁定方法OrientationModule提供了多种方向锁定方法这些方法通过调用Activity的setRequestedOrientation来实现lockToPortrait()锁定为竖屏ReactMethod public void lockToPortrait() { final Activity activity getCurrentActivity(); if (activity null) { return; } activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); }lockToLandscape()锁定为横屏传感器控制ReactMethod public void lockToLandscape() { final Activity activity getCurrentActivity(); if (activity null) { return; } activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); }lockToLandscapeLeft()和lockToLandscapeRight()分别锁定为左横屏和右横屏unlockAllOrientations()解除所有方向锁定使用传感器检测方向获取当前方向getOrientation方法通过读取设备配置信息获取当前方向ReactMethod public void getOrientation(Callback callback) { final int orientationInt getReactApplicationContext().getResources().getConfiguration().orientation; String orientation this.getOrientationString(orientationInt); if (orientation null) { callback.invoke(orientationInt, null); } else { callback.invoke(null, orientation); } }getOrientationString方法将Android的方向常量转换为可读性更好的字符串private String getOrientationString(int orientation) { if (orientation Configuration.ORIENTATION_LANDSCAPE) { return LANDSCAPE; } else if (orientation Configuration.ORIENTATION_PORTRAIT) { return PORTRAIT; } else if (orientation Configuration.ORIENTATION_UNDEFINED) { return UNKNOWN; } else { return null; } }生命周期管理OrientationModule实现了LifecycleEventListener接口以在应用生命周期变化时管理BroadcastReceiver的注册与注销Override public void onHostResume() { final Activity activity getCurrentActivity(); if (activity null) { FLog.e(ReactConstants.TAG, no activity to register receiver); return; } activity.registerReceiver(receiver, new IntentFilter(onConfigurationChanged)); } Override public void onHostPause() { final Activity activity getCurrentActivity(); if (activity null) return; try { activity.unregisterReceiver(receiver); } catch (java.lang.IllegalArgumentException e) { FLog.e(ReactConstants.TAG, receiver already unregistered, e); } }这确保了在应用暂停时不会接收方向变化事件避免资源浪费和潜在的内存泄漏。JavaScript桥接层JavaScript层通过index.js文件与原生模块交互提供了简洁的API供React Native开发者使用module.exports { getOrientation(cb) { Orientation.getOrientation((error,orientation) { cb(error, orientation); }); }, lockToPortrait() { Orientation.lockToPortrait(); }, lockToLandscape() { Orientation.lockToLandscape(); }, // 其他方法... addOrientationListener(cb) { var key getKey(cb); listeners[key] DeviceEventEmitter.addListener(orientationDidChangeEvent, (body) { cb(body.orientation); }); } }包注册OrientationPackage类负责将OrientationModule注册到React Native应用中位于android/src/main/java/com/github/yamill/orientation/OrientationPackage.javapublic class OrientationPackage implements ReactPackage { Override public ListNativeModule createNativeModules(ReactApplicationContext reactContext) { return Arrays.NativeModuleasList( new OrientationModule(reactContext) ); } // 其他方法... }总结react-native-orientation的Android实现通过OrientationModule巧妙地连接了Android原生API和React Native框架。它利用BroadcastReceiver监听方向变化通过ReactMethod暴露原生功能给JavaScript层并通过LifecycleEventListener管理组件生命周期确保了高效可靠的方向检测和控制功能。理解这一实现原理有助于开发者更好地使用该库并为自定义React Native原生模块提供了参考范例。要开始使用react-native-orientation你可以通过以下命令克隆仓库git clone https://gitcode.com/gh_mirrors/re/react-native-orientation然后按照项目文档进行安装和配置即可在你的React Native应用中轻松实现方向控制功能。【免费下载链接】react-native-orientationListen to device orientation changes in react-native and set preferred orientation on screen to screen basis.项目地址: https://gitcode.com/gh_mirrors/re/react-native-orientation创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考