1. Are you ready for the Galaxy S20? Here is everything we know so far!

Messages from Service to Client

Discussion in 'Android Development' started by mdsousa, Jul 13, 2020.

  1. mdsousa

    mdsousa Newbie
    Thread Starter

    Hi

    I am creating an Activity(Client)/Service App. The Service is local and bound to the Activity. What I would like to do is send messages from the Service to the Activity. I've looked at the Messenger class and it looks promising, except the only examples I've found have the handler in the Service, where I would like the Handler in the Activity (Client) and am not sure how to reverse the examples I've found. The Client does not need to send messages to the Service. If need be, the Client will make method calls on the Service using the IBinder.

    One example I've looked through is there;
    https://developer.android.com/guide/components/bound-services
    There are many others that do the same thing.

    The plan is the Service will open comms with other processes and funnel up messages back to the Client.

    Both Client and Service will be a long running process.

    Any advice would be appreciated.

    Edit:

    I have an example of what I'd like to do
    The activity example:
    Code (Java):
    1.  
    2. // Activity
    3. public static class Binding extends Activity
    4. {
    5.    class IncomingHandler extends Handler   /// Handle messages from Service
    6.     {
    7.         @Override
    8.         public void handleMessage(Message msg)
    9.         {
    10.             switch (msg.what)
    11.             {
    12.                 case MessengerService.MSG_SET_VALUE:
    13.                     mCallbackText.setText("Received from service: " + msg.arg1); /// MessengerService should be LocalService
    14.                     break;
    15.                 default:
    16.                     super.handleMessage(msg);
    17.             }
    18.         }
    19.     }
    20.    final Messenger mMessenger = new Messenger(new IncomingHandler());
    21.  
    22.    private LocalService mBoundService;   /// For Local Service
    23.  
    24.    private ServiceConnection mConnection = new ServiceConnection()
    25.    {
    26.         public void onServiceConnected(ComponentName className, IBinder service)
    27.         {
    28.  
    29.             mBoundService = ((LocalService.LocalBinder)service).getService();  /// For Local Service
    30.  
    31.             mService = new Messenger(service);
    32.         }
    33.    }
    34.    void doBindService()
    35.    {
    36.         bindService(new Intent(Binding.this, MessengerService.class), mConnection, Context.BIND_AUTO_CREATE);   /// MessengerService should be LocalService      
    37.         bindService(new Intent(Binding.this, LocalService.class), mConnection, Context.BIND_AUTO_CREATE)); /// For Local Service
    38.  
    39.         mIsBound = true;
    40.         mCallbackText.setText("Binding.");
    41.     }
    42. }
    43.  
    44.  
    Example of the Service:
    Code (Java):
    1. // Service
    2. public class LocalService extends Service
    3. {
    4.    NotificationManager mNM;
    5.    final Messenger mMessenger = new Messenger(new IncomingHandler());
    6.  
    7.    /// The Local Binder
    8.    public class LocalBinder extends Binder
    9.    {
    10.       LocalService getService()
    11.       {
    12.          return LocalService.this;  /// Does this include both the ability to make local methods call and the messenger?
    13.       }
    14.    }
    15.  
    16.    class IncomingHandler extends Handler
    17.    {
    18.       @Override
    19.       public void handleMessage(Message msg)
    20.       {
    21.       }
    22.    }
    23.  
    24.    private final IBinder mBinder = new LocalBinder(); /// For Local comm with client (method calls)
    25.  
    26.    /// For the Messenger version
    27.    @Override
    28.    public IBinder onBind(Intent intent)
    29.    {
    30.      return mMessenger.getBinder();
    31.    }
    32.  
    33.    /// For the Local method call version
    34.    @Override
    35.    public IBinder onBind(Intent intent)
    36.    {
    37.       return mBinder;   /// Does this include the messenger capability?
    38.    }
    39. }
    So as you can see, I'm trying to do two things with my Service, make local method calls on it from the Activity through the IBinder and also have the Service send messages back to the Activity through a Messenger. I have to believe that this is possible, I would like to know if I'm on the right track?

    Thanks...
     


    #1 mdsousa, Jul 13, 2020
    Last edited: Jul 14, 2020
  2. mdsousa

    mdsousa Newbie
    Thread Starter

    Update: The IBinder method calls into the Service would be nice, but may not be necessary with the Messenger handling most of the communication, as I believe the Messenger could handle two way messaging (I'm thinking this would make it much more complicated?).

    But another twist, is the Service will be communicating with a separate process through a socket (I imagine the Service would use a Thread to perform this task?) and would send what it receives through the Socket packets back up to the Client (Activity) using the messenger.

    I'm curious if this what a video streamer might be like, Activity binds to a Service, which in turn connects to a video stream (through a socket), receives packets, passes them back to the Activity?
     
    #2 mdsousa, Jul 20, 2020
    Last edited: Jul 20, 2020
Loading...

Share This Page

Loading...