2

In our Android app, we're using 3rd party vendor for Chat service. We need to create session for the user, then login to use the service and then have to fetch messages. All are separate HTTP requests which needs to be called inside one's success callback. So how can I make it, like below.

   ExternalService.createSession(param1, param2, new Callback<Session>() {

        void onSuccess(Session session) {
             session.login(new Callback<User> {


             void onSuccess(User user) {

                 user.getMessages(new Callback<List<Messages>> {

                      void onSuccess(List<Messages> messages) {
                         // This is original place where everything is success
                      }

                      void onError(Error error) {


                      }

                 } 
             }

             void onError(Error error) {


             }
        }

        void onError(Error error) {

        }

   });

If I run in the Activity, it is working fine no issues. How to do it in a Service ? Because in Service also I have problem running AsyncTask, throwing error "Can't create handler inside thread that has not called Looper.prepare()". Service exits after calling the AsyncTask and reaching end of handleAction block.(I understand, that is the behaviour Service). But how can I achieve without using Looper ? Or if there is no other way to do without using Looper, how can I stop it after accomplishing what I need ? What is the best approach to do this ?

Kamalakannan J
  • 2,818
  • 3
  • 23
  • 51

2 Answers2

0

Assuming there's no UI interactions (because you're running the code from a Service), just convert the AsyncTask to a Runnable and wrap a Thread around it:

new Thread(new Runnable {

  @Override
  void run() {
      // whatever happens in AsyncTask.doInBackground goes in here
  }

}).start();
adelphus
  • 10,116
  • 5
  • 36
  • 46
0

You can use an IntentService to do background operations like this.

It basically executes work requests passed to it sequentially in a dedicated background thread.

In the onHandleIntent() method, you can define your sequence of API calls sequentially without even needing callbacks making the code more readable (if your library supports blocking calls of course).

Something like:

//This is just pseudocode to give an idea of how this would work @Override protected void onHandleIntent(Intent workIntent) { Session session = createSession(); User user = session.login(); List<Messages> messages = user.getMessages(); }

You can do this because everything that happens in the onHandleIntent() method happens in a background thread and thus these operations will not block the UI.

You can look at the official IntentService tutorial for example usage.

One thing to note is that you cannot manipulate UI directly through this service. To communicate back to the Activity, you can use the LocalBroadcastManager.sendBroadcast() method. This is also explained in the tutorial.

Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84