We develop an application that sends data to other applications (ecosystem application)

Hi guys!



This is my second article about the development of my project . For those who have not read the previous article : it is about how to automatically export data from one place (google table) to another (home).



Today I will tell you how I wrote (and designed) the library so that third-party applications can receive data sent by my application. I ask everyone interested under cat.



Part 1. Problem



Briefly about the project. There is a device that connects to a smartphone. There is my application, inside which the neural network recognizes data from the device and gives the result. And there are other applications that want to get this very result.



The result can be of several types: bare data from the device, processed data from the device, information about the state of the device, information about the application’s access rights to the data (for example, that the user has revoked access rights to the device’s data). It is necessary to somehow transfer this very result to other applications.



If I suddenly explain something incorrectly in my code - here is the documentation for my library.



Part 2. Planning a solution



There is a wonderful mechanism - Broadcasts. In short, this is a message from one application to other applications. You can send it to everyone right away, or you can send it to a specific one.



To send and receive this business, you need:



  1. Somehow make JSON from the passed object
  2. Submit Broadcast
  3. Accept Broadcast in another application
  4. Recover object being passed from JSON


In general, making a JSON from an object is not always correct. You can send a thing called Parcelable via Broadcast, or Serializable. Serializable is a standard thing from Java, Parcelable is a similar thing, but optimized for mobile devices.



My objects are quite small. In addition, often it is necessary to get JSON: I myself wrote a third-party application so that it sends raw data to the server. Therefore, I chose "make JSON from the passed object". Maybe then I'll change my mind.



Part 3. Sawing the solution



Clause 1 and Clause 4. In JSON, and then back



Everything is simple here. There is a Gson library that is wonderfully suited to our needs.



To make things cool, override the toString () method. Well, do fromString () to get our object back.



class SecureData(val eeg1: Double?, val eeg2: Double?, date: Date) { override fun toString(): String { val gson = Gson() return gson.toJson(this) } companion object { fun fromString(model: String): SecureData { val gson = Gson() return gson.fromJson(model, SecureData::class.java) } } }
      
      





Point 2. We send the object



Here is an example of such code:



 val intent = Intent() intent.action = BroadcastUtils.BROADCAST_GESTURE intent.putExtra(BroadcastUtils.EXTRA_GESTRE, it.toString()) sendBroadcast(intent)
      
      





Here we create intent, set its action, put the object and send it as broadcast.

BroadcastUtils.BROADCAST_GESTURE is a little thing by which we will filter incoming broadcasts in another application (whether it should be processed or not).



To send a message to a specific application, you need to additionally specify the following:



  intent.component = ComponentName( PermissionsFetcher.REFACE_APP, "${PermissionsFetcher.REFACE_APP}.receivers.ActionsReceiver" )
      
      





PermissionsFetcher.REFACE_APP is the APPLICATION_ID of my application, and $ {PermissionsFetcher.REFACE_APP} .receivers.ActionsReceiver is the path to the receiver.



Point 3. We receive objects



This is how we register the receiver. If you register it using the application context, it will receive broadcasts until the application is closed. If you use the activation context - until it closes.



  registerReceiver(GesturesReceiver(), IntentFilter(BroadcastUtils.BROADCAST_GESTURE))
      
      





And here is the GesturesReceiver:



 class GesturesReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { Timber.i("Received gesture") val action = BroadcastUtils.reparseGestureIntent(intent) MainApp.actionSubject.onNext(action) } }
      
      





Here, as you see, I received the intent, redid it back to an object, and sent it somewhere using RxJava.



Part 4. Conclusion



You read an article on designing applications that should interact with other applications. I hope this experience helps you with something.



To increase the effect, you can see the sources of my library and an example of working with it and put an asterisk in case you ever need it: github.com/reface-tech/CodeSpecialApp



All Articles