Tuesday, February 25, 2014

Intent in Android

An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components

An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities.
Simply and activity means if you press on particular button the another activity will be open.

Types of Android Activities/Intent.
  • Explicit Intent
  • Implicit Intent
Explicit Intent: After writing a single Activity, There comes a need   to call another activity to perform another task . For this we need the Explit Intent.

   Android provide android.content package in that we have to use Intent class. import the that class. as like import android.content.Intent;  
                                                       1)MainActivity.
   
                                                 2)AnotherActivity


Steps to implement Explicit Intent.

Step 1:- create New android project.

Step 2: Take one button in activity_main.xml file .
Step 3: set onClickListner  on them.
Step 4: create Intent class object and pass two argument of that class constructor that is (getApplicationContext(), AnotherActivity.class) 
1) first parametr is getApplicationContext() it will call the system resources
2)second parameter is your another activity class file name.

Step 5:How to create new activity in you project.
  •    Click on your project and press ctrl+n . or click on the prject then new->other->AndroidActivity.
        
  •    Click Next Button then give the of your own name for the your activity.

         Then automatically you .java and .xml file will be created.You can chek.
 

Following are the coding file of this Application.

MainActivity.java
package com.example.explicitintentdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    Button b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        b=(Button)findViewById(R.id.button_call_another_activity);
        b.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
            Intent intent=new Intent(getApplicationContext(),AnotherActivity.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;
    }

}


AnotherActivity.java
package com.example.explicitintentdemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class AnotherActivity extends Activity {

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

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

}

activity_main.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"
    android:background="#ff00ff"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button_call_another_activity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="41dp"
        android:text="@string/call_another_activity" />

</RelativeLayout>
 
activity_another.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"
     android:background="#fff000"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".AnotherActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="62dp"
        android:text="This is Another Activity"
        android:textSize="30sp" />

</RelativeLayout>
String.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">ExplicitIntentDemo</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="call_another_activity">Call Another Activity</string>
    <string name="title_activity_another">AnotherActivity</string>

</resources>


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.explicitintentdemo"
    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.example.explicitintentdemo.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.example.explicitintentdemo.AnotherActivity"
            android:label="@string/title_activity_another" >
        </activity>
    </application>

</manifest>

Output:-
                                                         Our Application Output will be.
                                                  1)MainActivity
                                        if you click on call Another Activity then AnotherActiviy will be call.

No comments:

Post a Comment