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
- 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’.
- Step 3: Set the data you want to send to other applications like Text or Image.
- Step 4: Set the type of data you want to send using MIME types
- Step 5: Launch the activity.
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:
- 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.
- This is commonly used to share an image but can be used to share any type of binary content
Comments
Post a Comment