0

Consider a data being generated in a service (Continuously), i want an other application to retrieve the data from service.

Example: App A (Service) will open the serial port and communicate with external device to get data.

App B (Standalone app) must read the data from service (Data from external device).i have not done that since i need data to be retrieved from external device even if App B is not running, and i have 3 more other apps that require the same data from external device, hence i have decided to run a service in the background.

RShenoy
  • 25
  • 7
NKR
  • 343
  • 3
  • 15
  • If you're doing this over HTTP, I'd suggest either looking at web sockets OR consider switching to pulling the data from the user. HTTP is a request/response protocol. There's a lot of complexity in managing data receipt. Better to leave the service out of it and let clients figure out what they need. – duffymo Feb 06 '17 at 12:22
  • How frequently does the data change? What is the size of the data? Are you looking for a push solution (service does something to cause changes to be delivered to the client), a pull solution (service only responds to requests for data from the client), or both? IOW, this has little to do with serial ports and a lot to do with the nature of the data being exchanged. – CommonsWare Feb 06 '17 at 12:37
  • @CommonsWare i want service to respond to the request and i'm not sure how often data changes. – NKR Feb 06 '17 at 12:41
  • @duffymo i dont know how web sockets work. I'm not doing this over HTTP. – NKR Feb 06 '17 at 12:42
  • "i want service to respond to the request" -- you accepted an answer that does not offer this. You could use a bound service (with an inter-process API defined via AIDL) or a `ContentProvider` (where you set up `query()` to treat requests akin to how a REST Web service would, generating responses). – CommonsWare Feb 06 '17 at 12:51
  • @CommonsWare Thanks, ill go through ContentProvider and let you know if i can implement it properly. – NKR Feb 06 '17 at 13:40
  • @CommonsWare can i get a sample code which implements Bound Services? because i have tried using 'Messenger' to send data, but i was unable to get the data from service to app.. – NKR Feb 06 '17 at 14:09
  • https://github.com/commonsguy/cw-omnibus/tree/master/Binding/Remote – CommonsWare Feb 06 '17 at 14:10
  • @CommonsWare Sorry for bothering you. The link you have provided uses AIDL approach, can i get a sample that transfer a string from service to an other app using 'Messenger'. This link explains how to send data when service is a part of the app : http://stackoverflow.com/questions/4300291/example-communication-between-activity-and-service-using-messaging I need it to work when service is independent. – NKR Feb 06 '17 at 14:31
  • "can i get a sample that transfer a string from service to an other app using 'Messenger'." -- I do not have a sample app that demonstrates this, sorry. – CommonsWare Feb 06 '17 at 14:53
  • @CommonsWare Thank you :) – NKR Feb 07 '17 at 04:17

1 Answers1

2

You could use Broadcasts.

In your app with the service you can broadcast Intents to the system, which can then be picked up by the other apps.

In your app with the service you can send a broadcast like so:

public class MyService extends Service {

    public static final String ACTION_PUBLISH_DATA = "com.example.action.publish_data";

    public void publishResult(Bundle data) {
        Intent intent = new Intent(ACTION_PUBLISH_DATA);
        intent.putExtras(data);
        sendBroadcast(intent);
    }

}

In your client apps, create a broadcast receiver:

public class DataBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // do something with data in the intent.
    }

}

And register it in your manifest like so:

<application>
    ...

    <receiver android:name="com.example.DataBroadcastReceiver">
        <intent-filter>
            <action android:name="com.example.action.publish_data" />
        </intent-filter>
    </receiver>

</application>

Instead of registering in your manifest, you can also register the receiver at runtime using the registerReceiver method

Edit:
This is, as far as I know, the easiest way to communicate between multiple applications. But note: as said in the comments, this is not on request. The service has know idea who, or anyone at all, is listening to the broadcasts.
You could however send broadcasts from the client app to the service app to start or stop the service.
You could also use an IntentService in your App A and start it from the other apps (with a ResultReceiver in the extras). Or you could also look into ContentProviders

AIDL is an option but I would not recommend this for you since it is more advanced.

RobCo
  • 6,240
  • 2
  • 19
  • 26
  • Thanks, i will try this. if u can provide a sample complete app code it will be very much helpful, because i'm very new to android programing. – NKR Feb 06 '17 at 12:48
  • @NKR A full working application is out of scope for this question. If you are just beginning I recommend starting [here](https://developer.android.com/training/basics/firstapp/index.html) and keep following them until you have a running app. Then you can work towards adding the elements as described in the answer, for which [this](https://developer.android.com/guide/components/intents-filters.html) might be a good read. – RobCo Feb 06 '17 at 13:12
  • Thanks, i will look into those links. – NKR Feb 06 '17 at 13:38