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 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.
If you would like to Enrich your career with an Android certified professional, then visit Mindmajix - A Global online training platform:“Android training” Course.This course will help you to achieve excellence in this domain.
Intents are used for navigation purposes in android that mean 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 :
Checkout:-Android Services
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.
By using two methods we can do navigation from one activity to another activity. They are:
Frequently asked Android Interview Questions
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 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("https://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);
Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!
Name | Dates | |
---|---|---|
Android Training | Apr 01 to Apr 16 | |
Android Training | Apr 04 to Apr 19 | |
Android Training | Apr 08 to Apr 23 | |
Android Training | Apr 11 to Apr 26 |
Ravindra Savaram is a Content Lead at Mindmajix.com. His passion lies in writing articles on the most popular IT platforms including Machine learning, DevOps, Data Science, Artificial Intelligence, RPA, Deep Learning, and so on. You can stay up to date on all these technologies by following him on LinkedIn and Twitter.
1 /15
Copyright © 2013 - 2023 MindMajix Technologies