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!

Monday, February 20, 2012

Android application components and developing our first app (I)

Here we are again. In the previous entry of this blog we prepared the working environment to android app programming on Ubuntu. Therefore in this post we'll make our first android application using this environment. But, before we start to program, we should know about how android apps work.

BASIC ANDROID APP COMPONENTS


These are Activities, Intents, Views, Services, Content Providers, Broadcast Receivers and finally the Manifest. In the following paragraphs I will briefly describe each one to start programming as soon as possible:

ACTIVITIES

This is the main component of an android app. The main process is usually done here, or, at least, it starts from here. We could say an activity is like  a screen on our application, moreover we can only have one active activity at the same time. So an android application usually consists of several loosely-related activities.
We could see that like a stack. Top activity would be the active one, and we only would see this one. If we push another activity , this will be on top so this will be the active one. From this activity we can return to the previous one taking it out of the stack.

Stack



I told you application activities are loosely-related, because it was designed in that way, to avoid coupling and increase cohesion between application modules, and later we'll see that complex information sharing between activities is not easy.

Activities have a well-defined life cycle starting when activity is created (either by system call or by another activity call into the same application) until it's stopped or removed.

Activity life cycle

As we can see in this diagram, an activity goes trough several states during its life. You must know that when we stop an activity, android system do not destroy it immediately, but stops it, saving its resources in case it has to restart it. System will destroy the activity when deemed appropriate, either by memory requirement or by time running. So when you close an application in your device, doesn’t mean that this application is no longer in the memory.

Each time an activity changes state, it calls a method. You can see these methods on the diagram, onCreate(), onStart()...

So, when we will programming our activities, we'll be able to execute instructions that we need into each one of these methods. Therefore, looking at this diagram, we can see that, when we launch an activity (if it was not already in memory), method onCreate() will be automatically called, and after that,  onStart() method will be called, and then onResume() will be called...
When we leave an application or when activity goes to background, onPause() method will be called, and after that onStop() method will be called. Method onDestroy() will be called when the activity is finishing or being removed by the system, as I told, either by resource requirement or something similar.

If we pay attention to the diagram, we'll see that just before execute the main code in the activity, the system always will call onResume() method, and when we leave the activity onPause() always will be called. Therefore this two plus onCreate() will be the three most commonly used methods, although we don't have to forget the other ones.

To finish with this, I must say if we want to finish an activity by code, we can call the finish() method.

On the next entry I'll explain the rest of the application components and we'll make our first app.


Thursday, February 16, 2012

Android Development Environment on Ubuntu

First things first. To app programming in Android we need an working environment. There are many possibilities to configure this environment but in this entry I'm going to explain the one that I use.

Operating System:

Ubuntu 10.10 Maverick Meerkat
Programming Environment: Eclipse Indigo + ADT Plug-In
Latest Version Android SDK

I must say that there are new available updates of this OS, but I decided to use 10.10 because  Unity desktop (or Gnome Shell) are too slow, so working with them could become a nightmare.


First: Prepare the OS

You need to install the SUN ORACLE JDK.
  1. Download JDK from ORACLE download page.
  2. Unzip the packet: #tar -xvzf jdk7u2.tar.g
  3. Move created folder to /usr/local. You can also take advantage in this point to shortening the name, for example jdk7.2
  4. Now, you can tell the OS that uses this JDK with the following comands:
  5. #update-alternatives --install
    "/usr/bin/java" "java"
    "/usr/local/jdk7.2/bin/java" 1
  6. #update-alternatives --set java
    /usr/local/jdk1.7/bin/java
  7. Then you'll need to create the JAVA_HOME environment variable and update the PATH. To do this you can edit /etc/profile and add:
  8. export JAVA_HOME=/usr/local/jdk7.2
  9. export PATH=$JAVA_HOME/bin:$PATH
  10. Execute #source /etc/profile to load the environment variable
  11. Now JDK is installed. To check it you can try #java -version and the system should show infromation of the instaled version of Java.
This first part has not been too much complicated and the upcoming ones will be even more simple.

ECLIPSE AND ANDROID SDK INSTALLATION
 
Once JDK is installed we can move to the next point. Installing Eclipse programmer framework. At the moment I'm writing, latest version of this framework is Indigo. We can download it from the official site, getting the eclipse-java-indigo-SR1-linux-gtk.tar.gz file. Unzip this file in our personal folder (for example).
Once unziped you can go to the Ubuntu Menu,  System->Preferences->Main Menu. Here you can add new element to the programming menu and select the icon you will find into the unziped Eclipse folder.
The Android Developer page recommends to modify eclipse.ini to set the configuration like this:


-XX:MaxPermSize=256m
-Xms128m
-Xmx512m

It refers to the amount of memory to use by the Java Virtual Machine.

Now we must download the Android SDK. This contains libraries and utilities to connect the devices  to the computer. Unzip it and that's time to install ADT plug-in for Eclipse. This plug-in allow the communication between Eclipse and Android SDK. To install it you can go to Help->Install new software, in the Eclipse Menu. In the appearing dialog you must type the following direction:


https://dl-ssl.google.com/android/eclipse/


Typing address in ADT plug-in installation.




Once installation is done, you have to restart Eclipse and then you must configure ADT plug-in to link the SDK Android folder you unziped in the previous step (go to Window->Preferences->Android). With this plug-in you can download all Android Versions to develop. The majority of android devices uses version 2.2.1 or later. You must know that 3.x versions are only for tablets devices, and version 4.x are for both tablets and mobile devices, but, for the time being, it works only on the brand new Samsung Galaxy Nexus.

It seems that our development environment begins to be valid, but we still have one small thing to do: we need our system detects our device to be able to install apps and debbug it from Eclipse.

In Windows we have only to install drivers, but in Linux it's different. First we have to plug device to computer using USB.


MAKE UBUNTU DETECT ANDROID DEVICE TO DEVELOPMENT

First of all, we need to know device Vendor Id: We must use lsusb command and we'll see something like this, depending on the device brand:


Bus 002 Device 005: ID 18d1:4e22 Google Inc.

We have to look at first 4 numbers of ID.
Then, we should go to /etc/udev/rules.d and we'll see what are the names of .rules files (they should begin with a number, like 70 in my case).
In this case we would create a file named 70-android.rules writing on it the following:

SUBSYSTEMS=="usb", SYSFS{idVendor}=="xxxx", MODE="0666"

where xxxx would be the first 4 numbers of ID, that is 18d1 in my particular case.
Be careful with double quotes, because if there aren't appropiate it won't work. o be sure, the better is to write yourself, don't copy-paste.

Now you must unplug device and go to platform-tools folder into Android SDK unziped folder. There must be an executable named adb. From there you can execute the folowing commands:
sudo restart udev
./adb kill-server
./adb
start-server
./adb devices
Where we should see :


List of devices attached
34346DD882B200EC
device

If there are question marks means it has not worked.


note 1:(adb is located in
androidSDK/platform-tools
if we want to use adb like a regular command we must insert it in the PATH:


export
PATH=${PATH}:/home/TU_USUARIO/android-sdk/platform-tools
export
PATH=${PATH}:/home/TU_USUARIO/android-sdk/tools


and then we will be able to use adb much more comfortably
)



note 2: If any time trying to install an app to device from Eclipse you have the following error:


Unable to open sync
connection!

You can take two ways:
  1. restart device
  2. check out debugging mode and check in then
Obviously second way is faster.

Well, following these simple instructions taken mainly from the android developer website, you can get your Ubuntu ready to start developing android applications in a comfortable way. In this website you can find guides and samples to start, and in this blog we will start developing on following entries. See you soon!






Hello

This blog has been created to provide information I have been taken about android applications development. The blog will be focused on graphic applications (games) so it will include Open GL ES concepts, but all from the beggining.