Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Implicit intent in android with example

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 106
    Comment on it

    Intent:

    Subclass : android.content.Intent class

    Intent is basically a way of communication between the various components such as activities, broadcast receivers , etc.

    We use it with startActivity() method to start an activity.

    As the term says, its purpose is to perform any operation or action depending on parameters passed.

    e.g. They can be used to open a message, open a page in the browser , open some location in the google map, etc.

    Implicit Intent:

    This type of intent just tells about the action which needs to be performed and it does not tell that which component should be used by the system to perform the specified action.

    It is left over to the system that which component to choose from the available ones and perform the action.

    e.g

    View a webpage:

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

    So we are just specifying that it should open www.google.com but we don't specify that which browser should be used. It is left over to the system whether it can open it with chrome, firefox, etc. 

    Generally the default browser will open up this page.

    Lets see an example:

    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=".MyActivity" >  
        <EditText  
            android:id="@+id/myText"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_alignParentTop="true"  
            android:layout_centerHorizontal="true"  
            android:layout_marginTop="108dp"  
            android:ems="10" />  
        <Button  
            android:id="@+id/myButton"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_below="@+id/Text"  
            android:layout_centerHorizontal="true"  
            android:layout_marginTop="108dp"  
            android:text="Visit" />  
    </RelativeLayout>  

    MyActivity.java

    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;  
    import android.widget.EditText;  
    public class MyActivity extends Activity {  
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
            final EditText editText=(EditText)findViewById(R.id.myText);  
            Button button=(Button)findViewById(R.id.myButton);  
            button.setOnClickListener(new OnClickListener() {  
                @Override  
                public void onClick(View view) {  
                    String myUrl=editText.getText().toString();  
                    Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse(myUrl));  
                    startActivity(intent);  
                }  
            });  
        }  
    }  
    

    This will display the url entered in the Text Box when you click the button.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: