Friday 29 July 2011

Java Preprocessor

If you do a google search for java preprocessor you are likely to get a pretty sparse set of results. There are a few projects out there that provide tools to solve this problem but I needed something with the following characteristics:
  1. C preprocessor like syntax with configurable macro prefix
  2. solid Ant integration
    • properties defined in build script visible to preprocessor
    • only process source files newer than target file
  3. open source license
Finally got frustrated enough to just write my own. The end result is a tool called javapp.jar and it is licensed under the GNU GPL.
The core processor logic is implemented in Python and runs under the Java runtime using Jython. I used an excellent lexical analysis module called Plex by Greg Ewing.
Using Python has the distinct advantage of reducing development time (~ 6 hours), but sadly there is a rather significant performance hit. Processing a large project (~ 100 files) for the first time can take upwards of two minutes! Subsequent builds should be much quicker since the ant task will only process source files newer than the destination file.

Downloading

The latest version is javapp-04.zip.


Getting Started

Extract the zip file and put javapp.jar somewhere Ant can find it. A good permanent spot is $ANT_HOME/lib.
Define and run the task:
<taskdef resource="javapp-defs.xml" />
 
<property name="os_version" value="4.2" />
 
<target name="preprocess">
   <javapp destdir="staging" prefix="//#">
      <fileset dir="src" includes="**/*.java" />
      <property name="poo" value="smelly" />
   </javapp>
</target>
Turns this:
public class Poo {
   public static void main(String[] args) {
      //#if defined(poo)
      System.out.println("Poo is ${poo}");
      //#else
      System.out.println("Poo is not defined");
      //#endif
 
      //#if ${os_version} == 4.1
      System.out.println("OS 4.1");
      //#elif defined(poo) and ${os_version} >= 4.2
      System.out.println("OS 4.2 and above");
      System.out.println("Ooooh; compound expressions");
      //#endif
   }
}
Into this:
public class Poo {
   public static void main(String[] args) {
 
      System.out.println("Poo is smelly");
 
 
 
      System.out.println("OS 4.2 and above");
      System.out.println("Ooooh; compound expressions");
 
   }
}

Thursday 21 July 2011

Reading and writing Serializable objects

See following example :


/**
 *
 * @author prasobh
 */
import java.io.*;
import java.util.*;

public class ExerciseSerializable {

    public static void main(String... aArguments) {
        //  create a Serializable Map
        Map<Integer, String> names = new HashMap<Integer, String>();
        names.put(1, "prasobh");
        names.put(2, "mohanlal");
        names.put(3, "mammokka");



        try {
            //  use buffering
            OutputStream file = new FileOutputStream("names.store");
            OutputStream buffer = new BufferedOutputStream(file);
            ObjectOutput output = new ObjectOutputStream(buffer);
            try {
                output.writeObject(names);
            } finally {
                output.close();
            }
        } catch (IOException ex) {
            System.out.println("Exception " + ex);
        }

        // deserialize the quarks.ser file


        try {
            //use buffering
            InputStream file = new FileInputStream("names.store");
            InputStream buffer = new BufferedInputStream(file);
            ObjectInput input = new ObjectInputStream(buffer);
            try {
                //deserialize the List

                Map<Integer, String> recoverednames = (Map<Integer, String>) input.readObject();

                //display its data

                System.out.println("recoverednames: " + recoverednames);

            } finally {
                input.close();
            }
        } catch (ClassNotFoundException ex) {
            System.out.println("Exception " + ex);
        } catch (IOException ex) {
            System.out.println("Exception " + ex);
        }
    }
}




Saturday 9 July 2011

Gradient buttons for android

 

Blue button

<?xml version="1.0" encoding="utf-8"?>
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#449def" />
            <stroke
                android:width="1dp"
                android:color="#2f6699" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#449def"
                android:endColor="#2f6699"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#2f6699" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>
 

Red button

<?xml version="1.0" encoding="utf-8"?>
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#ef4444" />
            <stroke
                android:width="1dp"
                android:color="#992f2f" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#ef4444"
                android:endColor="#992f2f"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#992f2f" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>
 

Purple button

 

<?xml version="1.0" encoding="utf-8"?>
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#a276eb" />
            <stroke
                android:width="1dp"
                android:color="#6a3ab2" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#a276eb"
                android:endColor="#6a3ab2"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#6a3ab2" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>
 

Green button

<?xml version="1.0" encoding="utf-8"?>
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#70c656" />
            <stroke
                android:width="1dp"
                android:color="#53933f" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#70c656"
                android:endColor="#53933f"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#53933f" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>
 

Yellowbutton

<?xml version="1.0" encoding="utf-8"?>
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#f3ae1b" />
            <stroke
                android:width="1dp"
                android:color="#bb6008" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#f3ae1b"
                android:endColor="#bb6008"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#bb6008" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>
 

Blackbutton

<?xml version="1.0" encoding="utf-8"?>
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#343434" />
            <stroke
                android:width="1dp"
                android:color="#171717" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#343434"
                android:endColor="#171717"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#171717" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>
 
All text on the buttons will have the same styles we can define a style in strings.xml:

<style name="ButtonText">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">#ffffff</item>
    <item name="android:gravity">center</item>
    <item name="android:layout_margin">3dp</item>
    <item name="android:textSize">30dp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:shadowColor">#000000</item>
    <item name="android:shadowDx">1</item>
    <item name="android:shadowDy">1</item>
    <item name="android:shadowRadius">2</item>
</style>
 
Source code of an activity layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">
   <Button android:text="Button" android:id="@+id/button1" android:background="@drawable/btn_red" style="@style/ButtonText"></Button>
   <Button android:text="Button" android:id="@+id/button2" android:background="@drawable/btn_blue" style="@style/ButtonText"></Button>
   <Button android:text="Button" android:id="@+id/button3" android:background="@drawable/btn_purple" style="@style/ButtonText"></Button>
   <Button android:text="Button" android:id="@+id/button4" android:background="@drawable/btn_green" style="@style/ButtonText"></Button>
   <Button android:text="Button" android:id="@+id/button5" android:background="@drawable/btn_orange" style="@style/ButtonText"></Button>
   <Button android:text="Button" android:id="@+id/button6" android:background="@drawable/btn_black" style="@style/ButtonText"></Button>
</LinearLayout>
 

 

 

 

 

 

 

 

 

 

 

Tuesday 5 July 2011

Android : Custom Views

Step 1:

We’ll start off by extending our class by the android.view.View

example:

public class DrawView extends View {
        /**
         * Constructor
         */
        public DrawView(Context context) {
            super(context);
        }
    }

Step 2:

Next we’ll use the onDraw() function provided by the View class.

( Tip: use Ctrl+space to see the available methods and select onDraw(…) )

It will look something like this…

public class DrawView extends View {

        /**
         * Constructor
         */
        public DrawView(Context context) {
            super(context);
        }

        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
        }
    }


Step 3:

Lets draw some Text in it..

For that we’ll need the paint class.

Let’s create an object of Paint..

private Paint paint = new Paint();


The Paint class holds the style and color information about how to

draw geometries, text and bitmaps. )

Let’s set its properties..

// set's the paint's colour
paint.setColor(Color.WHITE);
// set's paint's text size
paint.setTextSize(25);
// smooth's out the edges of what is being drawn
paint.setAntiAlias(true);


Step 4:

Let’s draw the actual text…

 canvas.drawText("Hello World", 5 , 30 ,paint);



The final code will look some thing like this…

public class DrawView extends View {

        private Paint paint;

        /**
         * Constructor
         */
        public DrawView(Context context) {
            super(context);

            paint = new Paint();
            // set's the paint's colour
            paint.setColor(Color.GREEN);
            // set's paint's text size
            paint.setTextSize(25);
            // smooth's out the edges of what is being drawn
            paint.setAntiAlias(true);
        }

        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            canvas.drawText("Hello World", 5, 30, paint);
            // if the view is visible onDraw will be called at some point
            // in the future
            invalidate();
        }
    }


Step 5:

Now we need to display this View ….

For that we need to create an activity and set its content view as this DrawView.

One way is to suclass DrawView or create a new class file of DrawView..

The final code looks something like this…

package opensourzesupport.android.canvas

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;

public class temp extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new DrawView(this));
    }

    private class DrawView extends View {

        private Paint paint;
        /**
         * Constructor
         */
        public DrawView(Context context) {
            super(context);

            paint = new Paint();
            // set's the paint's colour
            paint.setColor(Color.GREEN);
            // set's paint's text size
            paint.setTextSize(25);
            // smooth's out the edges of what is being drawn
            paint.setAntiAlias(true);
        }

        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            canvas.drawText("Hello World", 5, 30, paint);
            // if the view is visible onDraw will be called at some point in the
            // future
            invalidate();
        }
    }
}

Monday 4 July 2011

Android Intents

1. Android Intents

1.1. Overview

Objects of type "android.content.Intent" are used to send asynchronous messages within your application or between applications. Intents allow to send or receive data from and to other activities or services. They also allow to broadcast that a certain event has occurred.
Intents are a powerful concept as they allow the creation of loosely coupled applications. Intents can be used to communicate between any installed application component on the device.
An Intent object can contain information for the receiving component. For example if your application calls via an Intent a browser it may send the URL to the browser component. An Intent also contain information for the Android system so that the Android system can determine which component should handle the request.

1.2. Implicit vrs Explicit Intents

Android supports explicit intents and implicit intents. Explicit intent names the component, e.g. the Java class which should be called.
Implicit intents asked the system to perform a service without telling the system which Java class should do this service. In constructing an implicit Intent you specify the action which should be performed and optionally an URI which should be used for this action. For example you could tell the system that you want to view (action) a webpage (URI). By starting an intent for this data the system would try to find an application which is registered for this event, e.g. a browwer. You can add more data to the Intent by adding "extras" to the Intent. These are key/value pairs.

1.3. Getting the Intent data in the called Activity

To get the Intent information in the called Activity use the method getIntent(). If the Activity was called via an implicit Intent you can receive the data and url from this Intent via getAction(), getData() and getExtras().

1.4. Intent Filter

The Android system will determine suitable applications for an implicit intent and if several applications exists offer the user the choice to open one. The determination is based on intent filters, e.g. the class "android.content.IntentFilter". Intent filters are typically defined via the "AndroidManifest.xml" file.
To react to a certain implicit intent an application component must register itself via an IntentFilter in the "AndroidManifest.xml" to this event. If a component does not define intent filters it can only be called by explicit intents.

1.5. Intents as event triggers

Intents can also be used to broadcast messages into the Android system. An Android application can register Broadcast Receivers to these events and react accordingly. The Android system also uses Intents to broadcast system events. Your application can also register to these system events, e.g. a new email has arrived, system boot is complete or a phone call is received and react accordingly.

1.6. Starting Activities and Sub-Activities

To start an activity use the method startActivity(Intent) if you do not need a return value from the called activity.
If you need some information from the called activity use the method startActivityForResult(). Once the called Activity is finished the method onActivityResult() in the calling activity will be called. If you use startActivityForResult() then the activity which is started is considered a "Sub-Activity

2. Implicit Intents - Opening an URL

The following creates an examle project for calling several implicit intent. The Android system is asked to display a URI and chooses the corresponding application for the right URI. Create a new Android application "opensourzesupport.android.intent.implicit" with the Activity "CallIntents". Create the following view layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<Button android:id="@+id/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Call browser" 
android:onClick="callIntent"></Button>
<Button android:id="@+id/Button02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Call Someone" 
android:width="100px" android:onClick="callIntent"></Button>
<Button android:id="@+id/Button03" android:layout_width="wrap_content" 
android:layout_height="wrap_content" android:text="Dial" 
android:width="100px" android:onClick="callIntent"></Button>
<Button android:id="@+id/Button04" android:layout_width="wrap_content" 
android:layout_height="wrap_content" android:text="Show Map" 
android:width="100px" android:onClick="callIntent"></Button>
<Button android:id="@+id/Button05" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Search on Map" 
android:width="100px" android:onClick="callIntent"></Button>
<Button android:id="@+id/Button06" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Take picture"
android:width="100px" android:onClick="callIntent"></Button>
<Button android:id="@+id/Button07" android:layout_width="wrap_content" 
android:layout_height="wrap_content" android:text="Show contacts" 
android:width="100px" android:onClick="callIntent"></Button>
<Button android:id="@+id/Button08" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Edit first contact" 
android:width="100px" android:onClick="callIntent"></Button>

</LinearLayout>
 
To be able to use certain intents you need to register then for
  your application. Maintain the following "AndroidManifest.xml". 
 
 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="de.opensourzesupport.android.intent.implicit"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".CallIntents"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="9" />


<uses-permission android:name="android.permission.CALL_PRIVILEGED"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
</manifest>  

Change your activity to the following. We will start the new intent with the method startActivityForResult() which allow us to specify a desired result code. Once the intent is finished the method onActivityResult() is called and you can perform actions based on the result of the activity.

package opensourzesupport.android.intent.implicit;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class CallIntends extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 }

 public void callIntent(View view) {
  Intent intent = null;
  switch (view.getId()) {
  case R.id.Button01:
   intent = new Intent(Intent.ACTION_VIEW,
     Uri.parse("http://www.opensourzesupport.blogspot.cxom"));
   startActivity(intent);
   break;
  case R.id.Button02:
   intent = new Intent(Intent.ACTION_CALL,
     Uri.parse("tel:(+49)12345789"));
   startActivity(intent);
   break;
  case R.id.Button03:
   intent = new Intent(Intent.ACTION_DIAL,
     Uri.parse("tel:(+49)12345789"));
   startActivity(intent);
   break;
  case R.id.Button04:
   intent = new Intent(Intent.ACTION_VIEW,
     Uri.parse("geo:50.123,7.1434?z=19"));
   startActivity(intent);
   break;
  case R.id.Button05:
   intent = new Intent(Intent.ACTION_VIEW,
     Uri.parse("geo:0,0?q=query"));
   startActivity(intent);
   break;
  case R.id.Button06:
    intent = new Intent("android.media.action.IMAGE_CAPTURE");
   startActivityForResult(intent, 0);
   break;
  case R.id.Button07:
   intent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people/"));
   startActivity(intent);
   break;
  case R.id.Button08:
   intent = new Intent(Intent.ACTION_EDIT, Uri.parse("content://contacts/people/1"));
   startActivity(intent);
   break;
  default:
   break;
  }
 }
 
 @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (resultCode == Activity.RESULT_OK && requestCode == 0) {
     String result = data.toURI();
     Toast.makeText(this, result, Toast.LENGTH_LONG);
   }
 }
}


 If you start your application you should see an list of buttons and if you press the button, different activities should be performed. Note that you do not specify any specific application.

3. Explicit intents and data transfer between activities

The following demonstrates how you can transfer data between two activities. We will use explicit intents in this example and create two activities. The first activity will call the second one via an explicit intent. This second activity will receive data from the first one via the class "Bundle" which can be retrieved via intent.getExtras().
The second activity can be finished either via the back button on the phone or via the button. The method finish() is performed in this case. In this method you can transfer some data back to the calling activity. THis is possible because we use the method startActivityForResult(). If you start an activity via this method the method onActivity result is called on the calling activity once the called activity is finshed.
Create a new Android application "opensourzesupport.android.intent.explicit" with the Activity "ActivityOne". Change the layout "main.xml" to the following.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content"
android:layout_height="wrap_content"><TextView android:id="@+id/TextView01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="First Activity. Press button to call second activity"
android:minHeight="60dip" android:textSize="20sp"></TextView></LinearLayout>
<LinearLayout android:id="@+id/LinearLayout02" android:layout_width="wrap_content" 
android:layout_height="wrap_content"></LinearLayout>
<Button android:id="@+id/Button01" android:layout_width="wrap_content" 
android:layout_height="wrap_content" android:onClick="onClick" 
android:text="Calling an intent"></Button>
</LinearLayout> 
 
Create the layout "second.xml". 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">

 <LinearLayout android:id="@+id/LinearLayout01"
  android:layout_width="wrap_content" android:layout_height="wrap_content">
  <TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
   android:layout_height="wrap_content" android:text="First value from Activity 1"></TextView>
  <EditText android:text="@+id/EditText01" android:id="@+id/EditText01"
   android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText>
 </LinearLayout>
 <LinearLayout android:id="@+id/LinearLayout02"
  android:layout_width="wrap_content" android:layout_height="wrap_content">
  <TextView android:id="@+id/TextView02" android:layout_width="wrap_content"
   android:layout_height="wrap_content" android:text="Second Value from Activity one"/>
  <EditText android:text="@+id/EditText02" android:id="@+id/EditText02"
   android:layout_width="wrap_content" android:layout_height="wrap_content"/>
 </LinearLayout>
 <Button android:id="@+id/Button01" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:onClick="onClick"
  android:text="Finished this activity"></Button>
</LinearLayout>
Create a new activity "ActivityTwo" via the AndroidManifest. 
 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="opensourzesupport.android.intent.explicit"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ActivityOne"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    <activity android:label="ActivityTwo" android:name="ActivityTwo"></activity>
</application>
    <uses-sdk android:minSdkVersion="9" />

</manifest> 
 Create   the following coding for your two activities. The second   activity will   be called from the first one, displays the transferred   data and if you   select the button of the back button on the phone you   send some data   back tot the calling application.

package opensourzesupport
.android.intent.explicit;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class ActivityOne extends Activity {
 private static final int REQUEST_CODE = 10;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 }

 public void onClick(View view) {
  Intent i = new Intent(this, ActivityTwo.class);
  i.putExtra("Value1", "This value one for ActivityTwo ");
  i.putExtra("Value2", "This value two ActivityTwo");
  // Set the request code to any code you like, you can identify the
  // callback via this code
  startActivityForResult(i, REQUEST_CODE);
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
   if (data.hasExtra("returnKey1")) {
    Toast.makeText(this, data.getExtras().getString("returnKey1"),
      Toast.LENGTH_SHORT).show();
   }
  }
 }
}
 
package opensourzesupport
.android.intent.explicit;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class ActivityTwo extends Activity {

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle bundle) {
  super.onCreate(bundle);
  setContentView(R.layout.second);
  Bundle extras = getIntent().getExtras();
  if (extras == null) {
   return;
  }
  String value1 = extras.getString("Value1");
  String value2 = extras.getString("Value2");
  if (value1 != null && value2 != null) {
   EditText text1 = (EditText) findViewById(R.id.EditText01);
   EditText text2 = (EditText) findViewById(R.id.EditText02);
   text1.setText(value1);
   text2.setText(value2);
  }
 }

 public void onClick(View view) {
  finish();
 }

 @Override
 public void finish() {
  Intent data = new Intent();
  data.putExtra("returnKey1", "Swinging on a star. ");
  data.putExtra("returnKey2", "You could be bettern then you are. ");
  setResult(RESULT_OK, data);
  super.finish();
 }
}