 |
|
 |
|
Senior Member
Join Date: Jan 2008
Age: 23
Posts: 1,178
Rep Power: 4 
|
|
 |
|
 |
|
 |
|
 |
|
Build Mobile Apps with Google Android
 |
|
 |
|
The whole of last year there were speculations about Google com¬ing up with its own mobile
phone, termed as GPhone. But last No¬vember, the air was cleared of all rumors when Open Handset Alliance (aHA) re¬leased Android, a complete set of soft¬ware for mobile devices that includes an operating system, middleware and keyap¬plications. The aHA is a group of 34 lead¬ing multinational companies, including leading mobile operators, handset manu¬facturers, softr'are companies, semicon¬ductor companies and commercial companies. They had teamed up to de¬velop a mobile phone that could give richer and more cohesive experience to users. And Google, as a member of the al¬liance has released Android SDK for the Android platform. Using this SDK, developers would be able to create mobile ap¬plications that would be able to harness all features of a handset.
For example, apps could be created to call upon a phone's core functionality like making calls, sending SMSes, accessing the calendar or even using the camera as a utility. In this article, we will tell you about Android and how to use it for developing mobile applicatoins.
Getting started
The Android SDK has been distributed with this month's PC<LProfessional CD. In Windows, only XP and Vista are sup¬ported. Other platforms supported by Android SDK are Linux and Mac as X. To start application development you would also need Java SDK and Eclipse IDE. Android requires JDK 5 or later to work. Previous versions of JDK and even the Java Runtime Environment are not sufficient for it. Plus, it supports Eclipse 3.2 or 3.3 (Europa) for developing the mobile apps. Once you have installed your JDK and configured Eclipse IDE, you can straightaway start up with An¬droid development. For this article we used JDK 6 and Europa as the IDE on a Vista machine.
Extract the contents of the zipped Android SDK to a folder. When you ex¬pand the directory you will find three folders: samples, docs and tools. You have to give the same directory path to the Tools folder as the path for environ¬ment variables.
Once you have config¬ured the path entry for system environment variables, you can start configuring the Android plugin for Eclipse. Start Eclipse and from Help> Software Updates> Find and Install, you can download and install the An¬droid plugin.ln the next window select the option 'Search for New features to Install,' and click on Next. Then on In¬stall window, select New Remote Site and in the New Update Site dialog box enter the name as Android and URL Click OK and the Android entry is added to the search list on the main win¬dow. Now press Finish to proceed with the search for the Eclipse updates. In the sub¬sequent Search Result dialog box, select the checkbox Android> developer> An¬droid Development Tools (ADT) and then click Next to proceed with the download. In the subsequent window accept the agreement and click Next button, while in the last window press Finish. The ADT is not signed and while installation asks for acceptance, select 'Install All' to complete the download and installation process.
Once irlstalled,Eclipse restarts for the plu¬gin to configure up. Now we can proceed up with Android development process within the Eclipse IDE, but before that we need to set Eclipse Preferences to point to the Android SDK directory. This can be configured by selecting Window> Prefer¬ences' and later clicking on Android on the left panel. In the main panel, browse for the SDK's location and then press OK to con¬figure Android for Eclipse preferences. Now we can start with the development of an Android based application.
Building an application Through the Android documentation provided along with the SDK you can start up with a 'Hello World' kind of appli¬cation. Here we would try to explore a lit¬tie more of the SDK framework; about the layout of VIs and Views and Activities. To start developing the demo application, first create a new Android Project by se¬lecting File> New> Project menu. On the New Android Project window name the project as DemoAndroid, ftll the Properties values for Package name, Activity name and Application name as shown in the visual. Upon clicking the Finish button, the Android plugin runs and the auto-generated code for the project De¬moAndroid gets created. The DemoAn¬droidApp class will be extending Activity class. An Activity is a thing that interacts with the user and is responsible for a window being created on which the VI components are placed. There are two methods that each subclass of Activity would implement:
onCreate(Bundle):- It initializes the activity and by method setCon¬tentViewO it describes the layout re¬source that defines the User Interface (VI). The method findViewByIdO is used to retrieve the VI components in that VI with which you want to interact with programmatically.
onPauseO :- You override this method when you are dealing with a user who is leaving your activity, and you have to commit all changes done by the user before he leaves the activity.
Along with the DemoAndroidApp class you will find R.java file. It is an in- dex for all the resources defined in the file. You use this class in your source code as a sort of short-hand way to refer to resources you've included in your project. We know that the method set¬ContentViewO looks for a resource that defines the UI. This definition file is an XML file and can be found in res direc¬tory under the main project directory. With res directory you can see there are directories named layout, drawable and values. The layout directory holds the main.xml file, which will be our VI defi¬nition file for the project.
Other two di¬rectories are: Drawable, which holds the resource definition for the VI compo¬nents design scheme; and 'Colors while values; that holds values for VI compo¬nents, Qe it a color or a string.
For our demo application we intend to ask user to enter his name and upon clicking the submit button, a message gets displayed that welcomes the user. For this we need an inputbox and a few text labels and a button. In Android we have EditText in place of inputbox and TextView to act as labels. The default layout is Linear¬Layout, that we'll use and add the following code for defining the VI before the Linear¬Layout attribute gets closed: This way we have defined few TextViews for displaying the text and an EditText to take input from the user. Now we can programmatically call the layout and use the components to ask user for an input and upon selecting the button he gets a response. The following code snip¬pet for DemoAndroidApp shows how, using the setContentViewO method, we call upon the layout defined in main.xml and also how components like EditBox are be¬ing called upon by referring their ids as defined in main.xml file. We associate a listener to the button so that when the user clicks on the button he gets a welcome message as a response. Now we can run and execute the application.
Executing on Android emulator
Once you are done with coding, you can start running your application by select¬ing Run> open Debug Dialog. In the fol¬lowing Debug window, from the left pane, select the Android application and a new launcher entry appears with the name New_Configuration. Change the name of launcher to demoApp, and then under An¬droid tab browse and select the current project. From the Activity option select the activity, which in our case is DemoAn¬droidApp. On pressing the Run button, the debugger starts the Android emulator, on which the application gets executed.
This way we have run a simple app where we defined the VI. You can refer the Android's documentation for more sam¬ple applications and other APIs. The com¬plete source code for this project can be found on PCQ Forums under Current
Issue thread.
|
|
 |
|
 |
|
|
 |
|
 |
|