Intent is the “message that is passed between components” such as activities, content providers, broadcast receivers, services etc. The dictionary meaning of intent is intention or purpose. So, it can be described as the intention to do action.

It is 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.

Types of Intents:
There are two types of intents in android:
1.       Implicit Intent.
2.     Explicit Intent.

1. Implicit Intent:

Implicit Intent doesn't specify the component (activities, content providers, broadcast receivers, services etc). In such case, intent provides information of available components provided by the system that is to be invoked.

For example, write the following code to view the webpage.

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

2. Explicit Intent:

Explicit Intent specifies the component. In such case, intent provides the external class to be invoked.

For example, write the following code to pass the information from one activity to another using explicit intent.

Intent i = new Intent(getApplicationContext(), ActivityTwo.class);
startActivity(i);
An Activity is an application component that provides a screen with users can interact in order to do something, such as dial the phone, take a photo, view a map, or send an email etc. Each activity is given a window in which to draw its user interface.

How to create an Activity:
To create an activity, you must create a subclass of Activity.
onCreate():
You must implement this method. The system calls this when creating your activity.
onPause():
The system calls this method as the first indication that the user is leaving your activity. 

Implementing a user interface:
  • The user interface for an activity is provided by a hierarchy of views-objects derived from the View class.
  • The most common way to define a layout using views is with XML layout file saved in your application resources.
  • You can set the layout as the UI for your activity with setContentView(), passing the resource ID for the layout.
Declaring the activity in the manifest:
You must declare your activity in the manifest file in order for it to be accessible to the system.
<manifest ... >
  
<application ... >
      
<activity android:name=".HelloActivity" />
      ...
  
</application ... >
  ...
</manifest >
Managing the Activity LifeCycle:
We can control and manage the resource through the life cycle methods of activity.
There are 7 life cycle methods of android.app.Activity class. They are as follows:
onCreate() –called when activity is first created.
onStart() –called when activity is becoming visible to the user.
onResume() –called when activity will start interacting with the user.
onPause() –called when activity is not visible to the user.
onStop() –called when activity is no longer visible to the user.
onRestart() –called after your activity is stopped, prior to start.
onDestroy() –called before the activity is destroyed.

package com.example.helloworld;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class HelloActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
        }
       
        @Override
        protected void onStart() {
                super.onStart();
                Toast.makeText(getApplicationContext(), "onStart() invoked", 2000).show();
        }
       
        @Override
        protected void onResume() {
                super.onResume();
                Toast.makeText(getApplicationContext(), "onResume() invoked", 2000).show();
        }
       
        @Override
        protected void onPause() {
                super.onPause();
                Toast.makeText(getApplicationContext(), "onPause() invoked", 2000).show();
        }
       
        @Override
        protected void onRestart() {
                super.onRestart();
                Toast.makeText(getApplicationContext(), "onRestart() invoked", 2000).show();
        }
       
        @Override
        protected void onStop() {
                super.onStop();
                Toast.makeText(getApplicationContext(), "onStop() invoked", 2000).show();
        }
       
        @Override
        protected void onDestroy() {
                super.onDestroy();
                Toast.makeText(getApplicationContext(), "onDestroy() invoked", 2000).show();
        }
}
Android is now more popular than IOS or the Windows Phone. The OS tends to garner rave reviews for its ease of use and open source, but many IOS or Windows phone users argue that their preferred operating system can do just as much. Android supports great features. Few of them are listed below.

Beautiful User Interface:
  • Android OS basic screen provides a beautiful and intuitive user interface.
Screen Capture:
  • Android supports capturing a screenshot.
Media Support:
  • H.263, H.264, MPEG-4 SP, AMR, AMR-WB, AAC, HE-AAC 5.1, MP3, MIDI, WAV, JPEG, PNG, GIF, and BMP, etc.
Storage:
  • SQLite, a light weight relational database is used for data storage purposes.
Connectivity:
  • Bluetooth, WI-FI GSM/EDGE, IDEN, CDMA, EV-DO, UMTS, LTE, NFC and WiMAX.
Multi-touch:
  • Android has native support for multi-touch which was initially made available in handsets such as the HTC Hero.
Multi-tasking:
  • User can jump from one task to another and same time various applications can run simultaneously.
Messaging:
  • SMS and MMS.
GCM:
  • Google Cloud Messaging (GCM) is a service that lets developers send short message data to their users on Android devices, without needing a proprietary sync solution.
Multi-Language:
  • Supports single direction and bi-directional text.
Web Browser:
  • Based on the open-source WebKit layout engine, coupled with Chrome’s V8 JavaScript engine supporting HTML5 and CSS.
WI-FI Direct:
  • A technology that lets apps discover and pair directly, over a high-bandwidth peer-to-peer connection.
Video Calling:
  • Android does not support native video calling, but some handsets have a customized version of the operating system that supports it.
Automation:
  • The Tasker app lets you not only control app permission but also automate them.

⇚ PREV NEXT ⇛
Dalvik is the process virtual machine(VM) in Google’s Android OS system, which specifically, executes applications written for Android. This makes Dalvik an integral part of the Android software stack, which is typically used on mobile devices such as mobile phones and tablet computers, as well as more recently on devices such as smart TV’s and Wearables.

As we know the modern JVM is high performance and provides excellent memory management. But it needs to be optimized for low-powered handheld devices as well.

The Dalvik Virtual Machine (DVM) is an android virtual machine optimized for mobile devices. It optimizes the virtual machine for memory, battery life and performance.

Dalvik is a name of a town in Iceland. The Dalvik VM was written by Dan Bornstein.

The Dex compiler converts the class files into the .dex file that run on the Dalvik VM. Multiple class files are converted into one dex file.
  • The javac tool compiles the java source file into the class file.
  • The dx tool takes all the class files of your application and generates .dex file. It is a platform- specific tool.
  • The Android Assets Packaging Tool (aapt) handles the packaging process.
The first step is to create a simple Android Application using Eclipse IDE. Follow the option File -> New -> Project and finally select Android New Application wizard from the wizard list. Now name your application as HelloWorld using the wizard window as follows:
Next, follow the instructions provided and keep all other entries as default till the final step. Once your project is created successfully, you will have following project screen:


Directory Structure of Android Application 
Before you run your app, you should be aware of a few directories and files in the Android project:

1) src
  • This contains the .java source files for your project. By default, it includes an MainActivity.java source file having an activity class that runs when your app is launched using the app icon.
2) gen
  • This contains the .R file, a compiler-generated file that references all the resources found in your project. You should not modify this file.
3) bin
  • This folder contains the Android package files .apk built by the ADT during the build process and everything else needed to run an Android application.
4) res/drawable-hdpi
  • This is a directory for drawable objects that are designed for high-density screens.
5) res/layout
  • This is a directory for files that define your app's user interface.
6) res/values
  • This is a directory for other various XML files that contain a collection of resources, such as strings and colors definitions.
7) AndroidManifest.xml 
  • This is the manifest file which describes the fundamental characteristics of the app and defines each of its components.
Following section will give a brief overview few of the important application files.

The Main Activity File


The main activity code is a Java file MainActivity.java. This is the actual application file which ultimately gets converted to a Dalvik executable and runs your application. Following is the default code generated by the application wizard for Hello World! application.
package com.example.helloworld; 

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
Here, R.layout.activity_main refers to the activity_main.xml file located in the res/layout folder. The onCreate() method is one of many methods that are fi red when an activity is loaded.

The Manifest File(AndroidManifest.xml)

Whatever component you develop as a part of your application, you must declare all its components in a manifest file called AndroidManifest.xml which ressides at the root of the application project directory. This file works as an interface between Android OS and your application, so if you do not declare your component in this file, then it will not be considered by the OS. For example, a default manifest file will look like as following file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.helloworld"
   android:versionCode="1"
   android:versionName="1.0" >
   <uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="15" />
   <application
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/AppTheme" >
       <activity
           android:name=".MainActivity"
           android:label="@string/title_activity_main" >
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER"/>
           </intent-filter>
       </activity>
   </application>
</manifest>
Here <application>...</application> tags enclosed the components related to the application. Attribute android:icon will point to the application icon available under res/drawable-hdpi. The application uses the image named ic_launcher.png located in the drawable folders
The <activity> tag is used to specify an activity and android:name attribute specifies the fully qualified class name of the Activity subclass and the android:label attributes specifies a string to use as the label for the activity. You can specify multiple activities using <activity> tags.
The action for the intent filter is named android.intent.action.MAIN to indicate that this activity serves as the entry point for the application. The category for the intent-filter is named android.intent.category.LAUNCHER to indicate that the application can be launched from the device's launcher icon.
The @string refers to the strings.xml file explained below. Hence, @string/app_name refers to the app_name string defined in the strings.xml fi le, which is "HelloWorld". Similar way, other strings get populated in the application.
Following is the list of tags which you will use in your manifest file to specify different Android application components:

  • <activity>elements for activities
  • <service> elements for services
  • <receiver> elements for broadcast receivers
  • <provider> elements for content providers

The Strings File(Strings.xml)

The strings.xml file is located in the res/values folder and it contains all the text that your application uses. For example, the names of buttons, labels, default text, and similar types of strings go into this file. This file is responsible for their textual content. For example, a default strings file will look like as following file:

<resources>
    <string name="app_name">HelloWorld</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
</resources>

The R.java File

The gen/com.example.helloworld/R.java file is the glue between the activity Java files like MainActivity.java and the resources like strings.xml. It is an automatically generated file and you should not modify the content of the R.java file. Following is a sample of R.java file:

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package com.example.helloworld;

public final class R {
    public static final class attr {
    }
    public static final class dimen {
        public static final int padding_large=0x7f040002;
        public static final int padding_medium=0x7f040001;
        public static final int padding_small=0x7f040000;
    }
    public static final class drawable {
        public static final int ic_action_search=0x7f020000;
        public static final int ic_launcher=0x7f020001;
    }
    public static final class id {
        public static final int menu_settings=0x7f080000;
    }
    public static final class layout {
        public static final int activity_main=0x7f030000;
    }
    public static final class menu {
        public static final int activity_main=0x7f070000;
    }
    public static final class string {
        public static final int app_name=0x7f050000;
        public static final int hello_world=0x7f050001;
        public static final int menu_settings=0x7f050002;
        public static final int title_activity_main=0x7f050003;
    }
    public static final class style {
        public static final int AppTheme=0x7f060000;
    }
}

The Layout File(activity_main.xml)

The activity_main.xml is a layout file available in res/layout directory, that is referenced by your application when building its interface. You will modify this file very frequently to change the layout of your application. For your "Hello World!" application, this file will have following content related to default layout:


<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" >

   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerHorizontal="true"
       android:layout_centerVertical="true"
       android:padding="@dimen/padding_medium"
       android:text="@string/hello_world"
       tools:context=".MainActivity" />
</RelativeLayout>
 
This is an example of simple RelativeLayout which we will study in a separate chapter. The TextView is an Android control used to build the GUI and it have various attribuites like android:layout_width, android:layout_height etc which are being used to set its width and height etc. The @string refers to the strings.xml file located in the res/values folder. Hence, @string/hello_world refers to the hello string defined in the strings.xml fi le, which is "Hello World!".

Running the Application

Let's try to run our Hello World! application we just created. I assume you had created your AVD while doing environment setup. To run the app from Eclipse, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Eclipse installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following Emulator window:



Android Studio provides everything need to start developing apps for Android, including the Android Studio IDE and the Android SDK tools.

If you didn’t download Android Studio, go download Android Studio now, or switch to the stand-alone SDK Tools install instructions.

Before you set up Android Studio, be sure you have installed JDK 6 or higher (the JRE alone is not sufficient) JDK 7 is required when developing for Android 5.0 and higher.

To check if have KDK installed (and which version), open a terminal and type javac –version. If the SDK is not available or the version is lower than 6, go download JDK.

To setup Android Studio on Windows:

Launch the .exe file you just downloaded follow the setup wizard to install Android Studio and any necessary SDK tools. On some windows systems, the launcher script does not find where java is installed. If you encounter this problem, you need to set an environment variable indicating the current location.

Select Start menu > Computer > System Properties > Advanced System Properties. Then open Advanced tab > Environment Variables and add a new system variable JAVA_HOME that points to your JDK folder. For example C:\Program Files\Java\jdk1.7.0_21;

To set up Android Studio on Mac:

Launch the .dmg file you just downloaded.
Drag and drop Android Studio into the Applications folder.
Open Android Studio and follow the setup wizard to install any necessary SDK tools.

Depending on your security settings, when you attempt to open Android Studio, you might see a warning that says the package is damaged and should be moved to the trash. If this happens, go to System Preferences > Security & Privacy and under Allow applications downloaded from, select anywhere. Then open Android Studio again.

To setup Android Studio on Linux:

Unpack the downloaded ZIP file into an appropriate location for your applications. To launch Android Studio, navigate to the android-studio/bin/ directory in a terminal and execute studio.sh. You may want to add android-studio/bin/ to your PATH environmental variable so that you can start Android Studio from any directory. Follow the setup wizard to install any necessary SDK tools.

Android Studio is now ready and loaded with the Android developer tools, but there are still a couple packages you should add to make your Android SDK complete.

Adding SDK Packages:

To start adding packages, launch the Android SDK Manager in one of the following ways:
  • In Android Studio, click SDK Manager  in the toolbar.
  • If you’re not using Android Studio:
  1. Windows: Double-click the SDK Manager.exe file at the root of the Android SDK directory.
  2. Mac/Linux: Open a terminal and navigate to the tools/directory in the Android SDK, then execute android SDK.
When you open the SDK Manager for the first time, several packages are selected by default. Leave these selected, but be sure you have everything you need to get started by following these steps: 
  • Get the latest SDK tools.
  • Get the support library for additional APIs.
  • Get Google Play Services for even more APIs.
  • Install the packages.
  • Building your first app.
The Android SDK includes a mobile devices emulator –a virtual mobile device that runs on your computer. The emulator lets you develop and test Android applications without using a physical device.

Android Emulator is used to run, debug and test the android application. If you don’t have the real device, it can be the best way to run, debug and test the apps.

It uses an open source processor emulator technology called QEMU.
In the given image, you can see the sample android emulator image.


Keyboard Commands:
Below table.1 summarizes the mappings between the emulator keys and the keys of your keyboard.

Table 1. Emulator keyboard mapping.

Emulated Device key
Keyboard Key
Home
HOME
Menu(left softkey)
F2 or Page-Up button
Star (right softkey)
Shift-F2 or Page Down
Back
Esc
Call/Dial button
F3
End call
F4
Search
F5
Power Button
F7
Previous PostOlder Posts Home