Callback from gomobile to java for asynchornous call

1,051 views
Skip to first unread message

cyrille...@gmail.com

unread,
May 24, 2015, 1:50:39 PM5/24/15
to golan...@googlegroups.com
Hi all, 

I am using gomobile to write some asynchronous network call for my app and I want to write, in Android Java, only the UI feedback.
The go code is using http.Get() perform a network call. I would love to be able to display a spinner in my java android code before the request starts (i.e before the call the go code) and then remove the spinner when the go code returns the result.
How can I achieve that ? Is there a solution that does not require me to modify the automatically generated binding files ? More generally, is it possible to register a java callback for the go code to invoke when something becomes asynchronously available ?

Thank you
Cyrille

David Crawshaw

unread,
May 25, 2015, 8:58:53 AM5/25/15
to cyrille...@gmail.com, golang-nuts
In your Go package, you can define:

type SomeCallback interface {
DoSomething()
}

func RegisterSomeCallback(c SomeCallback) {
// save a copy of c and call DoSomething on it later
}

the generated Java will contain an abstract class named SomeCallback.
You can create a subclass of it:

public class ParticularCallback extends Pkg.SomeCallback {
public void DoSomething() {
// do something here
}
}

and then pass an instance of it to Go by calling Pkg.RegisterSomeCallback.
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Cyrille Quemin

unread,
May 26, 2015, 5:07:13 PM5/26/15
to golan...@googlegroups.com, cyrille...@gmail.com
Thank you David, for the quick answer. Exactly what I was looking for. 
It is pretty straight forward actually :)
Reply all
Reply to author
Forward
0 new messages