Android Intent and its types
Posted By : Sapna Sharma | 30-Jun-2015
Intent
Intent is the message that is passed between components such as activities, content providers, broadcast receivers, services etc.
The dictionary meaning of intent is intention or purpose. So, it can be described as the intention to do action.
Intent is used to perform various action like:
1. Start the service
2. Launch an activity
3. Display a web page
4. Display a list of contacts
5. Broadcast a message
6. Dial a phone call etc.
Intent Types
In android, intent is of two types : implicit and explicit
Implicit Intent
Implicit Intent doesn't specifiy the component. In such case, intent provides information of available components provided by the system that is to be invoked.
Implicit intent example to diaplay a web page
UI for this example containts only a button on click of which you will redirect to web page.
MainActivity.java
package com.exp.implicitintent; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button1=(Button)findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { String url="www.oodlestechnologies.com"; Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse(url)); startActivity(intent); } }); }
Explicit Intent
Explicit intent specifies the component that is which class to be invoked. We can pass the information from one activity to another using explicit intent.
Explicit intent exapmle to call one activity from another activity and passing data
UI for this example contains two activities both containing a button and a text view.
activityone_main.xml
activitytwo_main.xml
MainActivityOne.java
package com.example.explicitintent; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class ActivityOne extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button1=(Button)findViewById(R.id.Button01); button1.setOnClickListener(new OnClickListener(){ public void onClick(View view) { Intent i = new Intent(getApplicationContext(), MainActivityTwo.class); i.putExtra("Value", "Welcome to second activity"); startActivity(i); } }); } }
MainActivityTwo.java
package com.example.explicitintent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class ActivityTwo extends Activity { @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); TextView tv=new TextView(this); tv.setText("second activity"); setContentView(R.layout.activity_two); Bundle extras = getIntent().getExtras(); String value = extras.getString("Value"); Toast.makeText(getApplicationContext(),"Value is: "+value1,Toast.LENGTH_LONG).show(); Button button1=(Button)findViewById(R.id.Button01); button1.setOnClickListener(new OnClickListener(){ public void onClick(View view) { Intent i = new Intent(getApplicationContext(), MainActivityOne.class); startActivity(i); } }); } }
Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Sapna Sharma
Sapna is a bright Android Apps developer using Titanium framework. Sapna likes music and helping needy people.