Documente Academic
Documente Profesional
Documente Cultură
What is Intent?
o Intents are system messages, running around the inside of the device, notifying
applications of various events, from hardware state changes (e.g.,an SD card was
inserted), to incoming data (e.g., an SMS message arrived),to application events
(e.g., your activity was launched from the device’s main menu).
Not only can you respond to intents, but you can create your own, to launch other
activities, or to let you know when specific situations arise (e.g., raise such-and-so intent
when the user click this button).
Intents are asynchronous messages which allow Android components to request
functionality from other components of the Android system. For example an Activity can
send an Intents to the Android system which starts another Activity.
Three of the core components of an application — activities, services, and broadcast
receivers — are activated through messages, called intents [Read more at android
developers site].
Screen1 contains three controls – TextView shows ‘Enter your name’ text, Edittext to get name
from the user and Button to take user to next screen
Screen2 contains only one control – TextView which displays greet message
Project Structure
Layout creation:
Create new android project [File >> New >> Android Project] with project name IntentExample
Click next and select target android device version [I chose version 2.2]
Click next and enter package name – com.prgguru.android
Click finish
Code Listings
3 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4 android:layout_width="fill_parent"
android:layout_height="fill_parent"
5
android:orientation="vertical"
6
<TextView
7
android:id="@+id/textView1"
8
android:layout_width="match_parent"
9
android:layout_height="wrap_content"
10 android:text="Enter your name"
11 android:textAppearance="?android:attr/textAppearanceLarge" />
12 <EditText
13 android:id="@+id/editText1"
14 android:layout_width="match_parent"
android:layout_height="wrap_content"
15
android:ems="10" >
16
<requestFocus />
17
</EditText>
18
<Button
19
android:id="@+id/button1"
20 android:layout_width="match_parent"
21 android:layout_height="wrap_content"
23</LinearLayout>
24
1
<?xml version="1.0" encoding="utf-8"?>
2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical" >
6 <TextView
7 android:id="@+id/textView1"
8 android:layout_width="match_parent"
android:layout_height="wrap_content"
9
android:textAppearance="?android:attr/textAppearanceLarge"
10
android:text="TEXT"/>
11
</LinearLayout>
12
1. IntentExampleActivity.java
2. GreetingActivity.java
1. IntentExampleActivity.java
1EditText nameEditCtrl;
2Button btnCtlr;
3String name;
Refer the controls and set the listener for button inside onCreate method of
IntentExampleActivity class:
3btnCtlr.setOnClickListener(new ButtonClickHandler());
10}
16intObj.putExtra("USERNAME", name);
17//Start GreetingActivity
18startActivity(intObj);
}
19
}
20
21
2. GreetingActivity.java
Create Java class named ‘GreetingActivity’ under com.prgguru.android package and replace the
default skeleton with below one:
1 package com.prgguru.android;
2 import android.app.Activity;
3 import android.content.Intent;
4 import android.os.Bundle;
5 import android.widget.TextView;
6 public class GreetingActivity extends Activity {
TextView greetMsg;
7 /** Called when the activity is first created. */
8 @Override
9 public void onCreate(Bundle savedInstanceState) {
10 super.onCreate(savedInstanceState);
setContentView(R.layout.greeting);
11 greetMsg = (TextView) findViewById(R.id.textView1);
12 //Assign the intent that started this activity
13 Intent intename = getIntent();
14 //Get the USERNAME passed from IntentExampleActivity
15 String uname = (String) intename.getSerializableExtra("USERNAME");
//Set text for greetMsg TextView
16 greetMsg.setText("Welcome " + uname);
17 }
18
19}
20
21
22
<activity android:name=".GreetingActivity"
android:label="@string/app_name" >
</activity>
r Android device
Posts Tagged How to move from one activity
to another using an intent
Now, lets see how the components in android communicate.
Communication between the three core components in android-activity, service and broadcast
receiver is made possible using intents. Intents can be said as messages passed between these
components. An Intent object is a bundle of information containing information such as the
action to be performed, data on which the action is to be performed etc.
Explicit intents: They specify the target component by its name.That is, they explicitly specify
the target component which needs to respond to it.
Here is how it is done:
Or
Lets see what an explicit intent is and how it works through a simple example.