Sharing Simple Data Between Android Applications

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.

data sharing image

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.

  1. Intent sendIntent = new Intent();  
  2. sendIntent.setAction(Intent.ACTION_SEND);  
  3. sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");  
  4. sendIntent.setType("text/plain");  
  5. 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:

  1. Intent sendIntent = new Intent();  
  2. sendIntent.setAction(Intent.ACTION_SEND);  
  3. sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");  
  4. sendIntent.setType("text/plain");  
  5. 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 Content

Binary 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 Content

An 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.
  1. ArrayList<Uri> imageUris = new ArrayList<Uri>();  
  2. imageUris.add(imageUri1); // Add your image URIs here  
  3. imageUris.add(imageUri2);  
  4.   
  5. Intent shareIntent = new Intent();  
  6. shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);  
  7. shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);  
  8. shareIntent.setType("image/*");  
  9. startActivity(Intent.createChooser(shareIntent, "Share images to.."));  
Receiving Simple Data From Another Application

We 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 file

Intent 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.
  1. <activity android:name=".ui.MyActivity" >  
  2.    <intent-filter>  
  3.       <action android:name="android.intent.action.SEND" />  
  4.       <category android:name="android.intent.category.DEFAULT" />  
  5.       <data android:mimeType="image/*" />  
  6.    </intent-filter>  
  7.    <intent-filter>  
  8.       <action android:name="android.intent.action.SEND" />  
  9.       <category android:name="android.intent.category.DEFAULT" />  
  10.       <data android:mimeType="text/plain" />  
  11.    </intent-filter>  
  12.    <intent-filter>  
  13.       <action android:name="android.intent.action.SEND_MULTIPLE" />  
  14.       <category android:name="android.intent.category.DEFAULT" />  
  15.       <data android:mimeType="image/*" />  
  16.    </intent-filter>  
  17. </activity>  
Handling the Incoming Content

Since the manifest file was updated with the specifications we must code the Java part.
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.view.Menu;  
  4. import android.view.MenuItem;  
  5.   
  6.   
  7. public class RecentActivity extends Activity {  
  8.   
  9.     @Override  
  10.     void onCreate (Bundle savedInstanceState) {  
  11.         ...  
  12.         // Get intent, action and MIME type  
  13.         Intent intent = getIntent();  
  14.         String action = intent.getAction();  
  15.         String type = intent.getType();  
  16.   
  17.         if (Intent.ACTION_SEND.equals(action) && type != null) {  
  18.             if ("text/plain".equals(type)) {  
  19.                 handleSendText(intent); // Handle text being sent  
  20.             } else if (type.startsWith("image/")) {  
  21.                 handleSendImage(intent); // Handle single image being sent  
  22.             }  
  23.         } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {  
  24.             if (type.startsWith("image/")) {  
  25.                 handleSendMultipleImages(intent); // Handle multiple images being sent  
  26.             }  
  27.         } else {  
  28.             // Handle other intents, such as being started from the home screen  
  29.         }  
  30.         ...  
  31.     }  
  32.   
  33.     void handleSendText(Intent intent) {  
  34.         String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);  
  35.         if (sharedText != null) {  
  36.             // Update UI to reflect text being shared  
  37.         }  
  38.     }  
  39.   
  40.     void handleSendImage(Intent intent) {  
  41.         Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);  
  42.         if (imageUri != null) {  
  43.             // Update UI to reflect image being shared  
  44.         }  
  45.     }  
  46.   
  47.     void handleSendMultipleImages(Intent intent) {  
  48.         ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);  
  49.         if (imageUris != null) {  
  50.             // Update UI to reflect multiple images being shared  
  51.         }  
  52.     }  
  53.     @Override  
  54.     public boolean onCreateOptionsMenu(Menu menu) {  
  55.         // Inflate the menu; this adds items to the action bar if it is present.  
  56.         getMenuInflater().inflate(R.menu.recent, menu);  
  57.         return true;  
  58.     }  
  59.   
  60.     @Override  
  61.     public boolean onOptionsItemSelected(MenuItem item) {  
  62.         // Handle action bar item clicks here. The action bar will  
  63.         // automatically handle clicks on the Home/Up button, so long  
  64.         // as you specify a parent activity in AndroidManifest.xml.  
  65.         int id = item.getItemId();  
  66.         if (id == R.id.action_settings) {  
  67.             return true;  
  68.         }  
  69.         return super.onOptionsItemSelected(item);  
  70.     }  
  71. }  
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.

Up Next
    Ebook Download
    View all
    Learn
    View all