Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Intent

Android Intent is the message that is


passed between components such as
activities, content providers, broadcast
receivers, services etc.
It is generally used with startActivity()
method to invoke activity, broadcast
receivers etc.
The dictionary meaning of intent
is intention or purpose. So, it can be
described as the intention to do action.

Android intents are mainly used to:


 Start the service
 Launch an activity
 Display a web page
 Display a list of contacts
 Broadcast a message
 Dial a phone call etc.

next →← prev

Android Intent Tutorial

 Android Intent is the message that is


passed between components such as
activities, content providers,
broadcast receivers, services etc.
 It is generally used with
startActivity() method to invoke
activity, broadcast receivers etc.
 The dictionary meaning of intent
is intention or purpose. So, it can be
described as the intention to do
action.
 The LabeledIntent is the subclass of
android.content.Intent class.
 Android intents are mainly used to:
 Start the service
 Launch an activity
 Display a web page
 Display a list of contacts
 Broadcast a message
 Dial a phone call etc.

Types of Android Intents


There are two types of intents in
android: implicit and explicit.
1) Implicit Intent
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.
For example, you may write the
following code to view the webpage.
Intent intent=new Intent(Intent.ACT
ION_VIEW);
intent.setData(Uri.parse("http://ww
w.google.com"));
startActivity(intent);
2) Explicit Intent

Explicit Intent specifies the


component. In such case, intent
provides the external class to be
invoked.
Intent i = new Intent(getApplication
Context(), ActivityTwo.class);
startActivity(i);

Android Implicit Intent Example


activity_main.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/andr
oid"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. tools:context="example.myapplication.com.implicitintent.MainActivity">
8.
9. <EditText
10. android:id="@+id/editText"
11. android:layout_width="wrap_content"
12. android:layout_height="wrap_content"
13. android:layout_marginEnd="8dp"
14. android:layout_marginStart="8dp"
15. android:layout_marginTop="60dp"
16. android:ems="10"
17. app:layout_constraintEnd_toEndOf="parent"
18. app:layout_constraintHorizontal_bias="0.575"
19. app:layout_constraintStart_toStartOf="parent"
20. app:layout_constraintTop_toTopOf="parent" />
21.
22. <Button
23. android:id="@+id/b"
24. android:layout_width="wrap_content"
25. android:layout_height="wrap_content"
26. android:layout_marginRight="8dp"
27. android:layout_marginLeft="156dp"
28. android:layout_marginTop="172dp"
29. android:text="Visit"
30. app:layout_constraintEnd_toEndOf="parent"
31. app:layout_constraintHorizontal_bias="0.0"
32. app:layout_constraintStart_toStartOf="parent"
33. app:layout_constraintTop_toBottomOf="@+id/editText" />
34. </android.support.constraint.ConstraintLayout>

MainActivity.java

package example.myapplication.com.implicitintent;

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 AppCompatActivit


y{

Button b;
EditText e;

@Override
protected void onCreate(Bundle savedInstanceSt
ate) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
b = findViewById(R.id.b);
e = findViewById(R.id.editText);

b.setOnClickListener(new View.OnClickListene
r() {
@Override
public void onClick(View view) {
String call=e.getText().toString();
Intent intent=new Intent(Intent.ACTION
_VIEW, Uri.parse(url));
startActivity(intent);
}
});
}
}

Android Explicit Intent Example

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.
Android calling one activity from another activity example
activity_main.xml
File: activity_main.xml

1. <?xml version="1.0" encoding="utf-8"?>


2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/andr
oid"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. tools:context="example.myapplication.com.explicitintent.FirstActivity">
8.
9. <TextView
10. android:layout_width="wrap_content"
11. android:layout_height="wrap_content"
12. android:layout_marginEnd="8dp"
13. android:layout_marginStart="8dp"
14. android:layout_marginTop="8dp"
15. android:text="First Activity"
16. app:layout_constraintBottom_toBottomOf="parent"
17. app:layout_constraintEnd_toEndOf="parent"
18. app:layout_constraintHorizontal_bias="0.454"
19. app:layout_constraintLeft_toLeftOf="parent"
20. app:layout_constraintRight_toRightOf="parent"
21. app:layout_constraintStart_toStartOf="parent"
22. app:layout_constraintTop_toTopOf="parent"
23. app:layout_constraintVertical_bias="0.06" />
24.
25. <Button
26. android:id="@+id/button"
27. android:layout_width="wrap_content"
28. android:layout_height="wrap_content"
29. android:layout_marginEnd="8dp"
30. android:layout_marginStart="8dp"
31. android:layout_marginTop="392dp"
32. android:onClick="callSecondActivity"
33. android:text="Call second activity"
34. app:layout_constraintEnd_toEndOf="parent"
35. app:layout_constraintStart_toStartOf="parent"
36. app:layout_constraintTop_toTopOf="parent" />
37.
38. </android.support.constraint.ConstraintLayout>

ActivityOne class
File: MainActivityOne.java

package example.myapplication.com.explicitintent;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class FirstActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState
){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
}
public void callSecondActivity(View view){
Intent i = new Intent(getApplicationContext(),
SecondActivity.class);
i.putExtra("Value1", "Intent");
i.putExtra("Value2", "Example");
// Set the request code to any code you like, you can identify th
e
// callback via this code
startActivity(i);
}

}
activitytwo_main.xml
File: activitytwo_main.xml

1. <?xml version="1.0" encoding="utf-8"?>


2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android
.com/apk/res/android"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. tools:context="example.myapplication.explicitintent.SecondActivity">
8. </android.support.constraint.ConstraintLayout>

ActivityTwo class
File: MainActivityTwo.java

package example.myapplication.com.explicitintent;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class SecondActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Bundle extras = getIntent().getExtras();
String value1 = extras.getString("Value1");
String value2 = extras.getString("Value2");

}
}

You might also like