Getting Down and Dirty with Google’s Android

Getting Down and Dirty with Google’s Android

ust a few weeks ago, the Open Handset Alliance pushed its baby “Android” out into the spotlight. While no devices available on the market run it, Google has offered 10 million dollars in prize money to the developers who create the most compelling applications. The code exposed in this article isn’t going to win any of that money on its own, but perhaps it can get you started towards your own Android application. After you finish this article, you’ll understand the basics of setting up the SDK, displaying text using the XML layout schema, getting the device’s location from the GPS component, and finally rendering your own text with a custom view implementation.

Getting Setup
The code for this article was written on Mac OS X with Eclipse. Since Android ships with a plug-in for Eclipse it seemed the natural choice for a get-to-know-you article. Your mileage may vary. If you want to follow along directly with this article you can start by grabbing the following software:

If you’re using a different SDK, go ahead and skip down to the next section.

To get started, install Eclipse, then get the Android SDK and install the plug-in with the directions linked above. Creating an Android project should now be as simple as clicking “New” in the file menu. Traditionally, new languages are first demonstrated with the help of a “Hello World” application. But, since “Hello World” is so last decade, we’ll focus on something a little more interesting like “Hello World: Here I am.” Along the way, we’ll talk about custom vs. XML-driven views, GPS, and a few of the differences between other SDKs and Android.

Activities
Every mobile environment has its foundation class. J2ME applications have midlets, BREW has applets, and Android uses activities. The activity provides you with basic access to the phone’s OS and events. It will contain your base constructor, key handler, suspend/resume functionality, and other low-level hand-set access. Your app, in essence, will be an extension of the Activity class. Here’s how our sample application’s Activity looked before we cluttered it up with functionality:

public class LocateMe extends Activity {	public void onCreate(Bundle params) 	{    	super.onCreate(params);    	setContentView(R.layout.main);	}	public boolean onKeyDown(int keyCode, KeyEvent event)     	{		return true;	}}

The onCreate function will be called when your application is started for the first time. The bundle object contains basic information including any launch parameters or environment data needed on startup. Activities can be full screen or floating, they can be nested but each is a semi-autonomous unit. What, you might ask, is setContentView?

The “View”
The View class is Android’s super-class for nearly all screen rendering; there are several varieties. Each view has a rectangle it’s responsible for drawing on. That rectangle can be defined and extended in a myriad of ways. For the sake of expediency this article will only cover two major players: Custom Views and Android’s XML content views. Since the basic project, upon creation, starts with a pre-defined “Hello World” XML view, that seems a natural place to start.

If you snoop around in a new Android project, at some point you’ll find a file called main.xml. This file, through a simple XML schema, describes a screen layout to Android. Here’s what our sample application’s main.xml looks like:

While this seems relatively straightforward, let’s go over it anyway. This particular file defines a relative layout, which means all the elements are described by their relation to one-another or to their parent. As with Views, there are several layout methods but we’ll stick to this one for the sake of clarity.

The RealtiveLayout listed above contains one text element which is set to fill its parent (your LocateMe activity.) The LocateMe activity is full screen by default, so the text element will inherit that property and the text will appear in the top left corner of the screen. Additionally, a reference number must be set for the XML file so that Android can find it from your source code. By default, these reference numbers are stored in R.java and will look something like this:

public final class R { 	 public static final class layout 	{	        public static final int main=0x7f030001;    	}}

Views may be nested so, unlike in J2ME, it’s possible to mix custom views with the Widgets released by the Android team. In J2ME, developers were forced to choose between a GameCanvas and J2ME’s application canvas. This meant that if you wanted a custom look, you’d have to roll all your own widgets on top of the GameCanvas. Not so with Android; view types can be mixed and matched in code with ease. Android also comes with a widget library (built on top of the View class) for scrollbars, text-entry, progress-bars, and many more. These standard widgets can be overloaded and customized at the whim of your UI team. Now that you’ve a few building blocks in place, it’s time to get into the sample application.

Android in Action
This demonstration application will display the user’s current latitude and longitude in text form. The constructor onCreate will be identical to the first listing we covered, except you’ll add a bunch of code to the key handler. Now, onKeyDown will look like this:

public boolean onKeyDown(int keyCode, KeyEvent event) {	   	//Head out if we're loading or if it isn't the select key   	if(keyCode != KeyEvent.KEYCODE_DPAD_CENTER || m_bLoading)   	{   		return true;   	}   	   	//Flip the loading bit so we don't start this again.   	m_bLoading = true;   	   	getLocation();   	   	return true;}

Let’s break this down. First, the code checks to make sure the correct key has been struck and that it hasn’t started this process already, as you only want to call getLocation() once. Then, it will flip the loading flag and call into getLocation(). Here’s the getLocation function:

private void getLocation(){	Location loc;	LocationManager locMan;	LocationProvider locPro;	List proList;	//Show "Loading" on the screen.	setContentView(R.layout.laoding);					//Get the location manager from the server	locMan = (LocationManager) getSystemService(LOCATION_SERVICE); 	proList = locMan.getProviders();		//Just grab the first member of the list. It's name will be "gps"	locPro = proList.get(0);	loc = locMan.getCurrentLocation(locPro.getName());	Lat =  (float)loc.getLatitude();	Lon =  (float)loc.getLongitude();	CreateView();	setContentView(customView);}

This is where things start getting a little more interesting. Sadly, Google’s documentation on this subject is not overly helpful. After the variable declarations, you’ll want to display some loading information. R.layout.loading corresponds to another simple XML layout View. A simple setContentView() call will paint the screen with the loading message.

Author’s Note: At compile time, Android pre-packs all XML layout data. If you want to change layout properties after compile, as it stands now, you’ll have to do it programmatically.

The only method for obtaining a LocationManager is through a getSystemService() call. With a LocationManager in hand, it’s possible to get a list of location providers. On an actual handset this list could contain several GPS services. In reality, you’d want to be more selective about which provider you use based on power consumption, accuracy, and the availability of additional services. For now, the emulator provides a sample path through San Francisco. Custom GPS files can be uploaded for location based testing, consult the documentation here for more information. While more complicated and nuanced methods exist for testing and emulating location-based services, using the default path through San Francisco will work just fine for the sake of this simple application.

With a location Manager and a location provider, it’s now possible to make the actual getCurrentLocation call. This returns a snapshot of the phone’s current position in the form of a Location object. With this in hand, you can get the latitude and longitude. Methods for retrieving such information are available in the documentation. Now, with the location of the virtual handset, you can get into the final piece of this sample application: creating and displaying a custom view.

Using Custom Views
In its most simplistic form, a View in Android must overload only one function onDraw(). Custom Views can be complicated 3D playback implementations or they can be ultra simplistic text rendering. Here’s what the CreateView function listed above looks like:

public void CreateView(){	customView = new CustomView(this);}

This function simply evokes the constructor for the CustomView object. The definition of which follows below:

public class CustomView extends View{	LocateMe overlord;   	   	public CustomView(LocateMe pCtx)   	{   		super(pCtx);   		overlord = pCtx;   	}   	public void onDraw(Canvas cvs)   	{   		Paint p = new Paint();   		String sLat = "Latitude: " + overlord.getLat();   		String sLon = "Longitude: " + overlord.getLon();   		cvs.drawText(sLat , 32, 32, p);   		cvs.drawText(sLon, 32, 44, p);   	}   	}

This custom Android view renders the latitude and longitude test data to the screen in text. It requires a pointer to LocateMe, the Activity extender that is the heart of this application. Its two methods are the constructor and the onDraw methods. The constructor evokes the super-class constructor and stashes off the Activity pointer. onDraw will create a new Paint object (an object that encapsulates color, transparency, and other basic theme information) which will access the default color theme. From there, it’s a matter of setting up the display strings, and drawing them to the screen using the Canvas pointer. For those of you who know something about J2ME’s game canvas, this should look very familiar.

The Waiting Game
From purely a development standpoint, Android is a very powerful SDK. It combines XML layout schemes with custom view rendering and in house scrollbar, maps, and other in house widgets. All which can be overloaded and customized on the whim of a developer. Its documentation is rough; there are missing features and gaps in functionality (like SMS interop and a clear monetization plan), but overall Android looks very promising for what even Google acknowledges is a “First Look” SDK. Now we’ll just have to see if anyone actually deploys a phone that uses it.

devx-admin

devx-admin

Share the Post:
Battery Breakthrough

Electric Vehicle Battery Breakthrough

The prices of lithium-ion batteries have seen a considerable reduction, with the cost per kilowatt-hour dipping under $100 for the first occasion in two years,

Economy Act Soars

Virginia’s Clean Economy Act Soars Ahead

Virginia has made significant strides towards achieving its short-term carbon-free objectives as outlined in the Clean Economy Act of 2020. Currently, about 44,000 megawatts (MW)

Renewable Storage Innovation

Innovative Energy Storage Solutions

The Department of Energy recently revealed a significant investment of $325 million in advanced battery technologies to store excess renewable energy produced by solar and

Development Project

Thrilling East Windsor Mixed-Use Development

Real estate developer James Cormier, in collaboration with a partnership, has purchased 137 acres of land in Connecticut for $1.15 million with the intention of

USA Companies

Top Software Development Companies in USA

Navigating the tech landscape to find the right partner is crucial yet challenging. This article offers a comparative glimpse into the top software development companies

Battery Breakthrough

Electric Vehicle Battery Breakthrough

The prices of lithium-ion batteries have seen a considerable reduction, with the cost per kilowatt-hour dipping under $100 for the first occasion in two years, as reported by energy analytics

Economy Act Soars

Virginia’s Clean Economy Act Soars Ahead

Virginia has made significant strides towards achieving its short-term carbon-free objectives as outlined in the Clean Economy Act of 2020. Currently, about 44,000 megawatts (MW) of wind, solar, and energy

Renewable Storage Innovation

Innovative Energy Storage Solutions

The Department of Energy recently revealed a significant investment of $325 million in advanced battery technologies to store excess renewable energy produced by solar and wind sources. This funding will

Renesas Tech Revolution

Revolutionizing India’s Tech Sector with Renesas

Tushar Sharma, a semiconductor engineer at Renesas Electronics, met with Indian Prime Minister Narendra Modi to discuss the company’s support for India’s “Make in India” initiative. This initiative focuses on

Development Project

Thrilling East Windsor Mixed-Use Development

Real estate developer James Cormier, in collaboration with a partnership, has purchased 137 acres of land in Connecticut for $1.15 million with the intention of constructing residential and commercial buildings.

USA Companies

Top Software Development Companies in USA

Navigating the tech landscape to find the right partner is crucial yet challenging. This article offers a comparative glimpse into the top software development companies in the USA. Through a

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in and explore the leaders in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India, and kickstart your journey to

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner for your online project. Your

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the state. A Senate committee meeting

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor supply chain and enhance its

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with minimal coding. These platforms not

Cybersecurity Strategy

Five Powerful Strategies to Bolster Your Cybersecurity

In today’s increasingly digital landscape, businesses of all sizes must prioritize cyber security measures to defend against potential dangers. Cyber security professionals suggest five simple technological strategies to help companies

Global Layoffs

Tech Layoffs Are Getting Worse Globally

Since the start of 2023, the global technology sector has experienced a significant rise in layoffs, with over 236,000 workers being let go by 1,019 tech firms, as per data

Huawei Electric Dazzle

Huawei Dazzles with Electric Vehicles and Wireless Earbuds

During a prominent unveiling event, Huawei, the Chinese telecommunications powerhouse, kept quiet about its enigmatic new 5G phone and alleged cutting-edge chip development. Instead, Huawei astounded the audience by presenting

Cybersecurity Banking Revolution

Digital Banking Needs Cybersecurity

The banking, financial, and insurance (BFSI) sectors are pioneers in digital transformation, using web applications and application programming interfaces (APIs) to provide seamless services to customers around the world. Rising

FinTech Leadership

Terry Clune’s Fintech Empire

Over the past 30 years, Terry Clune has built a remarkable business empire, with CluneTech at the helm. The CEO and Founder has successfully created eight fintech firms, attracting renowned

The Role Of AI Within A Web Design Agency?

In the digital age, the role of Artificial Intelligence (AI) in web design is rapidly evolving, transitioning from a futuristic concept to practical tools used in design, coding, content writing

Generative AI Revolution

Is Generative AI the Next Internet?

The increasing demand for Generative AI models has led to a surge in its adoption across diverse sectors, with healthcare, automotive, and financial services being among the top beneficiaries. These

Microsoft Laptop

The New Surface Laptop Studio 2 Is Nuts

The Surface Laptop Studio 2 is a dynamic and robust all-in-one laptop designed for creators and professionals alike. It features a 14.4″ touchscreen and a cutting-edge design that is over

5G Innovations

GPU-Accelerated 5G in Japan

NTT DOCOMO, a global telecommunications giant, is set to break new ground in the industry as it prepares to launch a GPU-accelerated 5G network in Japan. This innovative approach will

AI Ethics

AI Journalism: Balancing Integrity and Innovation

An op-ed, produced using Microsoft’s Bing Chat AI software, recently appeared in the St. Louis Post-Dispatch, discussing the potential concerns surrounding the employment of artificial intelligence (AI) in journalism. These

Savings Extravaganza

Big Deal Days Extravaganza

The highly awaited Big Deal Days event for October 2023 is nearly here, scheduled for the 10th and 11th. Similar to the previous year, this autumn sale has already created

Cisco Splunk Deal

Cisco Splunk Deal Sparks Tech Acquisition Frenzy

Cisco’s recent massive purchase of Splunk, an AI-powered cybersecurity firm, for $28 billion signals a potential boost in tech deals after a year of subdued mergers and acquisitions in the