You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native Expo应用:实现Play商店安装后自动返回主应用

React Native Expo实现Play商店安装后自动返回主应用方案

核心思路

通过配置应用深层链接 + 使用Android Intent指定返回逻辑,结合AppState监听实现安装完成后自动返回并续接流程。

步骤实现

1. 配置应用深层链接(app.json)

确保应用能被系统唤起,在app.json中添加scheme和intentFilters:

{
  "expo": {
    "scheme": "yourappunique", // 自定义唯一scheme,比如你的应用包名前缀
    "android": {
      "intentFilters": [
        {
          "action": "VIEW",
          "data": [
            {
              "scheme": "yourappunique",
              "host": "main"
            }
          ],
          "category": ["BROWSABLE", "DEFAULT"]
        }
      ]
    }
  }
}

2. 使用IntentLauncher打开Play商店(带返回逻辑)

利用expo-intent-launcher发送带返回参数的Intent,替代直接用Linking.openURL:

import * as IntentLauncher from 'expo-intent-launcher';
import * as Linking from 'expo-linking';

const openPlayStore = async (targetPackage) => {
  try {
    // 生成返回当前应用的深层链接
    const returnLink = await Linking.createURL('/');
    
    // 构建打开Play商店的Intent
    const playStoreIntent = {
      action: 'android.intent.action.VIEW',
      data: `market://details?id=${targetPackage}`,
      flags: 0x00000010, // FLAG_ACTIVITY_CLEAR_TOP:确保返回原应用实例
      extras: {
        'android.intent.extra.RETURN_RESULT': true,
        'android.intent.extra.REFERRER': returnLink
      }
    };

    await IntentLauncher.startActivityAsync(playStoreIntent);
  } catch (err) {
    //  fallback到网页版Play商店(无法自动返回)
    await Linking.openURL(`https://play.google.com/store/apps/details?id=${targetPackage}`);
  }
};

3. 监听AppState自动续接流程

当应用从后台回到前台时,自动重新检查第三方应用安装状态:

import { AppState } from 'react-native';
import { useEffect } from 'react';

// 在组件中添加监听
useEffect(() => {
  const handleAppActive = (nextState) => {
    if (nextState === 'active') {
      // 调用你的应用安装状态检查逻辑
      checkTargetAppInstalled();
    }
  };

  const appStateSub = AppState.addEventListener('change', handleAppActive);
  return () => appStateSub.remove();
}, []);

注意事项

  • 仅支持Android平台:iOS App Store不提供安装后自动返回的机制,只能引导用户手动返回
  • 测试需构建正式包:Expo Go无法完整支持intent filter配置,需使用npx expo run:android或构建APK/AB测试
  • scheme需唯一:避免与其他应用的scheme冲突,建议使用应用包名作为scheme前缀

内容的提问来源于stack exchange,提问作者Bloom Flora

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 11:57:08