Intents
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.
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
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>
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;
explicit_btn = (Button)findViewById(R.id.explicit_Intent);
implicit_btn = (Button) findViewById(R.id.implicit_Intent);
}
});
implicit_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
}
package com.example.intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);