Mindmajix

Intents in Android

Intents

An Intent is Android‘s method for relaying certain information from one Activity to another. An Intent, in simpler terms, expresses to Android your intent to do something. You can think of an Intent as a message passed between Activities. For example, assume that you have an Activity that needs to open a web browser and display a page on your Android device. Your Activity would send an ―intent to open x page in the web browser,‖ known as a WEB_SEARCH_ACTION Intent, to the Android Intent Resolver. The Intent Resolver parses through a list of Activities and chooses the one that would best match your Intent; in this case, the Web Browser Activity. The Intent Resolver then passes your page to the web browser and starts the Web Browser Activity. Intents are broken up into two main categories:

  • Activity Action Intents:- Intents used to call Activities outside of your application. Only one Activity can handle the Intent. For example, for a web browser, you need to open the Web Browser Activity to display a page.
  • Broadcast Intents:- Intents that are sent out for multiple Activities to handle. An example of a Broadcast Intent would be a message sent out by Android about the current battery level. Any Activity can process this Intent and react accordingly—for example, cancel an Activity if the battery level is below a certain point. The current Activity Action Intents that are available to you. As you‘ll notice, in most cases, the name of the Intent does a good job of describing what that Intent does.

Intents are used for navigation purpose in android that means pass the control from one activity(class) to another activity(class). In three application components we are using this intent mechanism (Activity, Service, Receiver) but not for Content Provider. Intents are of two types.

They are :

  1. Implicit Intents
  2. Explicit Intents.

intents

Explicit Intents

Android Explicit intent specifies the component to be invoked from activity. In other words, we can call another activity in android by explicit intent.

We can also pass the information from one activity to another using explicit intent.

Passing the control from one Activity to another Activity:

By using two methods we can do navigation from one activity to another activity. They are:

  1. startActivity(Intent intent)
  2. startActivityForResult(Intent intent , int requestCode).

startActivity() :

This method is from context class and it supports only for single direction communication.

Intent intent = new Intent(context c,class);

startActivtity(intent);

Ex: If I want to pass the control from A(activity) to B(activity) at that time, we write this logic in A activity.

Coding:- Intent intent = new Intent(A.this,B.Class);

                 startActivity(intent);

startActivityForResult():

This method is from Activity class and it supports only for bi-direction communication. Here three steps will be involved as follows.

Step – I:  In class A, we write this code .

         Intent intent = new Intent(A.this,B.class);
         startActivityForResult(intent,101);

Step – II: In class B,we need to call the setResult() from Activity. We write this logic.

setResult(int ResultCode, Intent intent);

Logic :

Intent data = getIntent();
String result = F + S;
data.putExtra(“Result”,result);
setResult(1001,data)

Step – III: Again in A class we have to call callback method onActivityResult (int requestcode, int resultCode, intent data). Here, automatically the request code and result code will be bonded to this variables.

Logic :

protected void onActivityResult(int requestCode,int resultCode, Intent data) { if (requestCode == 100 && resultCode == 1001) {
String total = data.getStringExtra("result"); Result.setText(total);
}

Implicit Intents

Implicit Intent doesn’t specify the component. In such case, intent provides information of available components provided by the system that is to be invoked.

If we want to interact with default software components and hardware components, at that time we use this type of intents.

For Opening Browser:-

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.saitechnos.com"));
startActivity(intent);

For opening calling screen:-

Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:9493577797")); startActivity(intent);

For opening Dialing screen:-

Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:9493577797")); startActivity(intent);

For opening contacts:-

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people/1"));
startActivity(intent);

For opening Camera:-

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); startActivityForResult(intent, 0);

 

 


 

0 Responses on Intents in Android"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.