How to Send Simple Data to Other Apps?

What is Intent?

Intents are system messages, running around the inside of the device, notifying applications of various events, from hardware state changes (e.g.,an SD card was inserted), to incoming data (e.g., an SMS message arrived),to application events (e.g., your activity was launched from the device’s main menu).

What are we going to develop?

The application which we are going to develop will have couple of buttons. You can send text or images to other applications on clicking those button.
Need of send and receive data
Sending and receiving data (See How to Receive Simple Data from Other Apps?) between applications with intents is most commonly needed for social sharing of content. Intents allow users to share information quickly and easily, using their favorite applications.
Intent – Construction
Constructing a Intent involves five steps, they are:
  • Step 1: Create and instantiate Intent object
    1Intent sendIntent = new Intent();
  • Step 2: Specify the action you want the intent to trigger. If you want something to send to other applications, set the intent action as ‘ACTION_SEND’.
    1sendIntent.setAction(Intent.ACTION_SEND);
  • Step 3: Set the data you want to send to other applications like Text or Image.
    1sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
  • Step 4: Set the type of data you want to send using MIME types
    1sendIntent.setType("text/plain");
  • Step 5: Launch the activity.
    1startActivity(sendIntent);
Send Text Content
  • The most straightforward and common use of the ACTION_SEND action is sending text content from one activity to another.
    • For example, the built-in Browser app can share the URL of the currently-displayed page as text with any application. This is useful for sharing an article or website with friends via email or social networking.
  • Below is the code to implement this type of sharing:
1Intent sendIntent = new Intent();
2    // Set the action to be performed i.e 'Send Data'
3    sendIntent.setAction(Intent.ACTION_SEND);
4    // Add the text to the intent
5    sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
6    // Set the type of data i.e 'text/plain'
7    sendIntent.setType("text/plain");
8    // Launches the activity; Open 'Text editor' if you set it as default app to handle Text
9    startActivity(sendIntent);
  • When the code is executed, it launches the default application (For ex: Text editor) you set for handling text in your device
  • When no default application was set, it shows the chooser with list of applications (as shown below) that are capable of handling text, from which you can select one.
  • Optionally, you can set some standard extras for the intent to send data to Email applications like GMail App: EXTRA_EMAIL, EXTRA_CC, EXTRA_BCC, EXTRA_SUBJECT. If the receiving application is not designed to use them, it simply ignores them.
Send Binary Content
  • Binary data (most commonly Images) is shared using the ACTION_SEND action combined with setting the appropriate MIME type (image/png. image/jpg) and placing the URI to the data in an extra named EXTRA_STREAM.
    1String imagePath = Environment.getExternalStorageDirectory()
    2            + "/pgguru/" + "/share_this_image.png";
    3    File imageFileToShare = new File(imagePath);
    4    // Get the URI of the image 'share_this_image.png' exists under pgguru right under SD card
    5    Uri uri = Uri.fromFile(imageFileToShare);
    6    // Set the action to be performed i.e 'Send Data'
    7    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    8    // Add the URI holding a stream of data 
    9    sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
    10    // Set the type of data i.e 'image/* which means image/png, image/jpg, image/jpeg etc.,'
    11    sendIntent.setType("image/*");
    12    // Launches the activity; Open 'Gallery' if you set it as default app to handle Image
    13    startActivity(sendIntent);
  • This is commonly used to share an image but can be used to share any type of binary content

Comments

Popular posts from this blog

Styling Bottom Navigation

Lottie Animation in Android

Set Up the AWS Mobile SDK for Android