New Google Play services stuff like low power location

marcel

Active Member
Licensed User
Longtime User
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

Hi, Yes I know but when i do this I get a lot of errors of othe resource files?

B4X:
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.
..
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
This is a known issue with B4A 3.20 and should be fixed in the next version. See here.

You would get the same error if the function is added to the library. Workaround for now (since it is fixed in B4A) is to move your additional ressources to the #AdditionalRes folder.
 
Upvote 0

marcel

Active Member
Licensed User
Longtime User
Hi,

Ok. That's a kind of hassle because I mess up all my resource files. Ok. I will wait for the next B4A version.
 
Upvote 0

marcel

Active Member
Licensed User
Longtime User
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
 

Attachments

  • 20130621.zip
    10.5 KB · Views: 379
Upvote 0

marcel

Active Member
Licensed User
Longtime User
When I changed the code in the service destroy, the closing went ok and the next time I start it is disconnected:

B4X:
Sub Service_Destroy
      Log("Destroy the location Service")
    If LocationClient1.IsConnected Then
        LocationClient1.RemoveLocationUpdates(LocationListener1)
        LocationClient1.Disconnect
    End If
End Sub

Don't ask me why :)

I am not sure if this is machine related or not.
 
Upvote 0

fricentric

Member
Licensed User
Longtime User
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

I get the below error:
B4X:
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)
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
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.
 
Upvote 0

fricentric

Member
Licensed User
Longtime User
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.

i followed step 6 and now i get the below error
B4X:
** 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
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
:) 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.
 
Upvote 0

fricentric

Member
Licensed User
Longtime User
:) 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.

Thank You Martin.
That got the app working.
 
Upvote 0

Ratna Fang

Member
Licensed User
Longtime User
hi martin :)
i tried to add service and make is simpler like this
B4X:
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

but when i compile the project (b4a 3.8), i got this:
B4X:
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.

i use this location api to retrieve lat lng without gps like when i used locationmanager library.
i also edit, remove <depends on> in the xml file and compile again. and it's still failed.

just have no idea. if i remove the location api library (and also remove the service from this project), i can compile successfully.
this project is using google map library and the manifest is correct.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Upvote 0

Ratna Fang

Member
Licensed User
Longtime User
thanks for fast reply.
yes, i'm using google map, admob v.2, and location api.
all those libraries are in separate library folder, not in b4a default library folder.

is there any problem between those libraries? i need to remove one by one to check which one is not compatible.

*what is a chromecast?
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Cromecast, streams media from the internet to a HDMI port.

You'd obviously not using a Chromecast but have a compile error that the Cast API is being included in your library more than once.
The Cast API is part of the Google Play Services library so look in your code to find where and how you're including the google-play-services.jar and it's associated resource files.
I'd guess the google-play-services.jar is being included more than once - it might even be that an old version of google-play-services.jar is being included as well as a newer version.
Are you using the AdditionalRes project attribute to include the Google Play Services resources?

Can you export your project as a zip file and upload it and i'll take a look?

Martin.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
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.
 
Upvote 0

Ratna Fang

Member
Licensed User
Longtime User
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.

yes. i always copy the latest google play jar and also point it out in the main
B4X:
#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

my latest integer version is 4242000
 
Upvote 0

Ratna Fang

Member
Licensed User
Longtime User
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.

hi martin...
got the solution :)
remove google map lib, copy new google play jar, add google map again.
compile then it works.

thanks alot :)
 
Upvote 0

brelto85

Active Member
Licensed User
Longtime User
i've 3.82 and i've installed the last android sdk updates (20)
I've the following error when i click to Start button:

java.lang.IllegalStateException: A required meta-data tag in your app's AndroidManifest.xml does not exist. 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" />

It works if i add these:

#AdditionalRes: C:\Program Files (x86)\Android\android-sdk\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms

and into manifest:

AddApplicationText(
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
)

but after start button, the IntentHandler doesn't fired
why?
 
Upvote 0
Top