Search code examples
androidandroid-serviceandroid-bluetoothandroid-broadcastandroid-broadcastreceiver

Android service - invoking methods and passing objects, without binding


I have an app where I need to establish and maintain a bluetooth connection with another phone. However, this connection needs to remain alive even if the screen turns off.

So the way I've done this is 1) make it a service so the connection can exist in the background and 2) make an explicit call to start/stop the service instead of binding it to the activity (I believe if the screen goes off, the activity goes away, thus the service will stop)

This has now made things more complicated because my service has methods that I need to be able to manually invoke. For example, I want to start bluetooth discovery when the user clicks a button. So on button click, I need to tell this service to call my startDiscovery method. There are many situations like this (e.g. open a socket, pair to a device etc) where I need to manually call service methods

A lot of what I've read on this topic solves this by binding the service, but this I cannot do as explained earlier

Without binding, others suggest to use some sort of event bus, where on button click I send a message to the service. When it receives the message, it checks what type of message it is and then invokes the appropriate method.

OK, this works, but what if my method requires me to pass something into it? For example, lets say I have a list or something that I need to send over bluetooth. So I have a method in my Service that takes a list object, serializes it and sends it over BT to the other phone. But this doesn't seem possible with a basic messaging/event bus system

In sum, how do I pass an object through to a method in a service that is not bound to an activity, but instead has been manually started with startService?

I have seen this question here, but that method only seems to allow me to send objects when I start the service. In my case, the service is already started and sits in the background handling bluetooth traffic. I need to be able to invoke methods and pass objects while the service is already running


Solution

  • I have done something similar in my service. Sometimes i need to manually hide the notification that the service created. So i made the method public and static so it can be called anywhere like this:

    public static void hideNotification(){
        notificationManager.cancel(0);
    }
    

    Then call it in your activity like this: MyService.hideNotification()

    EDIT

    If you do not want a static method, you can create an empty constructor for your service and then when you need to call the method, create a new instance of your service and call it from that. For example:

    In the service:

    public class MyService extends Service{
        public MyService(){}
    
        public void hideNotification(){
            notificationManager.cancel(0);
        }
    }
    

    When you need to call a method:

    MyService service = new Myservice();
    service.hideNotification();