Android Get Data From Other Applications In 2 Steps

Tram Ho

Android apps can share data between apps easily. The shared data includes text, image, video, aduio file, etc. For example, you can share an image from the Google Photo application to the Facebook application or the Instagram application. Today it is a common feature in social apps on Android.

To receive data from other applications, we need to coinfig Activity in AndroidMainifest.xml and handle Intent containing action and data sent from the system. Implementing this sharing feature only needs 2 steps in about 10 minutes. I trust that you will find this article interesting and useful. We often start!

Step 1. Add <Intent-filter> In AndroidManifest

We add 4 tags to Activity to handle the incoming data in AndroidMainifest.xml. They are:

  1. <intent-filter> To say that this Activity is applied to handle the Intent system with the action and data identified in the corresponding tags as <action> and <data> 2. <action> Defines IntentAction with which this Activity can handle. android.intent.action.SEND receives a single data and android.intent.action.SENDMULTIPLE receives a list data. The action field is used by the system to filter out applications that are ready to handle Intent requests.
  2. <category> android.intent.category.DEFAULT must be defined at * <category> * otherwise the Activity cannot receive the implicit Intent (implicit Intent) therefore cannot be displayed in the sharing dialog below.
  3. ** <data> ** Declare the type of MINE ((Multipurpose Internet Mail Extensions) to pay attention to this Activity. There are several common types:
  • text / * this includes text / plain, text / rtf, text / html, text / json
  • image / * this includes image / jpg, image / png, image / gif
  • video / * this includes video / mp4, video / 3gp
  • audio / * this includes audio / wav
  • application / pdf * this includes pdf file

At this point you can see your application icon showing the dialog sharing. The title in bold (bold) is the name of the application and the title below is the name of the Activity declared in AndroidManifest.xml. Look below:

Step 2 Process Data At Activity

After clicking on the app icon at dialog sharing, your defined Activity will run and be initialized. The data sent (for example text, image, videok audio URI …) can be retrieved from the incoming Intent at onCreate () with different key values. Before it all starts, the line (7-8) in the above code is needed to determine that the intent was sent from ACTIONSEND or ACTIONSENDMULTIPLE. Also the data mine type is also checked to ensure that the correct key and The function is used later to retrieve the data that has been sent

At line 14-15 the submitted text can be retrieved from the Intent.EXTRATEXT key

At line 20-21 the URI of the submitted image can be retrieved from the Intent.EXTRASTREAM key

At line 20-21, the URI of many sent images can be retrieved from the Intent.EXTRASTREAM key but with another function getParcelableArrayListExtra (key: String)

Summary

  1. Activity must be declared with <intent-filter> to tell the system that this Activity can handle but certain Intents with actions and data are defined in <action> and <data> tags.
  2. android.intent.action.SEND or android.intent.action.SENDMULTIPLE is declared in the <action> tag to receive data from other apps.
  3. android.intent.category.DEFAULT must be defined in the <category> tag to display the app in the sharing dialog
  4. The MINE type is declared in the <data> tag to tell the system which data type this Activity can handle eg text /, image / ……
  5. Both IntentAction and data type must be checked at the specific Activity ‘s callback * onCreate () *. Intent.EXTRATEXT is processed to retrieve text and Intent.EXTRASTREAM is used to retrieve sent image.

The Above Article Is Referenced From Source: https://itnext.io/android-receiving-data-in-2-steps-21c49920172d

Share the news now

Source : Viblo