Developing Two Android Apps and Communicating Between Two

Developing two android apps and communicating between two

You cannot directly access methods of Activity in different app.

Broadcast would work but if you want more control (i.e. invoke remote methods) consider binding to a remote service in the other app or use Messenger and handler to communicate.

Here is a short tutorial on inter-app communication

android communicating two apps in separate devices

Connecting over networks that accept incoming socket connections

The usual way to do this between Android devices (or between any peer devices) is to use sockets.

You set up one or both devices to 'listen' for connections on a socket and then accept a connection from the other when they want to communicate (or you can have a dedicated client and server and the client always initiates the connections).

Once the connection is established you can send messages back and forth.

There are many examples of Android client server socket applications, but one I found useful was:

  • Android Server/Client example - client side using Socket (and its companion server side blog article - link included in the client blog)

Note that you may have to add your own 'protocol' on top of this - for example if you are sending a file of unknown length without any special 'end' character, you may want to add a byte (or several byte to represent an int, long etc) at the start to indicate the length of the transmission so the receiving side knows when it has received everything (or that it has not received everything in case of an error).

Connecting over networks which do not allow incoming connections (e.g. most 3G/4G)

In these scenarios, while there is nothing theoretically stopping sockets working, in practice many mobile operators will not allow incoming socket connections. In addition you would need to find the public IP address of the Mobile, which is possible but is extra complexity. If your solution will only ever run on a single operators network you can experiment and see if it works, but if not you may find it better and easier to use a server in the 'middle':

  • Device A connectes to server
  • Device B connectes to server
  • Device A asks server for addresses of connected devices and 'discovers' device B
  • Device A send a message for device B. It actually sends the messages to the server with an indication that it is to be sent to device B
  • The server notifies device B that a message is available for it (using some sort of message notification like Google Cloud Messaging for example, or simply by the devices polling regularly to see if they have any messages).
  • Device B retrieves the messages from the server

The above will work on pretty much any network that allows connectivity to the internet. It does have the disadvantage of requiring a server but it is likely a necessary approach over most mobile networks.

How to communicate between two applications in Android?

After I made some research on this topic I found that there are several ways to do this operation:

  1. Using Bounded Services that uses either a Messenger object to pass messages between the local process and the Remote Bounded Service or using AIDL to create an interface that will be passed from the Remote Bounded Service to the local process so that they can communicate.

  2. The second options would be using the good old fashion BroadcastReceivers. That way as always it is possible to fire an Intent from the local process to the remote process and there receive some information.

The different for the usage of those both two would be decided by how strong would you like the connection to be between the two processes and how often should they be communicating. If they need to do one operation once in a while the BroadcastReceivers would be a perfectly good solution. But if you need a more consistent connection the Bounded Service is the way to go.

Guide on how to communicate between 2 separate apps on two android devices

You said that you want the two apps to communicate via internet. You just answered yourself. Since you are new to android development start with learning firebase.

If you are using Android Studio, there is well documented Firebase Assistant built inside.

You can use it to connect the two apps. Use the 'Firebase notification service' to send custom notifications to any of the two apps.

For authenticating the message you can do a million things like adding a button or using some edit text in which user will enter some authentication code which will already be stored in your Firebase Database.

Then add another button which will either send the message received to your main server for running the queries or be stored in the second app.

Enable communication between two Android apps via custom permissions

Wouldn't this mean that the app can't access its own data any longer?

Correct.

I've added the following permission

I would dump the permission-group, as that should not be necessary.

Furthermore, both manifest files have got this part now

Only the one calling your IntentService might need that.

Shouldn't this do the job so that I can call the add-on's IntentService from my main app via this code?

Not if that IntentService is not exported. Make sure that your IntentService either has an <intent-filter> or android:exported="true". I would recommend going the <intent-filter> route, so you can declare and use a custom action string, so you get away from hard-coding package and class names in the client app.

Here is a directory with two sample projects using this basic approach, though in my case the communications are based on a secured ContentProvider rather than a secured IntentService. The concept is the same, though, and so with these minor tweaks, I would expect what you are doing to work just fine.

Android - communicating between two devices

You can connect them via bluetooth using BluetoothSockets. Android developer website has pretty good documentation on this.

http://developer.android.com/guide/topics/wireless/bluetooth.html

Or if you'd rather (and have internet on both devices), you can use regular Socket's.

http://developer.android.com/reference/java/net/ServerSocket.html for server side
http://developer.android.com/reference/java/net/Socket.html for client side

If you have a large amount of data to transfer, internet sockets have a greater data capacity and will be faster. The other advantage is that there is no such thing as "out of range". You can connect the two devices wherever internet is available, whereas with bluetooth they have to be within bluetooth range of each other



Related Topics



Leave a reply



Submit