Sunday, 21 December 2014

What is Intent in Android?

An Intent is exactly what it describes. It's an "intention" to do an action.

An Intent is basically a message to say you did or want something to happen. Depending on the intent, apps or the OS might be listening for it and will react accordingly.

Think of it as a blast email to a bunch of friends, in which you tell your friend John to do something. The other folks will ignore the email, but John will react to it.

To listen for an intent (like the phone ringing, or an SMS is received), you implement a broadcast receiver.

If you want to fire off an intent to do something, like pop up the dialer, you fire off an intent saying you will.

Example:

After writing a single activity, there comes a need to transition to another activity to perform another task either with or without information from the first activity.

Android platform allows transition by means of Intent Interface.

In this example there are two activities - IntentActionDemo.java and IntentA.java that both extend the super class Activity. Do not forget to declare any new activity in the AndroidManifest.xml with permission.

I have introduced buttons in both activities which have a listener to allow an intent to transition from one intent to the other activity and vis-versa.
Simple intent example;
Note that optional step 2 was not used in our demo.
Step 1: Intent i = new Intent(context, NameOfClassToTransitionTo.class)

Step 2:(Optional)Intents can take various forms that make it even carry data in key/name pairs iei.putExtra("key1", "My first Info")
i.putExtra("key2", "My second Info")

Step 3: startActivity(i)



IntentActionDemo.java
Code:


package com.intent.example;

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

public class IntentActionDemo extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Button button = (Button) findViewById(R.id.intentButton);
        button.setOnClickListener(this);
        
    }

    @Override
    public void onClick(View src) {
      Intent i = new Intent(this, IntentA.class);
      startActivity(i);
    }
}


IntentA.java

Code:


package 
com.intent.example;

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

public class IntentA extends Activity implements OnClickListener{

    @Override
    public void onClick(View src) {
      Intent i = new Intent(this, IntentActionDemo.class);
      startActivity(i);
    }
   
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.intenta);
       
        Button button = (Button) findViewById(R.id.ButtonIntentA);
        button.setOnClickListener(this);
    }
}


AndroidManifest.xml
Code:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="
com.intent.example"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".IntentActionDemo"
                  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="IntentA"></activity>
</application>
    <uses-sdk android:minSdkVersion="3" />

</manifest>


string.xml

Code:


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello</string>
    <string name="app_name">IntentActionDemo</string>
<string name="IntentA">Click Next intent</string>
<string name="world">World!</string>
<string name="Intent0">Click to Previous Intent</string>
</resources>


main.xml
Code:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    android:textSize="18sp"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/intentButton" android:text="@string/IntentA"></Button>
</LinearLayout>


intenta.xml
Code:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:orientation="vertical">

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/TextViewWorld" android:text="@string/world" android:textSize="18sp"></TextView>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ButtonIntentA" android:text="@string/Intent0"></Button>
</LinearLayout>


1 comment:

Blogger said...

Okay...

This might sound really weird, maybe even a little "strange"

HOW would you like it if you could just click "PLAY" to LISTEN to a short, "musical tone"...

And miraculously attract MORE MONEY into your life??

And I'm really talking about hundreds... even thousands of dollars!!

Do you think it's too EASY?? Think it's IMPOSSIBLE?

Well then, Let me tell you the news.

Sometimes the most magical blessings life has to offer are the easiest to RECEIVE!!

Honestly, I will provide you with PROOF by letting you PLAY a REAL "magical money tone" I've produced...

You just click "PLAY" and the money will start coming into your life... starting pretty much right away...

TAP here to play this magical "Miracle Money-Magnet Tone" - it's my gift to you!!