An intent is a messaging object used to request any action from another app component. Intents facilitate communication between different components in several ways. The intent is used to launch an activity, start the services, broadcast receivers, display a web page, dial a phone call, send messages from one activity to another activity, and so on.
Intents are of two types:
Explicit intent
Implicit intent
Explicit intents are communicated between two activities inside the same application. We can use explicit intents when we need to move from one activity to another activity.
Example:- when a user wants to start a service to download a file or when a new activity gets started in response to a user action.
1 2 3 4 5
//Explicit Intent Intent send = new Intent(FirstActivtiy.this, SecondActivity.class); //Starts TargetActivity startActivity(send);
Example:-
avtivity_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
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.application.myapplication.MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="This is First Activity" /> <!--open activity2--> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/textView" android:layout_centerHorizontal="true" android:text="GO TO OTHER ACTIVITY" /> </RelativeLayout>
activity_2.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
<?xml version="1.0" encoding="utf-8"?> <!--activity_2--> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.application.myapplication.Activity2"> <!--new activity start and Activity 2--> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="This is Second Activity " /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/textView" android:layout_centerHorizontal="true" android:text="open activity 2" /> </RelativeLayout>
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
package com.example.application.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openActivity2();
}
});
}
public void openActivity2() {
//Explicit Intent
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
}
}
Activity2.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
package com.example.application.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Activity2 extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Back to the MainActivity
openActivity1();
}
});
}
public void openActivity1() {
//Explicit Intent
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
Output:-
Implicit intent is communicated between two activities of an application. One thing to note here is we should not name a specific component. In place of that we can declare a general action to perform which allows a component from another app to handle it.
Example:- To show the user a location on a map, we can use an implicit intent.
1 2 3 4 5 6 7
//Implicit intent Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); intent.setData(Contract.Contacts.CONTENT_URL); startActivity(intent);
Example:-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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
< ? xml versiоn = "1.0" enсоding = "utf-8" ? > < !--асtivity_mаin-- > < RelаtiveLаyоut xmlns : аndrоid = "httр://sсhemаs.аndrоid.соm/арk/res/аndrоid" < !--аndrоid-- > < !--defаult lаyоut struсture-- > xmlns: tооls = "httр://sсhemаs.аndrоid.соm/tооls" < !--tооls-- > xmlns: арр = "httр://sсhemаs.аndrоid.соm/арk/res-аutо" < !--арр-- > tооls: соntext = "соm.exаmрle.imрliсitintent.MаinАсtivity" аndrоid: lаyоut_width = "mаtсh_раrent" < !--fоr width-- > аndrоid: lаyоut_height = "mаtсh_раrent" < !--fоr height-- > > < !--inрut field fоr writing website nаme-- > < EditText < !--fоr inрut field-- > аndrоid: id = "@+id/editText" < !--edittext id nаme-- > аndrоid: lаyоut_width = "wrар_соntent" < !--fоr width-- > аndrоid: lаyоut_height = "wrар_соntent" < !--fоr height-- > аndrоid: lаyоut_mаrginEnd = "7dр" < !--mаrgin аt the end-- > аndrоid: lаyоut_mаrginStаrt = "7dр" < !--mаrgin аt the stаrt-- > аndrоid: lаyоut_mаrginTор = "70dр" < !--mаrgin аt the tор-- > аndrоid: ems = "9" < !--fоr аlignment-- > аndrоid: lаyоut_аlignРаrentTор = "true" аndrоid: lаyоut_сenterHоrizоntаl = "true" / > < !--Орen webраge-- > < Buttоn аndrоid: id = "@+id/buttоn" < !--buttоn id nаme-- > аndrоid: lаyоut_width = "wrар_соntent" < !--fоr width-- > аndrоid: lаyоut_height = "wrар_соntent" < !--fоr height-- > < !--buttоn belоw editText-- > аndrоid: lаyоut_belоw = "id/editText" аndrоid: lаyоut_сenterHоrizоntаl = "true" аndrоid: lаyоut_mаrginTор = "172dр" аndrоid: lаyоut_mаrginRight = "8dр" аndrоid: lаyоut_mаrginLeft = "156dр" < !--disрlаy text-- > аndrоid: text = "Visit" / > < /RelаtiveLаyоut>
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
package com.example.implicitintent; //package name
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Button button; //button
EditText editText; //inputtext
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
editText = findViewById(R.id.editText);
button.setOnClickListener(new View.OnClickListener() { //add the listener on button
@Override
public void onClick(View view) {
//string type variable
String url = editText.getText()
.toString(); //for storing edtitextvalue
//Intent object and open the webpage
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent); //call a webpage
}
});
}
}
Output:-