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
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:
Using Bounded Services that uses either a
Messenger
object to pass messages between the local process and the Remote Bounded Service or usingAIDL
to create an interface that will be passed from the Remote Bounded Service to the local process so that they can communicate.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.
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.
Android: Communication between two Services of two separate APKs
Which way to communicate is "the best" one for the described case?
You only cite two options, since "Aidl" and "Binding" are the same thing. You use AIDL to describe and implement the client-side proxy and the server-side stub for cross-process service binding.
Given that you want call-and-response, binding is probably a better choice. Note, though, that there is no IPC approach that supports "complex objects" very well, outside of using Parcelable
, Serializable
, or converting the object graph into JSON or something.
What happens when Service A wants to delegate/send a message to Service B and Service B is not running?
When you call startService()
or bindService()
, Android forks a process for App B and creates an instance of the service (invoking its onCreate()
) method. At that point, the behavior is the same as if the process and service had already been running.
Communication between two closed android applications
Best way is just your server send notification to only one app. But if it is not possible, you can use Broadcast receiver I think.
You can use Broadcast receiver to communicate between two application.
In app B, for example declare broadcast receiver like follow:
<receiver android:name=".MyBCReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="what.ever.you.want" />
</intent-filter>
</receiver>
And from app A.
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setAction("what.ever.you.want");
intent.putExtra("key","data");
intent.setComponent(new ComponentName("com.app.b","com.app.b.MainActivity"));
sendBroadcast(intent);
Then your app B, you can just make a mark using sharedPreference to not make a notification again.
Related Topics
Android Studio - Creating Modules Without Copying Files
How to Get Child of Child Value from Firebase in Android
How to Hide System Navigation Bar in Tablets
Android Exception: Did You Forget to Call 'Public Void Setup (Localactivitymanager Activitygroup)'
Android Eclipse Noclassdeffounderror for External .Jar Files
Expand/Collapse Lollipop Toolbar Animation (Telegram App)
What Do Gc_For_Malloc, Gc_Explicit, and Other Gc_* Mean in Android Logcat
Android Contacts Display Name and Phone Number(S) in Single Database Query
Cannot Resolve Symbol 'Theme' in Styles.Xml (Android Studio)
Converting Activity into Fragment
List All the Files from All the Folder in a Single List
List All Camera Images in Android
Android: How to Make an Activity Return Results to the Activity Which Calls It
Android Cancel Toast When Exiting the App and When Toast Is Being Shown
Android: Display Image from Sd Card
How to Automate the Android Sdk Installation
How to Set Text Size of Textview Dynamically for Different Screens