{"id":357,"date":"2015-12-19T11:04:13","date_gmt":"2015-12-19T11:04:13","guid":{"rendered":"http:\/\/abhiandroid.com\/programming\/?page_id=357"},"modified":"2018-06-06T04:38:03","modified_gmt":"2018-06-06T04:38:03","slug":"intent-in-android","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/programming\/intent-in-android","title":{"rendered":"Intent Tutorial in Android With Example And Types"},"content":{"rendered":"<p>Android uses Intent for communicating between the components of an Application and also from one application to another application.<\/p>\n<p>Intent are the objects which is used in android for passing the information among Activities in an Application and from one app to another also. Intent are used for communicating between the Application components and it also provides the connectivity between two apps.<\/p>\n<p>For example: Intent facilitate you to redirect your activity to another activity on occurrence of any event. By calling, startActivity() you can perform this task.<\/p>\n<pre>Intent intent = new Intent(getApplicationContext(), SecondActivity.class);\r\nstartActivity(intent);<\/pre>\n<p>In the above example, foreground activity is getting redirected to another activity i.e. SecondActivity.java. getApplicationContext() returns the context for your foreground activity.<\/p>\n<hr \/>\n<h4><strong>Types of Intents:<\/strong><\/h4>\n<p>Intent are of two types: Explicit Intent and Implicit Intent<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-382\" src=\"\/programming\/wp-content\/uploads\/2015\/12\/Types-of-Intents.jpg\" alt=\"Types of Intents\" width=\"370\" height=\"220\" srcset=\"https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2015\/12\/Types-of-Intents.jpg 370w, https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2015\/12\/Types-of-Intents-300x178.jpg 300w\" sizes=\"auto, (max-width: 370px) 100vw, 370px\" \/><\/center><span style=\"text-decoration: underline; color: #008000;\"><strong>Explicit Intent:<\/strong><\/span><\/p>\n<ul>\n<li>Explicit Intents are used to connect the application internally.<\/li>\n<li>In Explicit we use the name of component which will be affected by Intent. For Example: If we know class name then we can navigate the app from One Activity to another activity using Intent. In the similar way we can start a service to download a file in background process.<\/li>\n<\/ul>\n<p>Explicit Intent work internally within an application to perform navigation and data transfer. The below given code snippet will help you understand the concept of Explicit Intents<\/p>\n<pre>Intent intent = new Intent(getApplicationContext(), SecondActivity.class);\r\nstartActivity(intent);<\/pre>\n<p>Here SecondActivity is the JAVA class name where the activity will now be navigated. Example with code in the end of this post will make it more clear.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-374\" src=\"\/programming\/wp-content\/uploads\/2015\/12\/Explicit-Intent-Example.jpg\" alt=\"Explicit Intent Example\" width=\"351\" height=\"307\" srcset=\"https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2015\/12\/Explicit-Intent-Example.jpg 351w, https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2015\/12\/Explicit-Intent-Example-300x262.jpg 300w\" sizes=\"auto, (max-width: 351px) 100vw, 351px\" \/><\/center><span style=\"text-decoration: underline; color: #008000;\"><strong>Implicit Intent:<\/strong><\/span><\/p>\n<ul>\n<li>In Implicit Intents we do need to specify the name of the component. We just specify the Action which has to be performed and further this action is handled by the component of another application.<\/li>\n<li>The basic example of implicit Intent is to open any web page<\/li>\n<\/ul>\n<p>Let\u2019s take an example to understand Implicit Intents more clearly. We have to open a website using intent in your application. See the code snippet given below<\/p>\n<pre>Intent intentObj = new Intent(Intent.ACTION_VIEW);\r\nintentObj.setData(Uri.parse(\"https:\/\/www.abhiandroid.com\"));\r\nstartActivity(intentObj);<\/pre>\n<p>Unlike Explicit Intent you do not use any class name to pass through Intent(). In this example we has just specified an action. Now when we will run this code then Android will automatically start your web browser and it will open AbhiAndroid home page.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-375\" src=\"\/programming\/wp-content\/uploads\/2015\/12\/Implicit-Intent-Example.jpg\" alt=\"Implicit Intent Example\" width=\"365\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2015\/12\/Implicit-Intent-Example.jpg 365w, https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2015\/12\/Implicit-Intent-Example-300x247.jpg 300w\" sizes=\"auto, (max-width: 365px) 100vw, 365px\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>Intent Example In Android:<\/strong><\/h4>\n<p>Let\u2019s implement Intent for a very basic use. In the below example we will Navigate from one Activity to another and open a web homepage of AbhiAndroid using Intent. <strong>The example will show you both implicit and explicit Intent together. Below is the final output:<\/strong><\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-373\" src=\"\/programming\/wp-content\/uploads\/2015\/12\/Implicit-And-Explicit-Intent-Example-Output-in-Android.jpg\" alt=\"Implicit And Explicit Intent Example Output in Android\" width=\"375\" height=\"366\" srcset=\"https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2015\/12\/Implicit-And-Explicit-Intent-Example-Output-in-Android.jpg 375w, https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2015\/12\/Implicit-And-Explicit-Intent-Example-Output-in-Android-300x293.jpg 300w\" sizes=\"auto, (max-width: 375px) 100vw, 375px\" \/><\/center>Create a project in Android Studio and named it \u201cIntents\u201d. Make an activity, which would consists Java file; MainActivity.java and an xml file for User interface which would be activity_main.xml<\/p>\n<p><strong>Step 1: Let&#8217;s design the UI of activity_main.xml:<\/strong><\/p>\n<ul>\n<li>First design the text view displaying basic details of the App<\/li>\n<li>Second design the two button of Explicit Intent Example and Implicit Intent Example<\/li>\n<\/ul>\n<p><strong>Below is the complete code of activity_main.xml<\/strong><\/p>\n<pre>&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\" android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\" android:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingTop=\"@dimen\/activity_vertical_margin\"\r\n    android:paddingBottom=\"@dimen\/activity_vertical_margin\" tools:context=\".MainActivity\"&gt;\r\n\r\n    &lt;TextView\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:textAppearance=\"?android:attr\/textAppearanceMedium\"\r\n        android:text=\"If you click on Explicit example we will navigate to second activity within App and if you click on Implicit example AbhiAndroid Homepage will open in Browser\"\r\n        android:id=\"@+id\/textView2\"\r\n        android:clickable=\"false\"\r\n        android:layout_alignParentTop=\"true\"\r\n        android:layout_alignParentStart=\"true\"\r\n        android:layout_marginTop=\"42dp\"\r\n        android:background=\"#3e7d02\"\r\n        android:textColor=\"#ffffff\" \/&gt;\r\n\r\n    &lt;Button\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:text=\"Explicit Intent Example\"\r\n        android:id=\"@+id\/explicit_Intent\"\r\n        android:layout_alignParentTop=\"true\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:layout_marginTop=\"147dp\" \/&gt;\r\n\r\n    &lt;Button\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:text=\"Implicit Intent Example\"\r\n        android:id=\"@+id\/implicit_Intent\"\r\n        android:layout_centerVertical=\"true\"\r\n        android:layout_centerHorizontal=\"true\" \/&gt;\r\n    \r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><strong>Step 2: Design the UI of second activity activity_second.xml<\/strong><\/p>\n<p>Now lets design UI of another activity where user will navigate after he click on Explicit Example button. Go to layout folder, create a new activity and name it activity_second.xml.<\/p>\n<ul>\n<li>In this activity we will simply use TextView to tell user he is now on second activity.<\/li>\n<\/ul>\n<p><strong>Below is the complete code of activity_second.xml<\/strong><\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\" android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\" android:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingTop=\"@dimen\/activity_vertical_margin\"\r\n    android:paddingBottom=\"@dimen\/activity_vertical_margin\"\r\n    android:background=\"#CCEEAA\"\r\n    tools:context=\"com.example.android.intents.SecondActivity\"&gt;\r\n\r\n    &lt;TextView\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:textAppearance=\"?android:attr\/textAppearanceLarge\"\r\n        android:text=\"This is Second Activity\"\r\n        android:id=\"@+id\/textView\"\r\n        android:layout_centerVertical=\"true\"\r\n        android:layout_centerHorizontal=\"true\" \/&gt;\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><strong>Step 3: Implement onClick event for Implicit And Explicit Button inside MainActivity.java<\/strong><\/p>\n<p>Now we will use setOnClickListener() method to implement OnClick event on both the button. Implicit button will open AbhiAndroid.com homepage in browser and Explicit button will move to SecondActivity.java.<\/p>\n<p><strong>Below is the complete code of MainActivity.java<\/strong><\/p>\n<pre>package com.example.android.intents;\r\n\r\nimport android.content.Intent;\r\nimport android.net.Uri;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.View;\r\nimport android.widget.Button;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n\r\n    Button explicit_btn, implicit_btn;\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n\r\n\r\n        explicit_btn = (Button)findViewById(R.id.explicit_Intent);\r\n        implicit_btn = (Button) findViewById(R.id.implicit_Intent);\r\n\r\n        \/\/implement Onclick event for Explicit Intent\r\n\r\n        explicit_btn.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View v) {\r\n\r\n                Intent intent = new  Intent(getBaseContext(), SecondActivity.class);\r\n                startActivity(intent);\r\n\r\n\r\n            }\r\n        });\r\n\r\n        \/\/implement onClick event for Implicit Intent\r\n\r\n        implicit_btn.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View v) {\r\n\r\n                Intent intent = new Intent(Intent.ACTION_VIEW);\r\n                intent.setData(Uri.parse(\"https:\/\/www.abhiandroid.com\"));\r\n                startActivity(intent);\r\n            }\r\n        });\r\n\r\n\r\n    }\r\n}<\/pre>\n<p><strong>Step 4: Create A New JAVA class name SecondActivity<\/strong><\/p>\n<p>Now we need to create another SecondActivity.java which will simply open the layout of activity_second.xml . Also we will use Toast to display message that he is on second activity.<\/p>\n<p><strong>Below is the complete code of SecondActivity.java:<\/strong><\/p>\n<pre>package com.example.android.intents;\r\n\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.widget.Toast;\r\n\r\npublic class SecondActivity extends AppCompatActivity {\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_second);\r\n\r\n        Toast.makeText(getApplicationContext(), \"We are moved to second Activity\",Toast.LENGTH_LONG).show();\r\n    }\r\n}<\/pre>\n<p><strong>Step 5: Manifest file:<\/strong><\/p>\n<p>Make sure Manifest file has both the MainActivity and SecondActivity listed it. Also here MainActivity is our main activity which will be launched first. So make sure intent-filter is correctly added just below MainActivity.<\/p>\n<p><strong>Below is the code of Manifest file:<\/strong><\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;manifest xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    package=\"com.example.android.intents\" &gt;\r\n\r\n    &lt;application\r\n        android:allowBackup=\"true\"\r\n        android:icon=\"@mipmap\/ic_launcher\"\r\n        android:label=\"@string\/app_name\"\r\n        android:supportsRtl=\"true\"\r\n        android:theme=\"@style\/AppTheme\" &gt;\r\n        &lt;activity android:name=\".MainActivity\" &gt;\r\n            &lt;intent-filter&gt;\r\n                &lt;action android:name=\"android.intent.action.MAIN\" \/&gt;\r\n                &lt;category android:name=\"android.intent.category.LAUNCHER\" \/&gt;\r\n            &lt;\/intent-filter&gt;\r\n        &lt;\/activity&gt;\r\n        &lt;activity android:name=\".SecondActivity\" &gt;\r\n\r\n        &lt;\/activity&gt;\r\n    &lt;\/application&gt;\r\n\r\n&lt;\/manifest&gt;<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>Output:<\/strong><\/span><\/p>\n<p>Now run the above program in your Emulator. The App will look like this:<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-373\" src=\"\/programming\/wp-content\/uploads\/2015\/12\/Implicit-And-Explicit-Intent-Example-Output-in-Android.jpg\" alt=\"Implicit And Explicit Intent Example Output in Android\" width=\"375\" height=\"366\" srcset=\"https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2015\/12\/Implicit-And-Explicit-Intent-Example-Output-in-Android.jpg 375w, https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2015\/12\/Implicit-And-Explicit-Intent-Example-Output-in-Android-300x293.jpg 300w\" sizes=\"auto, (max-width: 375px) 100vw, 375px\" \/><\/center>First Click on Explicit Intent Example. The SecondActivity will be open within the App:<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-374\" src=\"\/programming\/wp-content\/uploads\/2015\/12\/Explicit-Intent-Example.jpg\" alt=\"Explicit Intent Example\" width=\"351\" height=\"307\" srcset=\"https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2015\/12\/Explicit-Intent-Example.jpg 351w, https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2015\/12\/Explicit-Intent-Example-300x262.jpg 300w\" sizes=\"auto, (max-width: 351px) 100vw, 351px\" \/><\/center>Now go back in Emulator and click on Implicit Intent Example. The AbhiAndroid.com homepage will open in Browser (make sure you have internet):<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-375\" src=\"\/programming\/wp-content\/uploads\/2015\/12\/Implicit-Intent-Example.jpg\" alt=\"Implicit Intent Example\" width=\"365\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2015\/12\/Implicit-Intent-Example.jpg 365w, https:\/\/abhiandroid.com\/programming\/wp-content\/uploads\/2015\/12\/Implicit-Intent-Example-300x247.jpg 300w\" sizes=\"auto, (max-width: 365px) 100vw, 365px\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>Intent Uses In Android:<\/strong><\/h4>\n<p>Android uses Intents for facilitating communication between its components like Activities, Services and Broadcast Receivers.<\/p>\n<p><span style=\"color: #008000;\"><strong>Intent for an Activity:<\/strong><\/span><\/p>\n<p>Every screen in Android application represents an activity. To start a new activity you need to pass an Intent object to startActivity() method. This Intent object helps to start a new activity and passing data to the second activity.<\/p>\n<p><span style=\"color: #008000;\"><strong>Intent for Services:<\/strong><\/span><\/p>\n<p>Services work in background of an Android application and it does not require any user Interface. Intents could be used to start a Service that performs one-time task(for example: Downloading some file) or for starting a Service you need to pass Intent to startService() method.<\/p>\n<p><span style=\"color: #008000;\"><strong>Intent for Broadcast Receivers:<\/strong><\/span><\/p>\n<p>There are various message that an app receives, these messages are called as Broadcast Receivers. (For example, a broadcast message could be initiated to intimate that the file downloading is completed and ready to use). Android system initiates some broadcast message on several events, such as System Reboot, Low Battery warning message etc.<\/p>\n<hr \/>\n<h4><strong>Importance of using Intents in Android Applications:<\/strong><\/h4>\n<p>Whenever you need to navigate to another activity of your app or you need to send some information to next activity then we can always prefer to Intents for doing so.<\/p>\n<p>Intents are really easy to handle and it facilitates communication of components and activities of your application. Moreover you can communicate to another application and send some data to another application using Intents.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Android uses Intent for communicating between the components of an Application and also from one application to another application. Intent are the objects which is used in android for passing the information among Activities in an Application and from one app to another also. Intent are used for communicating between the Application components and it &hellip; <a href=\"https:\/\/abhiandroid.com\/programming\/intent-in-android\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Intent Tutorial in Android With Example And Types<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"footnotes":""},"class_list":["post-357","page","type-page","status-publish","hentry"],"psp_head":"<title>Intent Tutorial in Android With Example And Types \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Complete tutorial on Intent and its types Explicit And Implicit with example in Android. Android uses Intent for communicating between the components of an Application and also from one application to another application.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/programming\/intent-in-android\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/pages\/357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/comments?post=357"}],"version-history":[{"count":2,"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/pages\/357\/revisions"}],"predecessor-version":[{"id":905,"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/pages\/357\/revisions\/905"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/programming\/wp-json\/wp\/v2\/media?parent=357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}