Introduction
You might be wondering how your Android applications are integrating with all other applications or each other. How do they communicate and integrate themselves?
Whenever an intent is being constructed its main purpose is to make an action against the current activity or another activity whether the intent is triggered in the current activity. The Android library defines several actions, such as ACTION_SEND. It indicates that the triggering of the send action means sending something from one activity to another activity. This not only limits the one application's activity, it can trigger an action to another application, in other words across applications.
When sending the data to another application we need to specify the data type and other aspects associated with it. If there are multiple options to display the sent data by other activities then the system latches the compatible activity and the result is returned and is shown to the user. Sending and receiving the data among applications with intents is the most common way for the use of social sharing of contents.
Note: The best way to add a share action item to an ActionBar is to use sharedActionProvider made available with the API level 14.
Send Text Content
The most and very common use of the ACTION_SEND action is sending text content from one activity to another activity. This method is widely used when developing applications that share data.
- Intent sendIntent = new Intent();
- sendIntent.setAction(Intent.ACTION_SEND);
- sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
- sendIntent.setType("text/plain");
- startActivity(sendIntent);
The preceding code is the example of this type of sharing, however it is only for illustration purposes. It doesn't however produce any output. There's an installed application having a filter that Latches
ACTION_SEND and the
MIME type and the text.
The Android system will run it after doing this. If more than one application matches, the system displays a disambiguation dialog and a chooser that allows the user to choose an application.
Let us take an example if we call
Intent.createchooser(), ing if your Intent object return type of this is a chooser as having some pros as follows:
- Even if the user has previously selected a default action for this intent, the chooser will still be displayed.
- If no applications match, Android displays a system message.
- You can specify a title for the chooser dialog.
Let us analyze some code as given below:
- Intent sendIntent = new Intent();
- sendIntent.setAction(Intent.ACTION_SEND);
- sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
- sendIntent.setType("text/plain");
- startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
Intents have the standards
EXTRA_EMAIL,
EXTRA_CC,
EXTRA_BCC and
EXTRA_SUBJECT. If the receiving application is not designed to use them, it simply ignores them.
Send Binary ContentBinary data is shared using the ACTION_SEND action combined with the setting of the appropriate MIME type and placing the URI to the data in an extra name.
- You can use a MIME type of "*/*", but this will only match activities that are able to handle generic data streams.
- The receiving application needs permission to access the data the Uri points to. The recommended ways to do this is described in the following.
Store the data in your own
ContentProvider, be sure that other apps have the correct permission to access your provider. The preferred mechanism for providing access is to use per-URI permissions that are temporary and only grant access to the receiving application. An easy way to create a ContentProvider like this is to use the
FileProvider helper class.
Send Multiple Pieces of ContentAn ACTION_SEND_MULTIPLE action is used to send multiple pieces of code such that in the list of URI pointers to the content. The MIME type varies depending on the mix of content you are sharing.
For instance we send a JPEG extension file's image that has an image attribute such that an activity handles the type of this type of data. Then it can only be a part of receiving that data. Let us have a look at the given code.
- ArrayList<Uri> imageUris = new ArrayList<Uri>();
- imageUris.add(imageUri1);
- imageUris.add(imageUri2);
-
- Intent shareIntent = new Intent();
- shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
- shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
- shareIntent.setType("image/*");
- startActivity(Intent.createChooser(shareIntent, "Share images to.."));
Receiving Simple Data From Another ApplicationWe have learned the preceding, that our application can be integrated with another application within a device. Now it can be good to know that our application can receive data as well. We must specify our activity of which type of data is coming and accordingly we must code our activity depending on the data type we will receive.
Updating the Manifest fileIntent filters inform the system what intents an application component is willing to accept. Similar to how you constructed an intent with the action
ACTION_SEND in the receiving activity file.
- <activity android:name=".ui.MyActivity" >
- <intent-filter>
- <action android:name="android.intent.action.SEND" />
- <category android:name="android.intent.category.DEFAULT" />
- <data android:mimeType="image/*" />
- </intent-filter>
- <intent-filter>
- <action android:name="android.intent.action.SEND" />
- <category android:name="android.intent.category.DEFAULT" />
- <data android:mimeType="text/plain" />
- </intent-filter>
- <intent-filter>
- <action android:name="android.intent.action.SEND_MULTIPLE" />
- <category android:name="android.intent.category.DEFAULT" />
- <data android:mimeType="image/*" />
- </intent-filter>
- </activity>
Handling the Incoming Content Since the manifest file was updated with the specifications we must code the Java part.
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
-
-
- public class RecentActivity extends Activity {
-
- @Override
- void onCreate (Bundle savedInstanceState) {
- ...
-
- Intent intent = getIntent();
- String action = intent.getAction();
- String type = intent.getType();
-
- if (Intent.ACTION_SEND.equals(action) && type != null) {
- if ("text/plain".equals(type)) {
- handleSendText(intent);
- } else if (type.startsWith("image/")) {
- handleSendImage(intent);
- }
- } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
- if (type.startsWith("image/")) {
- handleSendMultipleImages(intent);
- }
- } else {
-
- }
- ...
- }
-
- void handleSendText(Intent intent) {
- String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
- if (sharedText != null) {
-
- }
- }
-
- void handleSendImage(Intent intent) {
- Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
- if (imageUri != null) {
-
- }
- }
-
- void handleSendMultipleImages(Intent intent) {
- ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
- if (imageUris != null) {
-
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- getMenuInflater().inflate(R.menu.recent, menu);
- return true;
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
-
-
-
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
Summary This article just tells us how to do the applications that integrate themselves using the sharing mechanism and so on. This is done using the intents and other classes as shown in the code.