What is Android Intent

What is Intent and what are the types of it ?

Android Intent is the message that is gone between segments, for example, exercises, content suppliers, telecast recipients, administrations and so on.

It is by and large utilized with startActivity() strategy to conjure action, telecast recipients and so forth.

The word reference significance of purpose is proposition or reason. Thus, it can be portrayed as the aim to do activity.

The LabeledIntent is the subclass of android.content.Intent class.

Android expectations are essentially used to:

  • Start the service
  • Launch an activity
  • Display a web page
  • Display a list of contacts
  • Broadcast a message
  • Dial a phone call etc.

Types of Android Intents

There are two types of intents in android: implicit and explicit.

1) 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.
For example, you may write the following code to view the webpage.

Intent intent=new Intent(Intent.ACTION_VIEW);  
intent.setData(Uri.parse("http://dheeruapps.blogspot.com"));  
startActivity(intent);  

2) Explicit Intent

Explicit Intent specifies the component. In such case, intent provides the external class to be invoked.

Intent i = new Intent(getApplicationContext(), ActivityTwo.class);  
startActivity(i);  

Android Implicit Intent Example

activity_main.xml

File: activity_main.xml
<RelativeLayout xmlns:androclass="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" >  
    <EditText  
        android:id="@+id/editText1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentTop="true"  
        android:layout_centerHorizontal="true"  
        android:layout_marginTop="44dp"  
        android:ems="10" />  
    <Button  
        android:id="@+id/button1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_below="@+id/editText1"  
        android:layout_centerHorizontal="true"  
        android:layout_marginTop="54dp"  
        android:text="Visit" />  
</RelativeLayout>  


Activity class

File: MainActivity.java
  1. package org.dheeruapps.implicitintent;  
  2. import android.net.Uri;  
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10. public class MainActivity extends Activity {  
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.         final EditText editText1=(EditText)findViewById(R.id.editText1);  
  16.         Button button1=(Button)findViewById(R.id.button1);  
  17.         button1.setOnClickListener(new OnClickListener() {  
  18.             @Override  
  19.             public void onClick(View arg0) {  
  20.                 String url=editText1.getText().toString();  
  21.                 Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse(url));  
  22.                 startActivity(intent);  
  23.             }  
  24.         });  
  25.     }  
  26. }  



0 comments:

Post a Comment

My Instagram