Intent in Android

 Intent is an Android Framework class that help us to switch between two activity or opening new application or app in android. In this blog, you will know 

  1.  What is Intent?
  2. Intent in Android with Example.
  3. Why are Android intents important?
  4. How many types of intent are there?
  5. What is Implicit Intent?
  6. What is Explicit Intent?
  7. Implementation of Explicit Intent in Android.
  8. Implementation of Implicit Intent in Android.
  9. What is an Intent filter in Android

What is Intent?

Intent is an Android Framework Class. When we open an app and after clicking something it will change the screen, it's mean changing the activity in the app. well instead of direct calling an Activity to another activity, we use a massaging object called intent. 

Intent does not only help us to open another activity in Android or Android studio, but It also does more than that. When we share something or click a button to open another app from an app, intent helps us to do so. Through Intent, we can share something, open a map, send emails, and many more.

Intent in Android with Example:

  1.  When we share something from an app in android a pop-up window will open and we see a bunch of apps through which we can share, this thing is possible for intent.
  2. Sometimes we see whenever we click the open map button in an app it will open the maps app, this is also possible for android.
  3. Today every app is multiscreen that means whenever we use an app and click, it changes its screen, this a task of intent.

Why are Android Intents Important?

You may think that we can call the activity class for going another activity or changing screen, then why should we use intent. Intent is doing more than that. Intent is a complete package or a framework class. For better understanding, we may assume intent is a black box that helps us do many things. 

The importance of intent in Android are
  1. Start Activity.
  2. end broadcast receiver.
  3. start services
  4. send message between two activities

Types of Intents in Android


In Android, there are two types of Intent 
  1. Explicit Intent
  2. Implicit Intent

What is Explicit Intent?

When we use intent for opening a component in our app, we specify the component or class that Intent should open by Intent is called Explicit Intent.

Example of Explicit Intent:

When we change the screen on our app or open another class or activity through one Activity in android, we use explicit Intent.

What is Implicit Intent?

When we use Intent to handle the operation from other apps is called Implicit Intent. In other words, When we do not know the app which can perform the operation, we use implicit intent. We use Implicit Intent to do some operation which can be handle by some other apps.

Example of Implicit Intent:
If you want to show the user a web result on a browser, you can use an implicit intent to request that another capable app show the specific data.
 

Implementation of Explicit Intent in Android

Explicit Intent is one you should use it when you want to launch a specific activity or component in your app. To use explicit intent you just pass the component name for an intent object, other properties ( like pass string, data, etc) are optional.

For example , if you build an app called "WebResult" to show the web result of a specific URL you can start it with the following code:

// the code in java 
//Executed in an Activity, so 'this' is the Context
// The WebUrl is a string URL, such as "http://www.example.com/q?github"
Intent WebIntent = new Intent(this, WebResult.class);
webIntent.setData(Uri.parse(WebUrl));
startService(WebIntent);



Implementation of Implicit Intent in Android

Implicit Intent invokes action and this action can handle by multiple apps on the device. We don't need to care about which app will handle it. Through the implicit intent, you will tell android to perform this action by other apps on the device.

For example, if you have content that you want the user to share with other people, create an intent with the ACTION_SEND action and add extras that specify the content to share. When you call startActivity() with that intent, the user can pick an app through which to share the content.

// the code is in java
// this code is copied from official android documents.
// Create the text message with a string.
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);
sendIntent.setType("text/plain");

// Try to invoke the intent.
try {
    startActivity(sendIntent);
} catch (ActivityNotFoundException e) {
    // Define what your app should do if no activity can handle the intent.
}



What is an Intent filter in Android?

Intent filter is used to specify which implicit intent we need in our app. For explicit intent we don't need intent filters because we specify the destination in our explicit intent. Each intent filter is described as implicit intent. we can use many intent filters in one app and this intent filter must declare in manifest file in our app. 

The Example of Intent Filter:

  • <action> : Declare the intent action accepted
  • <data>  : Declare the type of data we will provide.
  • <category>: Declare the intent category accepted by our app
For example, here's an activity declaration with an intent filter to receive an ACTION_SEND intent when the data type is the text:

<activity android:name="ShareActivity">
   
<intent-filter>
       
<action android:name="android.intent.action.SEND"/>
       
<category android:name="android.intent.category.DEFAULT"/>
       
<data android:mimeType="text/plain"/>
   
</intent-filter>
</activity>




Last Words

The above blog is for beginner guide to android development. To know more about intent, intent-filter, kindly check out the android official documentaion.

Post a Comment

0 Comments