Reputation: 679
Are there any drawbacks to using intents as a form of message passing between two apps that I control?
I have two apks which will always exist on the device together. And, I'd like to use explicit intents to pass messages back and forth as opposed to creating and managing two separate services. Using explicit intents just seems like an easier to manage approach than services.
Upvotes: 1
Views: 524
Reputation: 6715
Although the discussion here seems really good, at @André.C.S's suggestion, I will add my 2 cents.
Intents are a simple and effective way of passing messages between processes. One of Android's key features, the ability to call another application as a function (Activity.startActivityForResult), depends on it.
As everyone here has already pointed out, intents, whether fired at BroadcastReceivers
, Services
or Activities
, have security issues. If you decide to use them, you will need to protect them. Andre's description of how to use permissions to do that is spot on. You must treat the method that catches the intent as if it were a web service and verify parameters, etc. accordingly.
Finally, Intents
, while simple, are really not appropriate for high-bandwidth IPC. If you will be exchanging messages with the other app at a rate measured in milli-seconds, you will want to look into AIDL and a bound service. By my measurements, they are between 1 and 2 orders of magnitude faster.
Incidentally, you might consider running both applications in the same process. There are manifest application attributes that allow that. If you did that, using explicit intents would be easy and your apps would be much safer.
Edited to point out extremely embarrassing error
So, taking my own advice, I tried it. To my horror, as several people have tried to point out, explicit intents can be used across processes.
I am eating a large helping of crow, with humble pie for dessert.
An explicit intent contains a ComponentName
object, not an explicit reference to a class object, as I claimed. A ComponentName
contains a package name and a class name. The former is, typically, derived from the current context and thus is local to the current process. It is possible, however, to construct an intent with a package name that is an arbitrary string.
I stand corrected.
Upvotes: 0
Reputation: 5505
Communication between applications can expose certain rich, but if you really need to do this way, you can only customizing permissions that your applications will have knowledge. Then you can use BroadcasrReceiver
to exchange messages securely using custom permissions.
Defining their permission:
<permission android:name="com.yourapp.PERMISSION"
android:protectionLevel="signature"
android:label="@string/permission_label"
android:description="@string/permission_desc">
</permission>
By setting
<receiver android:name=".MyReceiver"
android:permission="com.yourapp.PERMISSION">
<intent-filter>
<action android:name="com.yourapp.ACTION" />
</intent-filter>
</receiver>
In addition to these permissions you can also set them their purchases Activities
, Services
, ContentProvider
.
Edited
Integration between existing processes in Android
(Inter-Process Communication
), the AIDL (Android Interface Definition Language
).
AIDL is particularly useful when different applications need to communicate among themselves to exchange information through a well-defined interface with support for multithreading. Unlike the use of Messenger with Bound Services with AIDL you are required to create a file. AIDL containing the declaration of the integration interface, which helps the client applications to know which operations are available as well as their respective arguments and returns.
Upvotes: 1
Reputation: 3430
You can use intents to pass data between two apps..however I see once drawback(not too big)-you need to take care that those intents dont get exposed to others.Some malicious app can use the same intents to send bad data something similar to Denial-of-service attacks.
Just to add-if the interactions are in background you can use broadcast receivers too.
Also if the apps are always going to be together why are you not packing them as single app.If you are using parcelables, your flow might break if there are two different incompatible versions of the parcelable.(if you add a field to a object in new version of an app,the other app is still not updated,the app might crash).
Upvotes: 0