0% found this document useful (0 votes)
6 views3 pages

Intents

The document explains intents in Android as message passing mechanisms for communicating actions, such as viewing messages or sending emails. It details the structure of intents, including action and data, and introduces the concept of Pending Intents for future event handling. Additionally, it provides example XML layout and Java code for implementing explicit and implicit intents in an Android application.

Uploaded by

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

Intents

The document explains intents in Android as message passing mechanisms for communicating actions, such as viewing messages or sending emails. It details the structure of intents, including action and data, and introduces the concept of Pending Intents for future event handling. Additionally, it provides example XML layout and Java code for implementing explicit and implicit intents in an Android application.

Uploaded by

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

Intents

Intents are message passing mechanisms to communicate actions. It provides descriptions of the
action that you want to do. For example, view a message, send an email, play a video etc. It is very
closely related to an activity. For example, if you try to open a facebook post consisting of a picture,
it would trigger in firing the intent associated with the click activity. This will lead to a new window
displaying the photograph along with its associated information.

An intent is the abstract description of an operation to be performed. We can use startAcitivity() to


begin an activity, BroadCast Intent to send it to any interested startService() Intent or
BroadCastReceiver components or bindService(Intent, ServiceConnection, Int) to communicate with
a background service.

Pending Intent

The pending Intent class provides a mechanism for creating intents that can be called by another
application at a later time. It is commonly used to package an intent that will be fired in response to
a future event. It offers static methods to construct pending intents used to start an activity, start a
service or broadcast an intent. It can be created in 3 types;

 getActivity(Context,Int,Intent,Int)
 getBroadCast(Context,Int,Intent,Int)
 getService(Context,Int,Intent,Int)

Intent Structure

In android, intents are the message passing mechanisms to communicate an action.

action:- The general action to be performed such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN.

data:- The data to operate on, such as a person record in the contacts database, expressed as Uri.

Program
<?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=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"

android:id="@+id/textView2"
android:clickable="false"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="42dp"
android:background="#3e7d02"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/explicit_intent_example"
android:id="@+id/explicit_Intent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="147dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/implicit_intent_example"
android:id="@+id/implicit_Intent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

</RelativeLayout>

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CCEEAA">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/this_is_second_activity"
android:id="@+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

package com.example.intent;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

Button explicit_btn, implicit_btn;


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

explicit_btn = (Button)findViewById(R.id.explicit_Intent);
implicit_btn = (Button) findViewById(R.id.implicit_Intent);

//implement Onclick event for Explicit Intent


explicit_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent intent = new Intent(getBaseContext(),


SecondActivity.class);
startActivity(intent);

}
});

//implement onClick event for Implicit Intent

implicit_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent intent = new Intent(Intent.ACTION_VIEW);


intent.setData(Uri.parse("https://www.gmail.com"));
startActivity(intent);
}
});

}
}

package com.example.intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class SecondActivity extends AppCompatActivity {

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

Toast.makeText(getApplicationContext(), "We are moved to second


Activity",Toast.LENGTH_LONG).show();
}
}

You might also like