Thursday, 27 February 2014

Android Explicit Intent With Example

Intents are used as a message passing mechanism within the application and between the applications.

An intent generically defines an “intention” to do some work. Intents encapsulate several concepts


You can use intents to invoke other applications from your application. You can use intents to invoke internal or external components from your application .

  Intents are used to perform the following tasks :-
·                        Start Activity to launch an Activity
·                        Broadcast Intent to send it to any interested Broadcast Receiver components. 
·                        Start Service to communicate with a background Service .intents ares always initiated by your application , An intent can be Explicit or implicit .

Explicit Intent: - 

In an explicit intent, we actually specify the activity that is required to respond to the intent. In other words, we explicitly designate the target component.
XML Layout:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/first_activity" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button" />

</LinearLayout>

activity_second.xml

<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"
    tools:context=".SecondActivity" >

    <TextView
        android:id="@+id/textview_Second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/second_activity" />

</RelativeLayout>
Activities:-

MainActivity

package com.CyberInnovation.explicitintent;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

                Button button;

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.activity_main);
                                button = (Button) findViewById(R.id.button1);
                                button.setOnClickListener(new View.OnClickListener() {

                                                @Override
                                                public void onClick(View v) {

                                                                Intent intent = new Intent(MainActivity.this,
                                                                                                SecondActivity.class);
                                                                startActivity(intent);
                                                }
                                });
                }

               
                @Override
                public boolean onCreateOptionsMenu(Menu menu) {
                                // Inflate the menu; this adds items to the action bar if it is present.
                                getMenuInflater().inflate(R.menu.main, menu);
                                return true;
                }

}

SecondActivity:-

package com.CyberInnovation.explicitintent;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class SecondActivity extends Activity {

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.activity_second);
                }

                @Override
                public boolean onCreateOptionsMenu(Menu menu) {
                                // Inflate the menu; this adds items to the action bar if it is present.
                                getMenuInflater().inflate(R.menu.second, menu);
                                return true;
                }

}
Create a class Explicit_intent and extend it to activity class and override the onCreate() method of activity class and set the content of the activity with the above defined xml file by calling setContentView() method and passing xml file name as a parameter to it                                    setContentView(R.layout.screen1);  

And inside the Explicit_intent class set the onclick listener for button and create the intent , and pass the intent object to startActivity method as a paramter to it .

AndroidMainifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.CyberInnovation.explicitintent"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.CyberInnovation.explicitintent.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.CyberInnovation.explicitintent.SecondActivity"
            android:label="@string/title_activity_second" >
        </activity>
    </application>

</manifest>


RUN




No comments:

Post a Comment