Android programming using Intents

By

September 10, 2012AndroidNo comments

This tutorial is the part of Android Development series

1.What is Android ?
2.Hello world mobile application using android.
3.What is Android activity ?
4.Understanding Android layouts
5.Introduction to AndroidManifest file
6.Android programming using Intents

Android Intents are the general abstraction of the activity that comes next.Intents are like messaging components that communicate across activities.Imagine you have created an activity in your application and you want that activity to be interacive.To be interactive means to communicate with other services and components.

Intents does the task of communication between activities.Usually Intents are used to start other activities.But also Intents can share data across activities which is very useful when we create interactive applications.

 

In our example we are going to see how the simple Intent works in Android.

Objective: Trigger a system activity by using android intents.

An activity which is already predefined in android can be used for this demonstration.We are going to place a button which will trigger the dialer application when we press the button.Dialer is the default activity defined in the core android system which we can start using the Intents.

1.Create a New project in android

I have selected Minimum required sdk to API 14 but you can choose it as per you want it.

android intents project

 

android intents- blank activity

 

android intents

 

 

Activity Name: MainActivity

Layout Name:activity_main

Open the activity_main.xml file in the res->layout folder and add a button to the screen

Layout:activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<relativelayout 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" >

    <button android:id="@+id/button1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="59dp"
       android:text="Click to dial" />

</relativelayout>

android intents

In the MainActivity.Java you need to write the code for the button action.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
Button b;

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

Intent i=new Intent(Intent.ACTION_DIAL);
startActivity(i);
}
});
}

Intent.ACTION_DIAL will initiate the dialer activity when you click the button.

android intent

Do you think this Android Development tutorial is useful ?? If Yes, Share/Comment below.You can also Get Regular Updates. Subscribe to Free RSS Feeds or Email Updates. Follow us on  Twitter @Devlup and Like us on Facebook.

Leave a Reply

*