How to Create Intent in Android?

How to Create Intent in Android?

An Intent (android.content.Intent) is an asynchronous message mechanism used by android operating system to match task requests with the appropriate activity or Service (launching it, if necessary) to dispatch broadcast Intents events to the system at large.

Launching a New Activity by Class Name

Activity can be started in many ways.
Use the Application Context object to call the start Activity() method, which takes a single parameter, an Intent.
This Intent requests the launch of the target Activity named MyDrawActivity by its class.
This class is implemented elsewhere within the package.
startActivity(new Intent(getApplicationContext(), MyDrawActivity.class));
code for transition from one Activity to the next.
Intent structure to pass data between Activities.

How to Creating Intents with Action and Data

Intents need not specify the component or class they want to launch explicitly.
The Android operating system attempts to resolve the Intent requirements and launch the appropriate activity based on the filter criteria.
The guts of the Intent object are composed of two main parts: The action to be performed  the data to be acted upon.

Launching an Activity Belonging to Another Application

An Intent is basically saying “do this” (the action) to “that” (the Uri describing what resource to do the action to).

Creating Intents with Action and Data

With the appropriate permissions, applications might also launch external Activities within other applications apart from starting only Activities defined within its own package.
For example, a Customer Relationship Management (CRM) application might launch the Contacts application to browse the Contact database, choose a specific contact, and return that Contact’s unique identifier to the CRM application for use.
Example of how to create a simple Intent with a predefined Action (ACTION_DIAL) to launch the Phone Dialer with a specific phone number to dial in the form of a simple Uri object:
Uri number = Uri.parse(tel:5555551212);
Intent dial = new Intent(Intent.ACTION_DIAL, number);
startActivity(dial);

Passing Additional Information Using Intents

You can also include additional data in an Intent.
The Extras property of an Intent is stored in a Bundle object.
The Intent class also has a number of helper methods for getting and setting name/value pairs for many common datatypes.
For example, the following Intent includes two extra pieces of information
a string value and a boolean:
Intent intent = new Intent(this, MyActivity.class);
intent.putExtra(“SomeStringData”,”Foo”);
intent.putExtra(“SomeBooleanData”,false);

0 comments:

Post a Comment

 
Design by Wordpress Theme | Bloggerized by Free Blogger Templates | Grocery Coupons