0% found this document useful (0 votes)
10 views

Android Intent

Uploaded by

kailashtrade27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Android Intent

Uploaded by

kailashtrade27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Android Intent

• Android Intent is themessage that is passed


between components such as ac vi es, content
providers, broadcast receivers, services etc.
• Intent are the objects which is used for passing
the informa on among ac vi es in an
applica on and from one app to another also.
• It is generally used with startAc vity() method
to invoke ac vity, broadcast receivers etc.
• Although intents facilitate communica on between
components in several ways, there are three
fundamental use cases:
• Star ng an ac vity: An Ac vity represents a single screen
in an app. You can start a new instance of an Ac vity by
passing an Intent to startAc vity(). The Intent describes
the ac vity to start and carries any necessary data.
• Star ng a service: A Service is a component that
performs opera ons in the background without a user
interface. With Android 5.0 (API level 21) and later
• Delivering a broadcast: A broadcast is a message that any
app can receive. The system delivers various broadcasts
for system events, such as when the system boots up or
the device starts charging. You can deliver a broadcast to
other apps by passing an Intent to sendBroadcast()
• Types of Intents
• There are following two types of intents
supported by Android:
• 1) Explicit Intent
• 2) Implicit Intent
Explicit Intents
• Explicit intent going to be connected internal
world of applica on, suppose if you wants to
connect one ac vity to another ac vity, we
can do this quote by explicit intent, below
image is connec ng first ac vity to second
ac vity by clicking bu on.
• These intents designate the target
component by its name and they are typically
used for applica on-internal messages - such
as an ac vity star ng a subordinate service or
launching a sister ac vity.
• For example −
• // Explicit Intent by specifying its class name
• Intent i = new Intent(FirstAc vity.this, SecondAc vity.class);
• // Starts TargetAc vity
• startAc vity(i);
Implicit Intents
• These intents do not name a target and the field
for the component name is le blank. Implicit
intents are o en used to ac vate components in
other applica ons.
• For example −
• Intent read1=new Intent(); read1.
setAc on(android.content.Intent.ACTION_VIEW)
; read1.setData(ContactsContract.Contacts.
CONTENT_URI); startAc vity(read1);
• Above code will give result as shown below
Android Implicit Intent Example
• File: ac vity_main.xml
• <?xml version="1.0" encoding="u -8"?>
• <android.support.constraint.ConstraintLayout xmlns:
android="h p://schemas.android.com/apk/res/android"

• xmlns:app="h p://schemas.android.com/apk/res-
auto"
• xmlns:tools="h p://schemas.android.com/tools"
• android:layout_width="match_parent"
• android:layout_height="match_parent"
• tools:context="example.javatpoint.com.implici ntent.
MainAc vity">
• <EditText
• android:id="@+id/editText"
• android:layout_width="wrap_content"
• android:layout_height="wrap_content"
• android:layout_marginEnd="8dp"
• android:layout_marginStart="8dp"
• android:layout_marginTop="60dp"
• android:ems="10"
• app:layout_constraintEnd_toEndOf="parent"
• app:layout_constraintHorizontal_bias="0.575"
• app:layout_constraintStart_toStartOf="parent"
• app:layout_constraintTop_toTopOf="parent" />
• <Bu on
• android:id="@+id/bu on"
• android:layout_width="wrap_content"
• android:layout_height="wrap_content"
• android:layout_marginRight="8dp"
• android:layout_marginLe ="156dp"
• android:layout_marginTop="172dp"
• android:text="Visit"
• app:layout_constraintEnd_toEndOf="parent"
• app:layout_constraintHorizontal_bias="0.0"
• app:layout_constraintStart_toStartOf="parent"
• app:layout_constraintTop_toBo omOf="@+id/
editText" />
• </android.support.constraint.ConstraintLayout>
• File: MainAc vity.java
• package example.javatpoint.com.implici ntent;

• import android.content.Intent;
• import android.net.Uri;
• import android.support.v7.app.
AppCompatAc vity;
• import android.os.Bundle;
• import android.view.View;
• import android.widget.Bu on;
• import android.widget.EditText;
• public class MainAc vity extends AppCompatAc vity {

• Bu on bu on;
• EditText editText;

• @Override
• protected void onCreate(Bundle savedInstanceState)
{
• super.onCreate(savedInstanceState);
• setContentView(R.layout.ac vity_main);

• bu on = findViewById(R.id.bu on);
• editText = findViewById(R.id.editText);
• bu on.setOnClickListener(new View.
OnClickListener() {
• @Override
• public void onClick(View view) {
• String url=editText.getText().toString();
• Intent intent=new Intent(Intent.
ACTION_VIEW, Uri.parse(url));
• startAc vity(intent);
• }
• });
• }
• }

You might also like