Through this blog we will be discussing Intent in Android devices. We will discuss what Intent is all about?
What is Intent?
So what is Intent? 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.
Intents are used generally for starting a new activity using startActivity().
Use of Intent
- For Launching an Activity
- To start a New Service
- For Broadcasting Messages
- To Display a list of contacts in ListView
Types of Intent
Intent is of two types:
- Implicit Intent
- Explicit Intent
Implicit Intent
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:
1 2 3 |
Intent i=new Intent(); i.setAction(Intent.ACTION_SEND); |
There are some other standard actions that intents can use for launching activities.
Explicit Intent
Explicit intent is the 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:
1 2 3 4 5 |
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 use in Inten
- ACTION_MAIN
Use: Adds an action to an intent Filter.
1 |
<action android:name = “string”> |
2.ACTION_PICK
Syntax:
It is using for picking the image from CAMERA or GALLERY.
1 2 3 |
<span style="color: #000000;"><span style="font-family: Calibri, serif;"><span style="font-size: medium;">Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);</span></span></span> <span style="color: #000000;"> <span style="font-family: Calibri, serif;"><span style="font-size: medium;">photoPickerIntent.setType("image/*");</span></span></span> <span style="color: #000000;"> <span style="font-family: Calibri, serif;"><span style="font-size: medium;">startActivityForResult(photoPickerIntent, SELECT_PHOTO);</span></span></span> |
3.ACTION_CHOOSER
Use: It is using for choosing the image from gallery.
1 |
[crayon-57bf6abf53b25372397558 class="western"]<span style="color: #000000;"><span style="font-family: Calibri, serif;"><span style="font-size: medium;">Intent select = new Intent(Intent.ACTION_CAMERA); </span></span></span> |
1 |
<span style="color: #000000;"><span style="font-family: Calibri, serif;"><span style="font-size: medium;">send.setData(uri);</span></span></span> |
1 |
<span style="color: #000000;"><span style="font-family: Calibri, serif;"><span style="font-size: medium;">startActivity(Intent.createChooser(select,”Select an Image from Camera”));</span></span></span> |
[/crayon]
4. ACTION_DIAL
Use – Display the phone dialer with the given number filled in.
1 2 3 4 5 |
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
1 2 3 4 5 |
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.
1 2 3 4 5 6 7 8 9 |
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”
1 2 3 4 5 |
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:
- First Screen contains three controls – TextView , EditText and Button
- Second Screen contains a TextView
- Now Go to File -> New -> Android Project with Project Name IntentDemo
- Set the target device and select target android device version.
- Click on Finish.
Code
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
<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> |
Give the name on Edit Text and click on Submit. You will get the output as shown below:
Leave a Reply