Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android sending periodically data from service to Activity

I have a application which starts service on the first launch. After that it pulls data from the server periodically.

  1. I have opened my activity and if there is refresh button, I already have service which is already fetching data in the background that moment I want to disable the button and as soon as new data is loaded I have to show it in activity and enable refresh button.

  2. If activity is not running then it should show notification.

So second point was the easiest and done. I'm stuck on the point 1. How to send periodically data to activity from service? I'm using database to store the data.

Any help on this ?

like image 898
Umakant Patil Avatar asked Dec 21 '22 07:12

Umakant Patil


1 Answers

  • You can have your service send 'Messages' to your Activity Messenger to make it react as soon as service detects new content (see this android developers help section on Activity/Service Messenging).

Here are samples for Two-Way messaging (from Service to Activity and from Activity to Service). Quoting the doc:

You can see an example of how to provide two-way messaging in the MessengerService.java (service) and MessengerServiceActivities.java (client) samples.

Here's the relevant parts.

Incoming Handler in Activity:

    /**
     * Activity Handler of incoming messages from service.
     */
    class IncomingHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MessengerService.MSG_SET_VALUE:
                    mCallbackText.setText("Received from service: " + msg.arg1);
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
    }

    /**
     * Activity target published for clients to send messages to IncomingHandler.
     */
    final Messenger mMessenger = new Messenger(new IncomingHandler());

In the service, showing only the relevant parts:

/**
 * Handler of incoming messages from clients.
 */
class IncomingHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
        //obtain Activity address from Message
        Messenger mClient=msg.replyTo;
        try { 
        // try to send it some mValue
           mClient.send(Message.obtain(null,MSG_SET_VALUE, mValue, 0));
        } catch (RemoteException e) {
          // The client is dead.  Remove it
          mClient=null;
        }
    }
}

/**
 * Target we publish for clients to send messages to IncomingHandler.
 */
final Messenger mMessenger = new Messenger(new IncomingHandler());
  • Also you can bind to your service from your activity and periodically call one of your service method to check for new content. For this, if your service is another application, you must use aidl (this is harder). If it's in the same package, I advise you to use the much easier 'local service binding'
like image 115
Laurent' Avatar answered Dec 27 '22 21:12

Laurent'