How to receive data from other apps in android ??

ยท

2 min read

How to receive data from other apps in android ??

In android, apps can send data to other apps, as well as receive it from other apps.

To understand this concept, let's take the example of YouTube. When you click on share icon on YouTube, the share sheet will open and you will see a bunch of sharing options.

Have you noticed how these apps are listed in the share sheet? Because these apps are receiving some kind of data from other apps.

So, let's discover how to receive data from other apps.

Users often send data to other apps via the Android Share sheet. All received data has a MIME type set by the providing app.

Your app can receive data sent by another app according to the activity that matches the intent-filter tag in the manifest.

MIME types:

text/*, senders will often send text/plain, text/rtf, text/html, text/json

image/*, senders will often send image/jpg, image/png, image/gif

video/*, senders will often send video/mp4, video/3gp

Receiving data with an activity:

Update your manifest -

<activity android:name=".MainActivity" >
    <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>

Whenever another application tries to share any of these things by passing an Intent to startActivity(), your application will show up in the Android Share sheet.

If the user selects your application, the corresponding activity ( .MainActivity in the example above) will be launched.

Handle the incoming data -

You can retrieve the Intent object using getIntent() to handle the content of an Intent. Once you have the object, you can examine its contents to determine what to do next

override fun onCreate(savedInstanceState: Bundle?) {
     super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    when {
        intent?.action == Intent.ACTION_SEND -> {
            if ("text/plain" == intent.type) {
                handleSendText(intent) 
            } else if (intent.type?.startsWith("image/") == true) {
                handleSendImage(intent)
            }
        }
        intent?.action == Intent.ACTION_SEND_MULTIPLE
                && intent.type?.startsWith("image/") == true -> {
                handleSendMultipleImages(intent)
        }
        else -> {
            // Handle other intents, such as being started from the home screen
        }
    }

}

private fun handleSendText(intent: Intent) {
    intent.getStringExtra(Intent.EXTRA_TEXT)?.let {
        // Update UI to display text being shared
    }
}

private fun handleSendImage(intent: Intent) {
    (intent.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let {
        // Update UI to display image being shared
    }
}

private fun handleSendMultipleImages(intent: Intent) {
    intent.getParcelableArrayListExtra<Parcelable>(Intent.EXTRA_STREAM)?.let {
        // Update UI to display multiple images being shared
    }
}

References : developer.android.com/training/sharing/rece..

Thanks โค for reading my blog here. If you find it useful then share it with your friends.

Did you find this article valuable?

Support Shivam Gupta by becoming a sponsor. Any amount is appreciated!

ย