How to share app data with other applications in android?


In android using implicit intent, we can send data with other applications using ACTION_SEND action. We have to call Intent.createChooser() to open default chooser of android mobile to send the data. it may same or different in Android mobiles.

This example demonstrates how to share app data with other applications in android.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:id="@+id/layout"
   android:gravity="center"
   android:layout_height="match_parent"
   android:orientation="vertical">
   <TextView
      android:id="@+id/data"
      android:layout_width="match_parent"
      android:gravity="center"
      android:textSize="30sp"
      android:text="www.tutorialspoint.com"
      android:layout_height="wrap_content" />
</LinearLayout>

In the above code, it contains textview, when user clicks on text view it going to send textview data with other applications.

Step 3 − Add the following code to src/MainActivity.java

package com.example.andy.myapplication;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
   public int counter;
   @TargetApi(Build.VERSION_CODES.O)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      final TextView textView = findViewById(R.id.data);
      textView.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent i = new Intent();
            i.setAction(Intent.ACTION_SEND);
            i.setType("text/plain");
            i.putExtra(android.content.Intent.EXTRA_TEXT, textView.getText().toString());
            startActivity(Intent.createChooser(i, "Share to"));
         }
      });
}
}

In the above code contains textview, when you click on textview, it going to send the data using intent.

Step 4 − No need to change of manifest.xml file

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from an android studio, open one of your project's activity files and click Run Icon  from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

This is an initial screen when you click on tutorialspoint.com it will open default chooser from your Android device as shown below -

Select any on application to send the data through that application. Either we can send the data for one person or post the data on your wall, it depends on chooser applications.

Click here to download the project code

Updated on: 30-Jul-2019

913 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements