Wednesday, June 03, 2009

SQLite Cursors and Content Values:

ContentValues objects are used to insert new rows into database tables (and Content Providers). Each Content Values object represents a single row, as a map of column names to values. 
Queries in Android are returned as Cursor objects. Rather than extracting and returning a copy of the result values, Cursors act as pointers to a subset of the underlying data. Cursors are a managed way of controlling your position (row) in the result set of a database query. 

The Cursor class includes several functions to navigate query results including, but not limited to, the following:

moveToFirst >> Moves the cursor to the fi rst row in the query result.

moveToNext >> Moves the cursor to the next row.

moveToPrevious >> Moves the cursor to the previous row.

getCount >> Returns the number of rows in the result set.

getColumnIndexOrThrow >> Returns an index for the column with the specifi ed name (throw-
ing an exception if no column exists with that name).

getColumnName >> Returns the name of the specifi ed column index.

getColumnNames >> Returns a String array of all the column names in the current cursor.

Monday, May 04, 2009

SQLite Database for Data Storage and Retrieval :

Rapid and efficient data storage and retrieval are essential for a device whose storage capacity is limited by its compact nature.

Android provides a lightweight relational database for each application using SQLite.

By default, each application database is sandboxed — its content is available only to the application that created it — but Content Providers supply a mechanism for the managed sharing of these application databases.

Native Google Maps, Geocoding, and Location-Based Services :

Native map support lets you create a range of map-based applications that leverage the mobility of Android devices. Android lets you create activities that include interactive Google Maps as part of your user interface with full access to maps that you can control programmatically and annotate using Android’s rich graphics library.

Android’s location-based services manage technologies like GPS and Google’s GSM cell-based location technology to determine the device’s current position. These services enforce an abstraction from spe-cifi c location-detecting technology and let you specify minimum requirements (e.g., accuracy or cost) rather than choosing a particular technology.

It also means that your location-based applications will work no matter what technology the host handset supports. To combine maps with locations, Android includes an API for forward and reverse geocoding that lets you fi nd map coordinates for an address, and the address of a map position.

Thursday, April 23, 2009

Access to Hardware including Camera, GPS, and Accelerometer :

Android includes API libraries to simplify development involving the device hardware. These ensure that you don’t need to create specifi c implementations of your software for different devices, so you can create Android applications that work as expected on any device that supports the Android software stack.

The Android SDK includes APIs for location-based hardware (such as GPS), camera, network connec-tions, Wi-Fi, Bluetooth, accelerometers, touch screen, and power management.

Native Android Applications :

Android phones will normally come with a suite of preinstalled applications including, but not limited to:

  • An e-mail client compatible with Gmail but not limited to it.
  • An SMS management application.
  • A full PIM (personal information management) suite including a calendar and contacts list, both tightly integrated with Google’s online services.
  • A fully featured mobile Google Maps application including StreetView, business finder, driving directions, satellite view, and traffi conditions.
  • A WebKit-based web browser.
  • An Instant Messaging Client.
  • A music player and picture viewer.
  • The Android Marketplace client for downloading thied-party Android applications.
  • The Amazon MP3 store client for purchasing DRM free music.

All the native applications are written in Java using the Android SDK and are run on Dalvik.

The data stored and used by the native applications — like contact details — are also available to third-party applications. Similarly, your applications can handle events such as an incoming call or a new SMS message.

The exact makeup of the applications available on new Android phones is likely to vary based on the hardware manufacturer and/or the phone carrier or distributor. 

Wednesday, April 01, 2009

More XML Layouts :

RelativeLayout :

Each child element is laid out in relation to other child elements. 
Relationships can be established so that children will start themselves where a previous child ends. Children can relate only to elements that are listed before them. So, build your dependency from the beginning of the XML file to the end.

AbsoluteLayout :

Each child must be given a specific location within the bounds of the parent layout object. The AbsoluteLayout object is probably the easiest to build and visualize but the hardest to migrate to a new device or screen size. 

TableLayout :

TableLayout is a layout object that allows you to specify table rows. 
Android tries to arrange each of the child elements into the correct row and columns. 





Sunday, March 01, 2009

Easy and Fast, the XML Layout :

XML layouts might seem simple at first, but it’s going to get complicated really quickly.

Laying Out :
Most XML screens will be wrapped in a layout object. Layout objects come in many different flavors. 

LinearLayout : 
All elements are arranged in a descending column from top to bottom or left to right.

Each element can have gravity and weight properties that denote how they dynamically

grow and shrink to fill space. Elements arrange themselves in a row or column notation

based on the android:orientation parameter. 


Wednesday, January 28, 2009

Creating an XML Layout File :

Now that you have an image resource, you can add it to your XML layout file.

These files are kept in res/layout/, and you should currently have one called main.xml.

Add a new XML file called splash.xml, and copy the contents of the main.xml file into it.

Next, modify the file by removing the tag and add an tag that

looks like the following: 

android:layout_width="fill_parent" 

android:layout_height="fill_parent"> 

 

Using Android’s XML layout objects is simple and straightforward.

As I mentioned, files in the /res directories can be referenced with the @ symbol

as shown earlier, for example, android:src="@drawable/menu_background".

Further, layout_width and layout_height dictate the size of the image view.

Look to make sure your new layout file has been added to R.java.

It should appear as follows:

public static final int splash=0x7f030001; 

Adding the Image Resource :

First you’ll need a sample splash screen image. The “socially awkward” splash screen I’ve included is not going to win any awards, but it is a good poke at the rash of social networking applications that seem to keep cropping up in the mobile space right now. 

To add this new resource, I’ve placed menu_background.jpg inside res/drawable.

Make sure a new ID is added to R.java.

It should look something like this: 

public static final int menu_background=0x7f020001; 

This is now your means of loading and drawing the image from within your code.

You’ll return to this concept in the next chapter on user interaction. 

Android Functionality :

Just like the midlet, an activity uses a series of functions to interact with the outside world.

At its base, your activity must override the method onCreate. Other functions you’ll want to override are onStop, onPause, onResume, and onKeyDown. These few functions are what will let you tie your activity into the Android handset at large. 

By default, new Android applications created within Eclipse will implement a “Hello, World” application. I’ll show you how to go from this basic application to a fully functional splash screen. 

Thursday, January 01, 2009

Android vs. Java ME vs. BREW :

A BREW application will, in the vast majority of all cases, consist of a single applet.

That applet communicates with the rest of the handset through receiving and sending events. You can think of a Java ME application, on the other hand, as an extension of the Midlet class.

The midlet has functions for starting, stopping, pausing, key handling, and performing any other interaction between the handset and application.

A Java ME application usually consists of a single midlet. 

Android applications can have any number of activities registered with the 
handset through the AndroidManifest.xml file. Android’s multiactivity 
architecture is probably the major difference between developing for
Android   and developing for other handset SDKs.

This single fact makes it much easier to write modular, compartmentalized code. In BREW and Java ME, a developer will implement most functionality within the confines of the midlet or the applet. In Android, you can write an activity, content handler, intent receiver, or service to handle nearly anything.

Once you’ve written an activity to edit a text file, you can refer to this activity in all future applications you write by sending and receiving intent actions.

This isn’t to say that such architecture isn’t possible within BREW or Java ME. 
It just has to be done at the Java, C, or C++ level or, in Brew, with cumbersome extensions instead of being built smoothly into the application 
framework. 

Monday, December 01, 2008

Android Applications: 

An application in Android is defined by the contents of its manifest.

Each Android application declares all its activities, entry points, communication layers, permissions, and intents through AndroidManifest.xml. Four basic building blocks, when combined, comprise a rich Android application: 

ƒ Activity: The most basic building block of an Android application.

ƒ Intent receiver: A reactive object launched to handle a specific task.

ƒ Service: A background process with no user interface.

ƒ Content provider: A basic superclass framework for handling and storing data.

Thursday, October 23, 2008

Installing the Android Eclipse Plug-In :

I like graphical user interfaces (GUIs), provided they have hotkeys like the 

Eclipse IDE, when working with Android. To get more than basic 
functionally out of the process, you’ll need to download the Android 
Developer Tools. To install it from within Eclipse, follow the directions outlined by Google.

http://code.google.com/android/intro/installing.html#installingplugin

If you’ve already installed the older version of the SDK and the Eclipse 

plug-in, I recommend you go back and update it to M5-RC15 (or the latest 
version) now using the previously mentioned links. Enough has changed 
between the older version and the latest that the changing details could be 
confusing. If you correctly follow the directions but get an error when 
trying to install the Android editors, go back and install the full version of 
Java EE Eclipse. The basic Java SDK doesn’t include all the correct packages used by the Android plug-in. 

Don’t forget to point the Android plug-in to where you unpacked your copy of the SDK. It’ll be in Windows/Preferences/Android on the Android tab. 

Create a new project by selecting File h New h Android Project. Give the 
project and activity a pithy name of your choosing. You’ll also have to 
insert into your source package name at least one dot (.), such as apress. 
book.sample or crazy.flyingmonkey.application. 

New Session Android JAVA : 

Getting the Android SDK :

You can find the Android SDK on Google’s website at 

http://code.google.com/android/download.html

Grab it, download it somewhere handy, and then unpack it.

I’ve put mine in my Development folder at /Developer/AndroidSDK. 

You can just as easily put the ZIP file anywhere on your filesystem.

Just remember where you’ve stashed it, because you’ll need to tell Eclipse where it is later.

It’s also a good idea, if you’re a Windows or Linux user, 

to add the location of the Android tools to your Path variable. 


Tuesday, September 30, 2008

An Overview of RMI Applications :

RMI applications often comprise two separate programs, a server and a client. A typical server program creates some remote objects, makes references to these objects accessible, and waits for clients to invoke methods on these objects. A typical client program obtains a remote reference to one or more remote objects on a server and then invokes methods on them. RMI provides the mechanism by which the server and the client communicate and pass information back and forth. Such an application is sometimes referred to as a distributed object application. 


Distributed object applications need to do the following: 

Locate remote objects. Applications can use various mechanisms to obtain references to remote objects. For example, an application can register its remote objects with RMI's simple naming facility, the RMI registry. Alternatively, an application can pass and return remote object references as part of other remote invocations. 
Communicate with remote objects. Details of communication between remote objects are handled by RMI. To the programmer, remote communication looks similar to regular Java method invocations. 
Load class definitions for objects that are passed around. Because RMI enables objects to be passed back and forth, it provides mechanisms for loading an object's class definitions as well as for transmitting an object's data.

Sunday, August 31, 2008

Hibernate Types  in JAVA:

Hibernate types fall into three broad categories: entities, components, and values.

1.) Entities:

Generally, an entity is a POJO class that has been mapped into the database using the or elements. An entity can also be a dynamic map (actually a Map of Maps). These are mapped against the database in the same way as a POJO, but with the default entity mode of the SessionFactory set to dynamic-map.

2.) Components:

Lying somewhere between entities and values are component types. When the class representation is simple and its instances have a strong one-to-one relationship with instances of another class, then it is a good candidate to become a component of that other class.

3.) Values:

Everything that is not an entity or a component is a value. Generally, these correspond to the data types supported by your database, the collection types, and, optionally, some userdefined types.

Tuesday, July 29, 2008

Preventing Thread Execution : 

A thread that's been stopped usually means a thread that's moved to the dead state.

Its your ability to recognize when a thread will get kicked out of running but not be sent back to either runnable or dead.

For the purpose of the understanding, we aren't concerned with a thread blocking on I/O(say, waiting for something to arrive from an input stream from the server).

We are concerned with the following :

1.)  Sleeping.

2.)  Waiting.

3.)  Blocked because it needs an object's lock.

Thread States in JAVA : 

A thread can be only in one of five states :

New :--> This is the state the thread is in after the Thread instance has been created, but the start() method has not been invoked on the thread. It is a live Thread object, but not yet a thread of execution. At this point, the thread is considered not alive.


Runnable :--> This is the state a thread is in when it's eligible to run, but the scheduler has not selected it to be the running thread. A thread first enters the runnable state when the start() method is invoked, but a thread can also return to the runnable state after either running or coming back from a blocked, waiting, or sleeping state. When the thread is in the runnable state, it is considered alive.

Running :--> This is it. The "big time." Where the action is. This is the state a thread is in when the thread scheduler selects it (from the runnable pool) to be the currently executing process. A thread can transition out of a running state for several reasons, including because "the thread scheduler felt like it." We'll look at those other reasons shortly. Note that in Figure 9-2, there are several ways to get to the runnable state, but only one way to get to the running state: the scheduler chooses a thread from the runnable pool.

Waiting/blocked/sleeping :--> This is the state a thread is in when it's eligible to run. Okay, so this is really three states combined into one, but they all have one thing in common: the thread is still alive, but is currently not eligible to run. In other words, it is not runnable, but it might return to a runnable state later if a particular event occurs. A thread may be blocked waiting for a resource (like I/O or an object's lock), in which case the event that sends it back to runnable is the availability of the resource—for example, if data comes in through the input stream the thread code is reading from, or if the object's lock suddenly becomes available. A thread may be sleeping because the thread's run code tells it to sleep for some period of time, in which case the event that sends it back to runnable is that it wakes up because its sleep time has expired. Or the thread may be waiting, because the thread's run code causes it to wait, in which case the event that sends it back to runnable is that another thread sends a notification that it may no longer be necessary for the thread to wait. The important point is that one thread does not tell another thread to block. Some methods may look like they tell another thread to block, but they don't.

If you have a reference t to another thread, you can write something like this:

t.sleep(); or t.yield()

But those are actually static methods of the Thread class—they don't affect the instance t; instead they are defined to always affect the thread that's currently executing. (This is a good example of why it's a bad idea to use an instance variable to access a static method—it's misleading. There is a method, suspend(), in the Thread class, that lets one thread tell another to suspend, but the suspend() method has been, deprecated and won't be on the exam (nor will its counterpart resume()). There is also a stop () method, but it too has been deprecated and we won't even go there. Both suspend () and stop() turned out to be very dangerous, so you shouldn't use them and again, because they're deprecated, they won't appear on the exam. Don't study 'em, don't use 'em. Note also that a thread in a blocked state is still considered to be alive. 

Dead :--> A thread is considered dead when its run() method completes. It may still be a viable Thread object, but it is no longer a separate thread of execution. Once a thread is dead, it can never be brought back to life! (The whole "I see dead threads" thing.) If you invoke start() on a dead Thread instance, you'll get a runtime (not compiler) exception. And it probably doesn't take a rocket scientist to tell you that if a thread is dead, it is no longer considered to be alive.

Friday, June 27, 2008

Running an Application from the Java Cache Viewer :

If you are using Java Version 1.6.0, you can run a Java Web Start application through the Java Cache Viewer. 

When Java Web Start first loads an application, information from the application's JNLP file is stored in the local Java Cache Viewer. To launch the application again, you do not need to return to the Web page where you first launched it; you can simply open the Java Cache Viewer. 

To open the Java Cache Viewer: 
Open the Control Panel.
Double click on the Java icon. The Java Control Panel opens.
Select the General tab.
Click View. The Java Cache Viewer opens.

Running a Java Web Start Application from a Browser :

You run an application with Java Web Start from a browser simply by clicking a link to the application's JNLP file, such as: 

Launch Notepad Application

Java Web Start then loads and runs the application based on instructions in the JNLP file.