Android Code Snippet Check and install Google Play Services

Status
Not open for further replies.

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.

B4X:
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:
B4X:
#AdditionalJar: com.google.android.gms:play-services-base
 
Last edited:

KMatle

Expert
Licensed User
com.google.android.gms.comm
I'm getting

B4X:
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\...)
 

Erel

Administrator
Staff member
Licensed User

arastoo

Active 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

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

B4X:
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)
 

arastoo

Active Member
Licensed User
Use B4A Sdk Manager to install the latest versions of all recommended components.
Hi Erel

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


B4X:
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>
 

Attachments

Paolo Fiaschi

Member
Licensed User
Please start a new thread for this in the questions forum. Make sure not to post the code as bold.
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.

B4X:
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:
B4X:
#AdditionalJar: com.google.android.gms:play-services-base
Hi, this code is equivalento to mine?

B4X:
If MapFragment1.IsGooglePlayServicesAvailable = False Then
    ToastMessageShow("Please install Google Play Services.", True)
End If
 

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?
 

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.
 

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:

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.
 
Status
Not open for further replies.
Top