如何在React Native/Expo安卓应用中打开已安装的自研APK
问题解答
完全可以在无需联网、无需跳转谷歌Play商店的前提下实现该需求,核心原理是安卓系统支持通过包名和指定Activity的Intent唤起已安装的第三方应用,具体实现方案分场景说明:
一、前置准备
你需要提前拿到自研APK的两个核心信息:
- 应用的包名(示例格式:
com.yourcompany.selfdeveloped.app) - 要唤起的入口Activity全路径(如果是默认启动页一般为
包名.MainActivity,也可以在自研APK侧自定义专属的唤起Action,避免写错Activity路径)
二、纯React Native(裸开发)实现步骤
- 可直接用
react-native-send-intent包简化原生Intent交互操作:- 安装命令:
npm i react-native-send-intent --save - 安卓11及以上系统要求应用主动声明要查询的包名,否则无法识别已安装应用,需要在当前RN应用的
AndroidManifest.xml中添加如下配置:
<queries> <package android:name="com.yourcompany.selfdeveloped.app" /> </queries> - 安装命令:
- 按钮点击事件代码示例:
import SendIntentAndroid from 'react-native-send-intent'; const openSelfDevelopedApk = async () => { // 先判断目标应用是否已安装 const isInstalled = await SendIntentAndroid.isAppInstalled('com.yourcompany.selfdeveloped.app'); if (isInstalled) { // 直接唤起目标应用 SendIntentAndroid.openApp('com.yourcompany.selfdeveloped.app'); } else { // 可自定义未安装的提示逻辑 console.log('目标应用未安装'); } }
三、Expo开发实现步骤
Expo环境无需预编译原生代码,使用官方提供的expo-intent-launcher包即可实现:
- 安装依赖:
npx expo install expo-intent-launcher expo-modules-core - 同样需要在
app.json中配置安卓包查询权限,适配安卓11及以上系统:"android": { "queries": { "packages": ["com.yourcompany.selfdeveloped.app"] } } - 按钮点击事件代码示例:
import * as IntentLauncher from 'expo-intent-launcher'; import { Alert } from 'react-native'; const openSelfDevelopedApk = async () => { try { await IntentLauncher.startActivityAsync( IntentLauncher.ActivityAction.MAIN, { packageName: 'com.yourcompany.selfdeveloped.app', className: 'com.yourcompany.selfdeveloped.app.MainActivity' } ); } catch (e) { Alert.alert('提示', '目标应用未安装在当前设备中'); } }
注意事项
- 如果需要在两个应用间传递参数,可以在Intent中额外添加
extra字段传递数据,全程不需要任何网络请求 - 如果希望自研APK不被其他任意应用唤起,可以在自研APK的
AndroidManifest.xml中给目标Activity添加自定义权限,只有你的RN/Expo应用申请了对应权限才能唤起,可自行控制安全性
内容的提问来源于stack exchange,提问作者Indunil Girihagama
相关产品推荐
相关产品推荐

