Tuesday, February 21, 2012

Android application components and developing our first app (II)

On the previous entry I explained android app activity concept. Now, on this entry I'm going to explain the other components of an application and we'll make our first android app.

 INTENTS
Intents are messages that are sent between activities or services. When they are received could launch processes. They have a little capacity of transport information (not so complex, basic data types, int, float... String). We can use them, for example, when we want to launch an activity from within another  activity and also if we want an activity to return something to the launcher activity once finished.

VIEWS
These are user interface graphic components, such as buttons, input text areas, lists... We can put them on the screen using a XML file or graphically using Eclipse.

SERVICES
Services are application components running in background, because of that they haven't got a graphic interface. For example music player is a service, and the music control application sends messages to start playing, stop or pause... Another example could be whatsapp or facebook services. They're running in the background standing by to receive messages to notify it. Keyboard has a service too.

CONTENT PROVIDERS
Content providers are used to store persistent data. There are some ones already defined and we can define new ones.

BROADCAST RECEIVERS
These components response to broadcast messages. These messages are usually sent by system, although they can be sent by applications as well. For example, when we use bluetooth, we need write an broadcast receiver to catch the messages sent by the system about the bluetooth state, if it's enabled or visible... (we'll see that in further entries). These receivers use intents to comunicate.

MANIFEST
 AndroidManifest.xml it's an important file within an application because in this file we configure the application. In this file we set the application activities, which one is the main activity (which will be launched first in application), which permissions will need, (sdcard access, internet, bluetooth... ), etc...

Here we finish the android application components description (very brief), and now, we are ready to start programming our first android application:


ANDROID HELLO WORLD APPLICATION 

You can find these instructions on the android developer website.

If we want to execute the application on an emulator, we have to create an AVD - Android Virtual Device. If you want to execute the application in your physical device (mobile or tablet), you can skip this step:

Create an AVD
From within Eclipse window->preferences->Android SDK and AVD manager: click Virtual Devices.
Click New, write a name and select the android platform it will work with. We have to keep in mind one thing, an 1.1 application will run in 1.5 platform, but not necessary will work backwards.

Create a project
We will need to create an android project in Eclipse. Type name, package, activity and platform...



Create android project button

Write name and select location

Select android platform

Write main activity name and select minimum platform version to run this application
Once created, enter to src and in HelloActivity.java add this:
import android.widget.TextView;
And modify the activity to look like this:

public class HelloActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    TextView tv = new TextView(this);
                    tv.setText("HELLO COOERS, I AM ANDROID");
                    setContentView(tv);
       }
      }
 Within onCreate() method, which will be executed when activity is created, declare a TextView element, which will be used to show a text on the screen. In the following line we will set the text that will be shown, and after that, we show it on the screen using setContentView();

Save project and click Run->Run. This will launch the AVD (it will take some minutes) and then it will launch the application. If we want to run the app in a physical device, we have to plug the device to the computer before click Run. If your system recognise the device (as I explained near the end of first entry of this blog)  the application will automatically be installed and launched.


Well, we finally wrote code and have seen something on our device! But that was too easy. Don't worry because in following entries we will make things more interesting and complex.
See you soon!

No comments:

Post a Comment