Intent is one of the things i need to master. So what must i know about Intent? Basically, I have to know what it is and how to use.
What is an Intent?
In simple terms, an Intent in Android is a way to pass certain information from one activity to another activity. There are 2 main categories of Intent:
- Activity Action Intents: Pass information to a single activity outside the application. e.g. “WEB_SEARCH_ACTION” opens Google search or another web page if specified.
- Broadcast Action Intents: Sent out information to multiple activity. e.g. “MEDIA_EJECT_ACTION” is sent when ejection action has been initiated on an SD memory card.
For an intent from an activity to work, it requires 2 other mechanism,
- Intent Receiver: sort of a mailbox for the activity. Every activity has a receiver and it has a list of intents that it accepts and process.
- Intent Filters: describes the type of intents it will receive and also outlines the data format the Intent accepted by the activity .
How do i use an Intent?
1. Import the appropriate package.
- android.content.Intent
2. Create an Intent object
Intent <intent_name> = new Intent(<Android_Intent>, <data>)
- <intent_name>: name of the my intent object
- <Android_Intent>: Intent name.
- <data>: data to be sent out by the intent
e.g. creating a DIAL_ACTION Intent
Intent DialIntent = new Intent(Intent.DIAL_ACTION, Uri.parse(“tel:5551212”));
Note: In this case, need to import android.net.Uri
3. Use setLaunchFlags() of the Intent object i just created to specify to android the type of activity to launch.
e.g. DialIntent.setLaunchFlags(Intent.NEW_TASK-LAUNCH);
4. Lastly, launch the activity using startActivity()
e.g. startActivity(DialIntent);
Reference:
1. Android A Programmer’s Guide, Jerome (J.F) DiMarzio
(ISBN 978-0-07-159988-7)