Android send data to other applications

When you build an application that needs to send data to other applications, this tip might be useful to you. In this Android tip, I am going to show you how to send data from your application to other applications using Intent. Intent allows you to send and receive data easily. You can specify an action for the intent to trigger, data, and type of data. For example, to send a text message to other applications, you need to specify Intent.ACTION_SEND for action, text to be sent, and text/plain for type of data using the setAction(), putExtra(), and setType() methods respectively. After an intent was constructed, you can call the startActivity() method to send the data. This method has one argument: the intent to be sent or an intent chooser that wraps the intent. If you use the intent chooser (by calling Intent.createChooser()), Android will always display the chooser dialog even if the user has previously selected a default action for this intent. You can specify a title for the chooser dialog.

It is a good idea to check whether there are applications available to receive the data before starting to send the data. You use PackageManager class to do the task. The queryIntentActivities() method of the PackageManager is able to query all applications on an Android device that can receive the data.

Now to have an example application on sending data to other application, you create a new Android project. Then modify the activity_main.xml file to add on EditText and one Button. The button is clicked to send the text in the EditText to other applications.


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"  
   tools:context=".MainActivity"
   android:orientation="vertical"
   >
  
   <EditText
      android:id="@+id/txt"
      android:layout_width="fill_parent"
      android:layout_height="100dp"
      android:textSize="20sp"
      android:inputType="text"          
      
      />
   <Button
      android:id="@+id/bt_send"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:onClick="sendIt"
      android:text="Send"
      
      />
  
</LinearLayout>


In the MainActivity class, add the sendIt() method as shown below. It will be called when the user clicked the Send button.

package com.example.androidexample;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
  
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

public void sendIt(View view){
EditText edit=(EditText)findViewById(R.id.txt);
//create an intent object
Intent intent= new Intent();
//specify an action for intent to trigger
intent.setAction(Intent.ACTION_SEND);
//put text
intent.putExtra(Intent.EXTRA_TEXT, edit.getText().toString());
//set data type
intent.setType("text/plain");
//check available applications to receive the text
PackageManager pm=getPackageManager();
   List<ResolveInfo> vapps=pm.queryIntentActivities(intent, 0);
   if(vapps.size()>0){
   //send the data
   startActivity(Intent.createChooser(intent,"Send Text"));
   }

}

}


If you want to send a file instead of the simple text message, you need to use Intent.EXTRA_STREAM and path of the file in the putExtra() method and the type of data is "*/*".

String file_path= Environment.getExternalStorageDirectory() + "/myimage.jpg";
Uri urlpath=Uri.parse("file://"+file_path);
Intent intent= new Intent(); intent.setAction(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_STREAM,urlpath);

Android-send-data-to-other-applications

Posted by: Dara | post date: 09-17-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.