Since B4A 3.20 you can add it yourself with the #AdditionalRes attribute and JavaObject/Reflection library.
- Download the Play Services SDK with the Android SDK Manager.
- Copy the ?\android-sdk\extras\google\google_play_services\libproject\google-play-services_lib\res to some folder
- In your B4A Project add this folder with the #AdditionalRes attribute.
B4X:#AdditionalRes: <path_to_the_folder>, com.google.android.gms
In Activity_Resume write the following:
B4X:Sub Activity_Resume Dim gpsHelper As GooglePlayServicesHelper Dim errorCode As Int errorCode = gpsHelper.IsGooglePlayServicesAvailable If errorCode <> gpsHelper.SUCCESS Then Log("Play Services not available -> try to install") Dim jo As JavaObject Dim r As Reflector jo.InitializeStatic("com.google.android.gms.common.GooglePlayServicesUtil") If jo.RunMethod("isUserRecoverableError", Array As Object(errorCode)) Then jo.RunMethodJO("getErrorDialog", Array As Object(errorCode, r.GetActivity, 9999)).RunMethod("show", Null) Else Log("Play Services not available for this device") 'Show error message here End If Else Log("Play Services available") End If End Sub
Generating R file. Error
res\values\ahqa_styles.xml:3: error: Resource does not already exist in overlay at 'Animations'; use <add-resource> to add.
res\values\ahqa_styles.xml:6: error: Resource does not already exist in overlay at 'Animations.PopDownMenu'; use <add-resource> to add.
res\values\ahqa_styles.xml:8: error: Resource does not already exist in overlay at 'Animations.PopDownMenu.Left'; use <add-resource> to add.
res\values\ahqa_styles.xml:9: error: Resource at Animations.PopDownMenu.Left appears in overlay but not in the base package; use <add-resource> to add.
res\values\ahqa_styles.xml:10: error: Resource at Animations.PopDownMenu.Left appears in overlay but not in the base package; use <add-resource> to add.
res\values\ahqa_styles.xml:13: error: Resource does not already exist in overlay at 'Animations.PopDownMenu.Right'; use <add-resource> to add.
res\values\ahqa_styles.xml:14: error: Resource at Animations.PopDownMenu.Right appears in overlay but not in the base package; use <add-resource> to add.
res\values\ahqa_styles.xml:15: error: Resource at Animations.PopDownMenu.Right appears in overlay but not in the base package; use <add-resource> to add.
res\values\ahqa_styles.xml:18: error: Resource does not already exist in overlay at 'Animations.PopDownMenu.Center'; use <add-resource> to add.
res\values\ahqa_styles.xml:19: error: Resource at Animations.PopDownMenu.Center appears in overlay but not in the base package; use <add-resource> to add.
res\values\ahqa_styles.xml:20: error: Resource at Animations.PopDownMenu.Center appears in overlay but not in the base package; use <add-resource> to add.
..
Sub LocationClient1_Disconnected
Log("LocationClient1_Disconnected")
StopService("")
End Sub
Sub Service_Destroy
Log("Destroy the location Service")
If LocationClient1.IsConnected Then
LocationClient1.RemoveLocationUpdates(LocationListener1)
LocationClient1.Disconnect
End If
End Sub
Hi,
I did get some wierd behaviours in my app. So I used the example of Martin again. (attached).
On the device settigns I have in location Services:
Use wireless networks : ON
Use GPS satellites : OFF
I will start the program "Location Client" and press the button "Tracking OFF". New locations updates are comming and everything seems to works fine.
Now I switch off in the settings Location Services:
Use wireless networks: OFF
rest is the same
THe programs stops updating locations (which seems locical). Now I go to the settings Location Services:
Use wireless networks: ON
rest the same
I expect that the service sees the change and start updateing again. But it won't infact when I press the button "Tracking ON" and stat again "Tracking OFF". this will not help it all. Also killing the program and start again won't help. I need to reboot my device and than it works again????
Another thing I expected when I press "Tracking ON" (which destroy the service) is that the procedure:
B4X:Sub LocationClient1_Disconnected Log("LocationClient1_Disconnected") StopService("") End Sub
will be called. This is not the case. So when I call again the service and this will be started the LocationClient is still connected. Even though the
LocationClient1.Disconnect is called when the Service will be destoryed.
Could some of you check this on your device? I used an Samsung Android S2 4.1.2.
Is there a failure in the demo code or library? Or is it my device that behaives wierd?
Thanks
Marcel
LogCat connected to: emulator-5554
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (locationtracker) Create **
java.lang.NoClassDefFoundError: com.google.android.gms.R$string
at com.google.android.gms.internal.ib.<init>(Unknown Source)
at com.google.android.gms.internal.hi.<init>(Unknown Source)
at com.google.android.gms.location.LocationClient.<init>(Unknown Source)
at uk.co.martinpearman.b4a.android.gms.location.LocationClient.Initialize(LocationClient.java:127)
at uk.co.martinpearman.b4a.locationapidemo.locationtracker._service_create(locationtracker.java:174)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:158)
at uk.co.martinpearman.b4a.locationapidemo.locationtracker.onCreate(locationtracker.java:45)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2959)
at android.app.ActivityThread.access$3300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2087)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
** Service (locationtracker) Create **
java.lang.NoClassDefFoundError: com.google.android.gms.R$string
at com.google.android.gms.internal.ib.<init>(Unknown Source)
at com.google.android.gms.internal.hi.<init>(Unknown Source)
at com.google.android.gms.location.LocationClient.<init>(Unknown Source)
at uk.co.martinpearman.b4a.android.gms.location.LocationClient.Initialize(LocationClient.java:127)
at uk.co.martinpearman.b4a.locationapidemo.locationtracker._service_create(locationtracker.java:174)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:158)
at uk.co.martinpearman.b4a.locationapidemo.locationtracker.onCreate(locationtracker.java:45)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2959)
at android.app.ActivityThread.access$3300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2087)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
You need to include the Google Play Services library resources in your b4a project.
That wasn't originally required when this thread was started but is now required.
Look at the Google Maps tutorial thread.
You need to follow step #6 and add an AdditionalRes attribute to your project.
Martin.
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (locationtracker) Create **
** Service (locationtracker) Start **
java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 4323000 but found 0. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
at com.google.android.gms.common.GooglePlayServicesUtil.t(Unknown Source)
at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
at uk.co.martinpearman.b4a.android.gms.location.GooglePlayServicesHelper.IsGooglePlayServicesAvailable(GooglePlayServicesHelper.java:27)
at uk.co.martinpearman.b4a.locationapidemo.locationtracker._service_start(locationtracker.java:204)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:158)
at uk.co.martinpearman.b4a.locationapidemo.locationtracker.handleStart(locationtracker.java:68)
at uk.co.martinpearman.b4a.locationapidemo.locationtracker.onStartCommand(locationtracker.java:53)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053)
at android.app.ActivityThread.access$3600(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Unable to start service uk.co.martinpearman.b4a.locationapidemo.locationtracker@44fb0c40 with Intent { cmp=uk.co.martinpearman.b4a.locationapidemo/.locationtracker }: java.lang.RuntimeException: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 4323000 but found 0. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3063)
at android.app.ActivityThread.access$3600(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 4323000 but found 0. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:200)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:158)
at uk.co.martinpearman.b4a.locationapidemo.locationtracker.handleStart(locationtracker.java:68)
at uk.co.martinpearman.b4a.locationapidemo.locationtracker.onStartCommand(locationtracker.java:53)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053)
... 10 more
Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 4323000 but found 0. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
at com.google.android.gms.common.GooglePlayServicesUtil.t(Unknown Source)
at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
at uk.co.martinpearman.b4a.android.gms.location.GooglePlayServicesHelper.IsGooglePlayServicesAvailable(GooglePlayServicesHelper.java:27)
at uk.co.martinpearman.b4a.locationapidemo.locationtracker._service_start(locationtracker.java:204)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
... 14 more
Yet another change that you have to make.
Open the manifest editor and add this entry:
B4X:AddApplicationText(<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />)
Martin.
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim GooglePlayServicesHelper1 As GooglePlayServicesHelper
Dim LocationClient1 As LocationClient
Dim LocationListener1 As LocationListener
Dim LocationStatusCodes1 As LocationStatusCodes
Dim GoogleLat As Double, GoogleLong As Double
End Sub
Sub Service_Create
LocationClient1.Initialize("LocationClient1")
End Sub
Sub Service_Start (StartingIntent As Intent)
If LocationClient1.IsConnected Then
CallSub2(Main, "UpdateStatus", "Waiting for location updates")
Else
Dim ServiceStatus As Int=GooglePlayServicesHelper1.IsGooglePlayServicesAvailable
Select ServiceStatus
Case GooglePlayServicesHelper1.SUCCESS
Log("GooglePlayServicesHelper IsGooglePlayServicesAvailable service available")
CallSub2(Main, "UpdateStatus", "LocationClient connection")
LocationClient1.Connect
Case Else
Log("GooglePlayServicesHelper IsGooglePlayServicesAvailable returned: "&ServiceStatus)
CallSub2(Main, "UpdateStatus", "Google Play Services unavailable")
StopService("")
End Select
End If
End Sub
Sub Service_Destroy
' i don't think we actually need to remove the listener and geofences
' i think calling Disconnect is all we need to do
' need to check and test...
If LocationListener1.IsInitialized Then
LocationClient1.RemoveLocationUpdates(LocationListener1)
End If
If LocationClient1.IsConnected Then
LocationClient1.Disconnect
End If
End Sub
Sub LocationClient1_Connected
'not sure if this is a good idea, to pass the last known location to the LocationChanged Sub
'pokoknya hajar aja ambil last know location
Dim LastLocation As Location=LocationClient1.GetLastLocation
If LastLocation.IsInitialized Then
'Log("Manually calling LocationListener1_LocationChanged")
'dipanggil manual ben remuk
LocationListener1_LocationChanged(LastLocation)
End If
End Sub
' ** the GPS library must be enabled to work with Location objects **
Sub LocationListener1_LocationChanged(Location1 As Location)
'Main.Locations.Add(Location1)
GoogleLat=Location1.Latitude
GoogleLong=Location1.Longitude
End Sub
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lcom/google/android/gms/cast/Cast$CastApi$a$9;
at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123)
at com.android.dx.dex.file.DexFile.add(DexFile.java:163)
at com.android.dx.command.dexer.Main.processClass(Main.java:490)
at com.android.dx.command.dexer.Main.processFileBytes(Main.java:459)
at com.android.dx.command.dexer.Main.access$400(Main.java:67)
at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:398)
at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:245)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:131)
at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:109)
at com.android.dx.command.dexer.Main.processOne(Main.java:422)
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:333)
at com.android.dx.command.dexer.Main.run(Main.java:209)
at com.android.dx.command.dexer.Main.main(Main.java:174)
at com.android.dx.command.Main.main(Main.java:91)
1 error; aborting
Optimized dexer failed. Switching to Standard dexer.
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lcom/google/android/gms/cast/Cast$CastApi$a$9;
One more thought...
Have you copied the Google Play Services 'res' folder to your project's Objects\res folder and also used an AdditionalRes project attribute to include them a second time?
Martin.
#AdditionalRes: E:\androidsdk\adt-bundle-windows-x86_64\adt-bundle-windows\sdk\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms
One more thought...
Have you copied the Google Play Services 'res' folder to your project's Objects\res folder and also used an AdditionalRes project attribute to include them a second time?
Martin.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?