The application that sends data to other applications (the app ecosystem)

This is my second article about the development of your project. Those who have not read the previous article: it is about how one place (Google table) to automatically export data to another (non).

Today I will tell you, as I wrote (and designed) library to third-party applications can obtain the data sent by my application. All interested please under cat.

Part 1. The problem

Briefly about the project. There is a device that connects to a smartphone. There’s my app, inside which the neural network recognizes data from the device and outputs the result. And there are other applications that want to obtain this result.

The result is of several types: the bare data from the device, processed data from the device, status information of devices, information about the access rights of the application data (for example that the user has revoked the access rights to the device). Need to pass this result to other apps.

If I get something wrong let me explain by its code — here is the documentation for my library.

Part 2. The planning decision

There is a wonderful mechanism — Broadcast’s. In a nutshell is a message from one application to other applications. You can send all at once, and any particular.

To this case to send and receive, you need to:

  1. To make the passed JSON object
  2. Send Broadcast
  3. To take the Broadcast in a different application
  4. To recover the transmitted object from JSON

To do from a JSON object is not always correct. You can send using Broadcast a thing called Parcelable and Serializable. Serializable is a standard piece of Java Parcelable — same thing, but optimized for mobile devices.

My objects are quite small. In addition, it is often necessary to get exactly the JSON I wrote a third-party application to send to the server as raw data. So I chose “to make the transmitted JSON object”. Maybe then I’ll reconsider.

Part 3. Sawing solution

 

Item 1 and item 4. In the JSON, and then back

Everything is simple. There is a library of Gson, which is wonderfully suited for our needs.

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

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)
}
}
}

Item 2. 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 an intent, specify the action to put the object and sent as broadcast.
BroadcastUtils.BROADCAST_GESTURE is the thing by which we filter incoming broadcast’s in another app (do I need to handle it or don’t need).

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

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

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

Item 3. Features received

So we register the receiver. If you register it using the application context — it will get broadcast’s, until the application is closed. If you use the context of activity — until it closed.

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

But GesutesReceiver:

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

Then I got the intent, transformed it back into an object and sent somewhere-there using RxJava.

Part 4. Conclusion

Have you read the article on designing applications that need to interact with other applications. I hope this experience teaches you something to help.

To heighten the effect, you can see the source code of my library and the example of work with her and put an asterisk just in case you ever will need: github.com/reface-tech/CodeSpecialApp

Source

Leave a Reply

Your email address will not be published. Required fields are marked *