Free Shipping

Secure Payment

easy returns

24/7 support

  • Home
  • Blog
  • Introduction to Intent in Android

Introduction to Intent in Android

 July 13  | 0 Comments

Through this blog, we will be discussing Intent in Android devices. We will discuss what Intent is all about?

What is Intent in Android?

An Intent is a simple message object that is used to communicate between android components such as activities, content providers, broadcast receivers and services. Intents are also used to transfer data between activities.

Get Skilled in Android Development

Intents are used generally for starting a new activity using startActivity().

Use of Intent

  1. For Launching an Activity
  2. To start a New Service
  3. For Broadcasting Messages
  4. To Display a list of contacts in ListView

    Types of Intent in Android

    Intent is of two types:

    1. Implicit Intent
    2. Explicit Intent

    Implicit Intent

    The implicit intent is the intent where instead of defining the exact components, you define the action that you want to perform for different activities.

    An Implicit intent specifies an action that can invoke any app on the device to be able to perform an action. Using an Implicit Intent is useful when your app cannot perform the action but other apps probably can and you’d like the user to pick which app to use.

    Syntax:

    Intent i=new Intent();
    i.setAction(Intent.ACTION_SEND);

    There are some other standard actions that intents can use for launching activities.

    Explicit Intent

    An explicit intent is an Intent where you explicitly define the component that needs to be called by the Android System. An explicit intent is one that you can use to launch a specific app component, such as a particular activity or service in your app.

    Syntax:

    Intent I = new Intent(getApplicationContext(),NextActivity.class);
    I.putExtra(“value1” , “This value for Next Activity”);
    I.putExtra(“value2” , “This value for Next Activity”);

    The Different Methods Used in Intent

    • Action_Main
    • Action_Pick
    • Action_Chooser
    • Action_Dial
    • Action_Call
    • Action_Send
    • Action_SendTo
    1. ACTION_MAIN

    Use: Adds an action to an intent Filter.

    <action android:name = “string”>

    2. ACTION_PICK

    Syntax:

    It is using for picking the image from CAMERA or GALLERY.

    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                                    photoPickerIntent.setType("image/*");
                                    startActivityForResult(photoPickerIntent, SELECT_PHOTO);

    3. ACTION_CHOOSER

    Use: It is used for choosing the image from the gallery.

    Intent select = new Intent(Intent.ACTION_CAMERA);  send.setData(uri); startActivity(Intent.createChooser(select,”Select an Image from Camera”));

    4. ACTION_DIAL

    Use – Display the phone dialer with the given number filled in.

    String myPhoneNumber = “tel : 123456 ;
    Intent next = new Intent (Intent.ACTION_DIAL, Uri.parse(myPhoneNumberUri));
    startActivity(next);

    5. ACTION_CALL

    Use: Placing and immediate phone call

    String data = “tel : 651234567”;
    Intent myActivity = new Intent(Intent.ACTION_CALL, Uri.parse(myNumber);
    startActivity(myActivity);

    Permission Needed:

    <uses-permission android:name = “android.permission.CALL_PHONE” />

    6. ACTION_SEND

    Use: Sending Text content from one activity to other.

    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, “This is my text to Send”);
    sendIntent.setType(“text/plain”);
    startActivity(sendIntent);

    7. ACTION_SENDTO

    Use : Preparing an SMS. The text is supplied as an Extra element. The intent excepts such as values to be called “sms_body”

    Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(“smsto : 15555215556 “));
    intent.putExtra(“sms_body”,”are we playing cricket today?”);
    startActivity(intent);

    Examples Of Intent

    There is a simple application with two screens:

    1. First Screen contains three controls – TextView , EditText, and Button
    2. Second Screen contains a TextView
    3. Now Go to File -> New -> Android Project with Project Name IntentDemo
    4. Set the target device and select target android device version.
    5. Click on Finish.

    Code

    MainActivity.java

    public class MainActivity extends AppCompatActivity {
    EditText nameEdit;
    Button btnSubmit;
    String name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    nameEdit = (EditText) findViewById(R.id.editText);
    btnSubmit = (Button) findViewById(R.id.button);
    btnSubmit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Intent intObj = new Intent(MainActivity.this,NextActitvity.class);
    intObj.putExtra("USERNAME", name);
    startActivity(intObj);
    }
    });
    }
    }

    NextActiivity.java

    public class NextActitvity extends Activity {
    TextView txtName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_next);
    txtName = (TextView) findViewById(R.id.text2);
    Intent intename = getIntent();
    //Get the USERNAME passed from IntentExampleActivity
    String uname = (String) intename.getSerializableExtra("USERNAME");
    //Set text for greetMsg TextView
    txtName.setText("Welcome " + uname);
    }
    }

    activity_main.xml

    <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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Enter Your Name"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="88dp" />
    <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:layout_alignBottom="@+id/textView"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_toRightOf="@+id/textView"
    android:layout_toEndOf="@+id/textView" />
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Submit"
    android:id="@+id/button"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="61dp" />
    </RelativeLayout>
    activity_next.xml
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Name"
    android:id="@+id/textView2"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="203dp" />
    </RelativeLayout>

     

>