Android Code Snippet Check and install Google Play Services

Discussion in 'Code Snippets' started by Erel, Oct 9, 2017.

Thread Status:
Not open for further replies.
  1. Erel

    Erel Administrator Staff Member Licensed User

    This code will check that an updated version of Google Play Services is installed on the device. If not then it will try to help the user install it.

    Code:
    Sub CheckForGooglePlayServices As Boolean
       
    Dim GoogleApiAvailablity As JavaObject
       GoogleApiAvailablity = GoogleApiAvailablity.InitializeStatic(
    "com.google.android.gms.common.GoogleApiAvailability").RunMethod("getInstance"Null)
       
    Dim context As JavaObject
       context.InitializeContext
       
    If GoogleApiAvailablity.RunMethod("isGooglePlayServicesAvailable"Array(context)) <> 0 Then
         GoogleApiAvailablity.RunMethod(
    "makeGooglePlayServicesAvailable"Array(context))
         
    Return False
       
    End If
       
    Return True
    End Sub
    You should also add:
    Code:
    #AdditionalJar: com.google.android.gms:play-services-base
     
    Last edited: Feb 5, 2018
    hatzisn, arastoo, iz0ndg and 8 others like this.
  2. KMatle

    KMatle Expert Licensed User

    I'm getting

    Code:
    java.lang.ClassNotFoundException: com.google.android$gms$common$GoogleApiAvailability
    Any #AdditionalRes parm required? (did not find a proper path in the SDK folder like C:\Android-SDK\extras\google\google_play_services\...)
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    No.

    This code is only relevant when you are using a library based on Google Play Services such as Google Maps.

    This path is not relevant any more. Google Play Services is part of Google Repository now.

    https://www.b4x.com/android/forum/threads/80090/#content
     
    arastoo likes this.
  4. arastoo

    arastoo Active Member Licensed User


    Hello
    How can I fix this problem?
    Can you guide me more accurately?
    Thanks

    Code:
    Error occurred on line: 120 (MasterActivity)
    java.lang.ClassNotFoundException: com.google.android$gms$common$GoogleApiAvailability
        at anywheresoftware.b4j.object.JavaObject.getCorrectClassName(
    JavaObject.java:288)
        at anywheresoftware.b4j.object.JavaObject.InitializeStatic(
    JavaObject.java:74)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at anywheresoftware.b4a.shell.Shell.runMethod(
    Shell.java:710)
        at anywheresoftware.b4a.shell.Shell.raiseEventImpl(
    Shell.java:339)
        at anywheresoftware.b4a.shell.Shell.raiseEvent(
    Shell.java:249)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:
    139)
        at tablet.group.italian.verb.masteractivity.afterFirstLayout(masteractivity.java:
    102)
        at tablet.group.italian.verb.masteractivity.access$
    000(masteractivity.java:17)
        at tablet.group.italian.verb.masteractivity$WaitForLayout.run(masteractivity.java:
    80)
        at android.os.Handler.handleCallback(Handler.java:
    733)
        at android.os.Handler.dispatchMessage(Handler.java:
    95)
        at android.os.Looper.loop(Looper.java:
    136)
        at android.app.ActivityThread.main(ActivityThread.java:
    5021)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
    827)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    643)
        at dalvik.system.NativeStart.main(Native Method)
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    arastoo likes this.
  6. arastoo

    arastoo Active Member Licensed User


    Hi Erel
    Thank you for your guidance.:)
    All sections have been updated but there is an error !
     

    Attached Files:

    • 1.PNG
      1.PNG
      File size:
      55.6 KB
      Views:
      395
    • 2.PNG
      2.PNG
      File size:
      84.1 KB
      Views:
      376
    • 3.PNG
      3.PNG
      File size:
      29.5 KB
      Views:
      396
  7. Erel

    Erel Administrator Staff Member Licensed User

    Use B4A Sdk Manager to install the latest versions of all recommended components.
     
    arastoo likes this.
  8. arastoo

    arastoo Active Member Licensed User

    Hi Erel

    There are only 2 items to download in the B4A Sdk Manager.
    And B4A Sdk Manager has an error to download.


    Code:
    Download: https://dl.google.com/dl/android/maven2/master-index.xml
    Download: https://dl.google.com/dl/android/maven2/com/android/support/test/orchestrator/
    1.0.1/orchestrator-1.0.1.pom
    Download: https://dl.google.com/dl/android/maven2/com/android/support/test/orchestrator/
    1.0.1/orchestrator-1.0.1.jar

    <!DOCTYPE html>
    <html lang=en>
      <meta charset=utf-
    8>
      <meta name=viewport content=
    "initial-scale=1, minimum-scale=1, width=device-width">
      <title>Error 
    404 (Not Found)!!1</title>
      <style>
        *{margin:
    0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/logos/errorpage/error_logo-150x54.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/logos/errorpage/error_logo-150x54-2x.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/logos/errorpage/error_logo-150x54-2x.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/logos/errorpage/error_logo-150x54-2x.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
      </style>
      <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
      <p><b>404.</b> <ins>That’s an error.</ins>
      <p>  <ins>That’s all we know.</ins>

    Download: https://dl.google.com/dl/android/maven2/com/android/support/test/services/test-services/1.0.1/test-services-1.0.1.pom
    Download: https://dl.google.com/dl/android/maven2/com/android/support/test/services/test-services/1.0.1/test-services-1.0.1.jar

    <!DOCTYPE html>
    <html lang=en>
      <meta charset=utf-8>
      <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
      <title>Error 404 (Not Found)!!1</title>
      <style>
        *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/logos/errorpage/error_logo-150x54.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/logos/errorpage/error_logo-150x54-2x.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/logos/errorpage/error_logo-150x54-2x.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/logos/errorpage/error_logo-150x54-2x.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
      </style>
      <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
      <p><b>404.</b> <ins>That’s an error.</ins>
      <p>  <ins>That’s all we know.</ins>
     

    Attached Files:

    • 11.PNG
      11.PNG
      File size:
      46.2 KB
      Views:
      299
    • 2222.PNG
      2222.PNG
      File size:
      91.8 KB
      Views:
      291
  9. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this in the questions forum. Make sure not to post the code as bold.
     
  10. Paolo Fiaschi

    Paolo Fiaschi Member Licensed User

    Hi, this code is equivalento to mine?

    Code:
    If MapFragment1.IsGooglePlayServicesAvailable = False Then
        
    ToastMessageShow("Please install Google Play Services."True)
    End If
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    No. It also tries to install it if it is not available.
     
  12. Paolo Fiaschi

    Paolo Fiaschi Member Licensed User

    I see, thank you.
     
  13. JohnC

    JohnC Well-Known Member Licensed User

    Can this check routine be called from Starter Service_Create or Service_Start when the app starts so that I will know if I can safely create the google_analytics object or not?
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    I expect it to work. Have you encountered any issue?
     
  15. JohnC

    JohnC Well-Known Member Licensed User

    I actually didn't try it because I don't want to take the chance of deploying my app and then getting a bunch of bad reviews if it crashes on some user's devices.

    I actually don't like the starter service because I did have firebase and google analytics objects created and initialized in there and then I setup each to log the screen of "Starter" and then in my Main Activity I had another screen log event of "Main". But everytime I checked the google analytics app for my app, it would always report that the realtime users were looking at the "Starter" screen and not the "Main" screen, so that led me to believe that the starter service was restarting AFTER the main activity was loaded and thus canceling out the "Main" screen event. I thought the starter service would only start once when the app is first run. So that's why I am staying away from using the starter service because it's unpredictable in that it seems like it will run multiple times during a single session.
     
  16. Erel

    Erel Administrator Staff Member Licensed User

    See my answer to your other thread. You don't need it at all for Google Analytics.
     
  17. JohnC

    JohnC Well-Known Member Licensed User

    I just wanted to add a tip to anyone using this routine - it will display it's own "This app needs Play Services" dialog with an "Update" button that will bring the user to the playstore to download the PS update.

    I only mention this because most users may not have a device that would trigger this routine to return false, so I just wanted everyone to know what happens when it does return "False" so you will know you don't have to display your own dialog.
     
    Last edited: Jul 5, 2018
  18. JohnC

    JohnC Well-Known Member Licensed User

    OK, new question directly related to this thread:


    I just ran my app on an android 4.4.4 and this routine popped up the "You need an update" and returned false.

    However, I ignored installing the update and the two libraries (Firebase CrashLytic and Google Analytics) that I was doing this check for worked just fine (I forced a fault) without the play services update.

    It appears these libraries don't need the latest version of PS to work OK. So, it there anyway to modify this routine to report the version of the play services or report if ps exists at all or not on a users device?

    I ask because I don't want to bother the user to do an update when it may not even be needed.
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for any question you have.
     
Thread Status:
Not open for further replies.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice