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

React Native与Expo:检测Android设备是否安装Google Play服务

检测Android设备Google Play服务的可行方案

Expo 环境下的官方方案

用Expo官方维护的expo-google-play-services包就能直接实现检测,步骤如下:

  1. 安装依赖:
    npx expo install expo-google-play-services
    
  2. 编写检测逻辑:
    import { isGooglePlayServicesAvailableAsync, GooglePlayServicesStatus } from 'expo-google-play-services';
    
    async function checkPlayServicesStatus() {
      const status = await isGooglePlayServicesAvailableAsync();
      if (status === GooglePlayServicesStatus.AVAILABLE) {
        // Google Play服务正常,可安全调用expo-location的getCurrentPositionAsync
        console.log('Google Play服务可用');
      } else {
        // 服务缺失/过期/禁用,避免调用依赖它的定位方法
        console.log('Google Play服务不可用');
      }
    }
    

这个包是Expo官方维护的,兼容性和稳定性都有保障,完全替代你找到的那个停止维护的旧包。

裸React Native项目方案

如果是脱离Expo的裸RN项目,有两种靠谱的方式:

1. 自定义原生模块(最灵活)

直接在Android端调用Google官方API,封装成RN可调用的方法:

  • 在Android项目的Kotlin代码中添加检测逻辑:
    import com.google.android.gms.common.GoogleApiAvailability
    
    // 封装成React Native可调用的方法
    @ReactMethod
    fun checkGooglePlayServices(promise: Promise) {
        val apiAvailability = GoogleApiAvailability.getInstance()
        val resultCode = apiAvailability.isGooglePlayServicesAvailable(reactApplicationContext)
        promise.resolve(resultCode == com.google.android.gms.common.ConnectionResult.SUCCESS)
    }
    
  • 按照RN原生模块的规范,将这个方法暴露给JS端,之后就能在JS里异步调用判断状态。

2. 使用维护中的第三方库

可以选用react-native-google-play-services(注意和你提到的旧包不是同一个),这个库仍在维护,提供了现成的检测方法:

import GooglePlayServices from 'react-native-google-play-services';

async function checkPlayServices() {
  const isAvailable = await GooglePlayServices.isAvailable();
  if (isAvailable) {
    // 执行定位相关操作
  } else {
    // 处理服务不可用的情况
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 13:52:11