React Native Android跨应用传值:从App1启动App2并传递文本数据
解决方案:React Native跨应用启动与传值
针对你的问题,我分两部分来解答:首先是如何将Intent中的数据传递到App2的React Native页面,然后分享几种无需手动编写大量Intent原生代码的简洁方案。
一、将Intent数据传递到App2的RN页面
你已经通过Intent启动了App2的空Activity,接下来只需要在这个Activity里把数据通过React Native的桥接机制传给RN端,有两种常用方式:
1. 通过发送原生事件传递数据
在App2的目标Activity中,获取Intent携带的数据,等到React Native实例初始化完成后,发送一个自定义事件给RN端:
// App2的MainActivity.java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 从Intent中取出传递的文本数据 String receivedText = getIntent().getStringExtra("APP1_SEND_TEXT"); // 监听React Native实例初始化完成事件 getReactInstanceManager().addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() { @Override public void onReactContextInitialized(ReactContext reactContext) { // 发送事件到RN端 reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) .emit("appLaunchedWithData", receivedText); // 移除监听,避免重复触发 getReactInstanceManager().removeReactInstanceEventListener(this); } }); }
然后在App2的RN页面中监听这个事件:
// App2的某个RN组件 import { DeviceEventEmitter, useEffect } from 'react-native'; function HomeScreen() { useEffect(() => { // 注册事件监听 const subscription = DeviceEventEmitter.addListener( 'appLaunchedWithData', (textData) => { console.log("从App1收到的数据:", textData); // 在这里处理数据,比如展示到页面上 } ); // 组件卸载时移除监听 return () => subscription.remove(); }, []); return (/* 页面内容 */); }
2. 通过初始Props传递数据
如果数据只需要在RN应用启动时初始化使用,可以把数据作为初始props传给RN根组件:
// App2的MainActivity.java @Override protected ReactRootView createRootView() { ReactRootView reactRootView = new ReactRootView(this); Bundle initialProps = new Bundle(); // 取出Intent中的数据 String receivedText = getIntent().getStringExtra("APP1_SEND_TEXT"); initialProps.putString("launchedText", receivedText); // 启动RN应用并传入初始props reactRootView.startReactApplication(getReactInstanceManager(), "App2", initialProps); return reactRootView; }
在RN根组件中直接获取props:
// App2的根组件 function App(props) { useEffect(() => { if (props.launchedText) { console.log("启动时收到的数据:", props.launchedText); } }, [props.launchedText]); return (/* 应用内容 */); }
二、无需手动Intent的简洁跨应用传值方案
如果想避免编写过多原生Intent代码,以下两种方案更适合React Native场景:
1. 深度链接(Deep Linking)
通过自定义URL Scheme启动App2并传递参数,RN层面就能完成大部分逻辑,同时支持iOS和Android。
步骤1:配置App2的深度链接
在App2的AndroidManifest.xml中添加Intent Filter:
<activity android:name=".MainActivity" android:launchMode="singleTask"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <!-- 自定义Scheme,比如app2 --> <data android:scheme="app2" /> </intent-filter> </activity>
步骤2:App1中通过深度链接启动App2
// App1的RN代码 import { Linking } from 'react-native'; const openApp2 = async () => { // 拼接带参数的链接 const deepLinkUrl = "app2://open?text=Hello%20from%20App1"; const canOpen = await Linking.canOpenURL(deepLinkUrl); if (canOpen) { await Linking.openURL(deepLinkUrl); } else { console.log("无法打开App2,请确保App2已安装"); } };
步骤3:App2中获取参数
// App2的RN代码 import { Linking, useEffect } from 'react-native'; function HomeScreen() { useEffect(() => { // 获取启动时的初始链接 const getInitialParams = async () => { const url = await Linking.getInitialURL(); if (url) { const params = new URLSearchParams(url.split('?')[1]); const text = params.get('text'); console.log("从App1收到的文本:", text); } }; getInitialParams(); // 监听App从后台被唤醒时的链接 const subscription = Linking.addEventListener('url', (event) => { const params = new URLSearchParams(event.url.split('?')[1]); const text = params.get('text'); console.log("后台唤醒时收到的文本:", text); }); return () => subscription.remove(); }, []); return (/* 页面内容 */); }
2. 系统分享API
如果只是传递简单文本,利用React Native的Share模块可以快速实现,无需编写原生Intent代码。
App1中分享文本到App2
// App1的RN代码 import Share from 'react-native-share'; const shareToApp2 = async () => { try { await Share.share({ message: "Hello from App1" }, { dialogTitle: "分享到App2", // 可选:过滤掉其他不需要的分享选项 excludedActivityTypes: ['com.android.bluetooth'] }); } catch (error) { console.log("分享失败:", error); } };
App2中配置接收分享
在App2的AndroidManifest.xml中添加接收分享的Intent Filter:
<activity android:name=".MainActivity" android:launchMode="singleTask"> <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/plain" /> </intent-filter> </activity>
然后App2可以通过之前提到的Intent数据传递方式,把分享的文本拿到RN端处理。
总结
- 如果你已经实现了Intent启动,通过原生桥接(事件/初始Props)就能把数据传到RN页面;
- 追求简洁跨平台方案的话,深度链接是最优选择,RN层面代码统一,无需大量原生开发;
- 简单文本传递也可以用系统分享API,实现成本更低。
内容的提问来源于stack exchange,提问作者anbu selvan
相关产品推荐
相关产品推荐

