检查是否为FCM启用了Google Play服务

时间:2017-10-19 16:10:42

标签: android google-play-services firebase-cloud-messaging

如果我想使用FCM,是否有必要检查Google Play服务是否已启用?我的应用程序必须支持OS 4.0及更高版本。它将仅通过Play商店进行分发,因此我认为默认情况下应在用户处启用Google Play服务。设备。

2 个答案:

答案 0 :(得分:0)

文档说:

  

FCM客户端也需要运行Android 4.0或更高版本的设备   已安装Google Play商店应用,或正在运行模拟器   带有Google API的Android 4.0。

此外:

  

依赖Play Services SDK的应用应始终检查设备   在访问Google Play之前获取兼容的Google Play服务APK   服务功能。建议在两个地方执行此操作:在   主要活动的onCreate()方法,以及onResume()方法。该   check in onCreate()确保在没有a的情况下无法使用该应用   成功检查。检查onResume()确保如果用户   通过其他方式返回正在运行的应用程序,例如通过   后退按钮,仍然执行检查。

所以是的,您应该始终检查Google Play服务,但如果您通过Play商店分发您的应用程序,则可以安全地说已经安装了Play服务。

答案 1 :(得分:0)

Play服务有不同的状态。您可以查看ConnectionResult.SUCCESS。确切地说,您可以使用

检查GPS的状态
     private void checkPlayService() {
         int isGPSAvailable = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(MainActivity.this);

            Toast.makeText(this,"isGPSAvailable " + isGPSAvailable,Toast.LENGTH_SHORT).show();
            switch (isGPSAvailable)
            {
                case ConnectionResult.API_UNAVAILABLE:
                    //API is not available
                    break;
                case ConnectionResult.NETWORK_ERROR:
                    //Network error while connection
                    break;
                case ConnectionResult.RESTRICTED_PROFILE:
                    //Profile is restricted by google so can not be used for play services
                    break;
                case ConnectionResult.SERVICE_MISSING:
                    //service is missing
                    break;
                case ConnectionResult.SIGN_IN_REQUIRED:
                    //service available but user not signed in
                    break;   case ConnectionResult.SERVICE_INVALID:
                    //  The version of the Google Play services installed on this device is not authentic
                    break;
                case ConnectionResult.SUCCESS:
                    break;
            }
}
相关问题