Google Play services: How to handle devices that do not have Google Play?

久未见 提交于 2019-11-27 20:30:47

问题


Google Play services is an Android library whose goal is to provide:

  • OAuth 2.0 authentication
  • Google+ sign-in
  • Google+ +1 button
  • various other goodies

If I were to use it (for instance because I want Google+ sign-in), what would happen to users whose device does not have Google Play? (Nook, Cyanogenmod, China Mobile, old devices, maybe Huawei?, etc)

QUESTION: Will my app become incompatible with such devices? Will it be displayed as compatible but then crash, or not work?
Is there a best practice to keep this in mind when using Google Play services?


回答1:


GooglePlayServicesUtil.isGooglePlayServicesAvailable(android.content.Context)

is deprecated!

Use:

 GoogleApiAvailability api = GoogleApiAvailability.getInstance();
        int code = api.isGooglePlayServicesAvailable(activity);
        if (code == ConnectionResult.SUCCESS) {
           // Do Your Stuff Here
        } else {
           AlertDialog alertDialog =
                 new AlertDialog.Builder(activity, R.style.AppCompatAlertDialogStyle).setMessage(
                       "You need to download Google Play Services in order to use this part of the application")
                       .create();
           alertDialog.show();
        }



回答2:


If the feature from Google Play Services is essential for your app there would be no way to get your App working.

You can check if the services are enabled from within your app with GooglePlayServicesUtil.isGooglePlayServicesAvailable(android.content.Context)
which returns ConnectionResult.SUCCESS if Play Services is available.

You can now try to convince the user to install it (if possible) or disable the feature that is using the service.

As the Google Play Services is not a feature declared in the manifest your app should install fine on any device but may crash later on if you are using the APIs without checking if they are available.

You can try the behaviour with the emulator. Just create an AVD without the Google APIs and put your App on it.




回答3:


If you are somehow required to use Play Services, or if you maintain a legacy app that makes calls to Play Services, then I would recommend this strategy:

  1. On app start, check whether Play Services is available or not
  2. If not available, redirect Play Services calls to microG

microG is an open source implementation of Google Play Services.
It lacks many features, but is under active development. Many features are still stubs.

For location services, there is also LOST, a drop-in replacement for the Google Play services location APIs.

You app might not work perfectly, but at least it is better than crashing.

Of course, the best is to NOT use Google Play Services, from the start.



来源:https://stackoverflow.com/questions/13191875/google-play-services-how-to-handle-devices-that-do-not-have-google-play

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!