Android receive data from other applications

Some Android applications need an ability to receive data sent from other applications. For example an image viewer application is able to view an image that is clicked to view from any location on Android device. Similarly, and text editor application is able to view a text file clicked to open from the external storage. To achieve this goal, first you need to tell Android system the action that is performed on the data and the type of data that can be received in your application by defining intent filter in the AndroidManest.xml file. Then in your code, you will able to get the data and process it.



Now to have an example application on receiving data sent from other applications, you need to create a new Android project. Then modify the activity_main.xml file to add an ImageView. The example application is able to view an image that is clicked to view from any location of an Android device.

<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"
   >
   <ImageView
      android:id="@+id/imgview"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
       />
</LinearLayout>


After that, open the AndroidManifest.xml file to define an intent filter to tell Android that the action to be performed on the data is action view and the type of data is image. The DEFAULT category allows the Android system to capture intents that do not have a category. In most cases, we use this category.

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.androidexample"
   android:versionCode="1"
   android:versionName="1.0" >

   <uses-sdk
     android:minSdkVersion="8"
     android:targetSdkVersion="17" />

   <application
     android:allowBackup="true"
     android:icon="@drawable/ic_launcher"
     android:label="@string/app_name"
     android:theme="@style/AppTheme" >
     <activity
        android:name="com.example.androidexample.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
           <action android:name="android.intent.action.MAIN" />
           <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
     </activity>
   </application>

</manifest>


Finally in the onCreate() method of the MainActivity class (in the MainActivity.java file), you need code to get the image data, and display the image in the ImageView. The image data stored in the intent object sent from other applications can be gotten by using the getParcelableExtra() method of the Intent.

package com.example.androidexample;
import android.net.Uri;
import android.os.BatteryManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent=getIntent();
   String action=intent.getAction();    
   String type =intent.getType();
   if(Intent.ACTION_VIEW.equals(action) && type!="") {
     if (type.startsWith("image/")) {
     ImageView imgView=(ImageView)findViewById(R.id.imgview);
     Uri imgUri=intent.getParcelableExtra(Intent.EXTRA_STREAM);
     imgView.setImageURI(imgUri);
     }
     }

}

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

Android-receive-data-from-other-applications

Posted by: Dara | post date: 09-07-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.