Friday, 28 August 2015

Android Programming Tutorials 25 : Intents In Android

Intents:


Intents are used to navigate from one Activity to Other Activity in Android.




Android intents are mainly 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.
Actually intents are not one of Android application components,instead it is the component activating mechanism in Android. It constitutes the core message system in Android and defines a message to activate a particular component. For example, if you want to invoke a new activity from your current activity, you need to fire an intent specifying the new activity. And if you want to start other application from your activity, then also you need to fire an intent. That is by firing an intent, you are telling the Android system to make something happen.

There are two types of Intents in Android:

                                          


Explicit Intents:
In explicit Intent, we are highly specific. We specify which activity should get active on receiving the intent.These are usually used for application’s internal communications.

1.   Intent i = new Intent(getApplicationContext(), MainActivity2.class);
2.startActivity(i);                                                                                 

Implicit Intents:
In Implicit Intent we are sending a message to the Android System find a suitable Activity that can respond to the intent. For example, to send an e-mail, we can use an intent. We will also specify the data to be operated on, with the intent. On receiving the Intent, Android system will invoke an Activity

which is able to send e-mail messages with the data that we specified. If there is more than one activity is capable of receiving the Intent, the system presents a chooser to the user so that he can select which Activity/Application should handle it.

    1.   Intent intent=new Intent(Intent.ACTION_VIEW);                         
    2.   intent.setData(Uri.parse("http://www.androidambilpur.com"));   
    3.   startActivity(intent);                                                                      

OUT PUT Of The Tutorial:



    Create Android Application:

  • File >> New >> Android Application
  • Enter Project name: IntentsApp
  • Package: com.ambilpursunil.newapp
  • Keep other default selections, click Next until you reach Finish

                                   

     1.Simply Copy and Past the Code which is display below in the  MainActivity.java 

    package com.ambilpursunil.newapp;

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

    public class MainActivity extends Activity {

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
    Button sendMegs = (Button) findViewById(R.id.bSendMessage);
    sendMegs.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
              // Implicit Intent Declaration
    Intent i = new Intent(android.content.Intent.ACTION_VIEW);
                Uri.parse("http://www.gmail.com");
                startActivity(i);
                }
                });
    Button sendPhn = (Button) findViewById(R.id.bSendPhone);
    sendPhn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    //Explicit Intent Declaration
    Intent i = new Intent(android.content.Intent.ACTION_VIEW);
                Uri.parse("tel:0000 000 003");
                startActivity(i);
                }
                });
                }
               @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;
                }
      }

    2.Simple Copy and Past the below code:  actvitiy_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="@drawable/back"
        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/bSendMessage"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="83dp"
            android:text="@string/sendmessage" />

        <Button
            android:id="@+id/bSendPhone"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button1"
            android:layout_centerVertical="true"
            android:text="@string/sendphone" />

        </RelativeLayout>

    3.Copy and past the code for manifest.xml

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

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

        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.ambilpursunil.newapp.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>
        </application>

    </manifest>

    4.Right click on the project and Run As-->Android Application


    OUT PUT Of The Tutorial:


                                       


    OutPut:

    When our application launch you will see two buttons  Show Message  and  Send Phone  if you click on the show message button the it will ask you to complete the action by which program from the below option if you choose the any program and click on Just once the you will get all the details about the program For Example if you choosen About then you will get total information about you device like wise Send Phone button also choose any program work the same.     

    Please Send Your Comments To ambilpura.sunil@gmail.com

    Stay Tune For Next Tutorial... Spinner In Android:

    No comments:

    Post a Comment