Android Intents


Intents are one of latest additions to the android phones and are available only on API level 15 of android phones (IcecreamSandwich). They are quite powerful and make the life of a developer a lot easier.
Intents allow Android components to request functionality from other components of the Android system. For example an Activity can send an Intent to the Android system which starts another Activity. Therefore Intents allow to combine loosely coupled components to perform certain tasks. They are very useful in creating shortcuts to applications. Here is a simple application to call the email client using Intents on Android 4.0.3 emulator.

Here is the code for EmailClientActivity.java

package com.EmailClient;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class EmailClientActivity extends Activity {   
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}
	// Code for sending plain text email
	public void sendSimpleEmail(View button) {

		Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
		String[] recipients = new String[]{"my@email.com", "",};
		emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
		emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test");
		emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is email's message");
		emailIntent.setType("text/plain");
		startActivity(Intent.createChooser(emailIntent, "Send mail..."));
		finish();
	}
}

AndroidManifest.xml

<!--?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.EmailClient"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="15" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" 
             android:debuggable="true">
        <activity
            android:name=".EmailClientActivity"
            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>
</manifest>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/ScrollView01" android:layout_width="wrap_content"
	android:layout_height="wrap_content" android:scrollbars="vertical">
	<LinearLayout android:orientation="vertical"
		android:layout_width="fill_parent" android:layout_height="wrap_content"
		android:gravity="center">
		<TextView android:layout_width="fill_parent"
			android:layout_height="wrap_content" android:text="@string/hello"
			android:gravity="center_horizontal" android:layout_gravity="top"
			android:textSize="10pt" />
		<Button android:id="@+id/Button_Simple" android:layout_height="fill_parent"
			android:text="@string/buttontext_simpleemail" android:layout_width="fill_parent"
			android:onClick="sendSimpleEmail"></Button>
	</LinearLayout>
</ScrollView>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s