The intent is the main component of Android. It is a messaging object which passes between components like content providers, activities, services, etc.
This intent specifies an action that can be invoked by any app on the device which enables us to perform an action. It does not have exact knowledge about the landing component. It can open another app or its own app’s component and many other options exist.
Examples: Downloaded song, PDF, image, document, dial call, map location, etc.
Syntax of open Google map
1
2
3
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.set Data(Uri.parse("http://www.google.com"));
startActivity(intent);
This intent specifies the component in an app. It is one that we use to launch a specific app component, such as a particular activity or service in our application. Using this intent we can pass the data from one activity to another activity.
Examples: startActivity (know about which activity will start), start service to download the file.
Syntax of explicit intent
1 2
Intent explicit_intent = new Intent(MainActivity.this, Explicit_intent.class); startActivity(explicit_intent);
Some uses of intent in Android:
Launch an activity.
Start the service.
Map GEO location.
Broadcast a message.
Dial a phone call.
Step 1: In this step first we will create a new Android project in Android studio.
Step 2: Design the UI of the activity_main file.
Now let us design the UI of the activity_main file. In this file, we need to first design two buttons each for explicit intent and implicit intent.
activity_main.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/bt_explicit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="60dp" android:layout_gravity="center_horizontal" android:text="EXPLICIT INTENT" android:onClick="callSecondActivity" /> <Button android:id="@+id/bt_implicit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_gravity="center_horizontal" android:text="IMPLICIT INTENT" /> </LinearLayout>
Step 3:
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.example.explicitimplicitintent;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button bt_explicit;
Button bt_implicit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_explicit = findViewById(R.id.bt_explicit);
bt_implicit = findViewById(R.id.bt_implicit);
bt_implicit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri
Intent implicit_intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("geo:37.7749,-122.4194"));
startActivity(implicit_intent);
Toast.makeText(MainActivity.this, "Clicked Implicit Intent Button", Toast.LENGTH_SHORT)
.show();
}
});
}
Public void callSecondActiviy(View view) {
Intent explicit_intent = new Intent(MainActivity.this,
Explicit_intent.class);
startActivity(explicit_intent);
Toast.makeText(MainActivity.this, "Clicked Explicit Intent Button", Toast.LENGTH_SHORT)
.show();
}
}
Step 4: Design the UI of the second layout file.
Now design the UI of another activity where the user will navigate after they click on the bt_explicit button. Go to the layout folder, create a new activity and name it activiy_explicit_intent.
In this activity, we will simply use the TextView to tell the user they are now on the second activity. Now create the button which will be used to go back to the first activity.
activity_explicit_intent.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".Explicit_intent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="60dp" android:text="Explicit Intent " android:textSize="20sp"/> <Button android:id="@+id/bt_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Call First Activity" android:onClick="callFirstActivity" android:layout_gravity="center" android:layout_marginTop="10dp"/> </LinearLayout>
Step 5: Create a new Java class named Explicit_intent.
Explicit_intent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.example.explicitimplicitintent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class Explicit_intent extends AppCompatActivity {
Button bt_back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_explicit_intent);
bt_back = findViewById(R.id.bt_back);
}
public void callFirstActivity(View view) {
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
Toast.makeText(this, "We are moved to First Activity", Toast.LENGTH_SHORT)
.show();
}
}
Output: