Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android communicating two apps in separate devices

All the questions here point to classes of the same app or different apps in separate processes yet in the same device. I would like to send data to and from two separate apps in two separate devices. I tried using broadcastreceiver but it didn't work. Here is my snippet to send the data.

addressstring = String.valueOf(acrilocation.getText());
            if (addressstring != null && addressstring.length() > 0){
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_SEND);
            intent.putExtra(Constants.LOCATION_DATA_EXTRA, addressstring);
            intent.setType("text/plain");
            sendBroadcast(intent);
            } else{
                Toast.makeText(getApplicationContext(), "Enter valid location address", Toast.LENGTH_SHORT).show();
            }

but when I receive the data in my other app using the following code snippet, It fails. When I debug the app I get null exception.

Intent intent = getIntent();
        String action = intent.getAction();
        String data = intent.getStringExtra(Intent.EXTRA_INTENT);
        String type = intent.getType();
        useraddress.setText(data);
           startActivity(intent);

Is there another way to achieve this? I mean to send data to and from another app which is installed in another device?

like image 614
The_Martian Avatar asked Mar 13 '15 19:03

The_Martian


People also ask

How do I communicate between two apps on Android?

Android inter-process communication At the simplest level, there are two different ways for apps to interact on Android: via intents, passing data from one application to another; and through services, where one application provides functionality for others to use.

Can you share an app between two devices?

Share a Direct Link from the Google Play Store Find the app or game that you want to share and tap the three-dot menu icon in the top-right corner. Next, select “Share” from the menu.

Can I have 2 phones with the same apps?

Using the Native Android Cloning FeatureIt lets you run multiple copies of the same app without having to install any third-party tool. This feature is available on Samsung, Xiaomi, Oppo, and OnePlus phones, among others. More brands may well adopt this functionality in the future.


1 Answers

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.

like image 189
Mick Avatar answered Sep 21 '22 08:09

Mick