Saturday, February 9, 2013

Optimizing web applications for Android

We start this discussion by considering how to code web applications so they’re viewable
both by desktop clients and by mobile platforms such as Android. Developing
web applications for Android can be viewed as a continuum of options. On one end is
a site created for universal access, meaning that it’ll be visited by both desktop and
mobile users. On the other end of the spectrum is a website designed specifically for
mobile users. Between these two extremes are a couple of techniques for improving
the user experience. We’ll use the term mobile in mind—this refers to a website that’s
not exclusively written for mobile users but expects them as part of the regular visitor
list. Let’s begin with a discussion of designing your site with mobile in mind.

Introducing WebKit for Android Web Development

The WebKit browser engine stems from an open source project that can be traced
back to the K Desktop Environment (KDE). WebKit made its significant mobile debut
when the iPhone was released, and since then WebKit has been adopted by Android
and other mobile platforms.
Prior to the adoption of WebKit, early mobile web solutions ranged from laughable,
to mediocre, to tolerable, though always limited. These early mobile web offerings
were often so constrained that they required content providers to generate a
mobile-specific stream in addition to the normal desktop version of their material. In
some cases, a server-side component would perform on-the-fly distillation of the
HTML into a format more readily digested by the mobile browser. Regardless of the
implementation, any requirement for content providers to generate multiple copies
of their material severely constrained the volume of content the early mobile devices
could readily consume. The early mobile web was virtually nonexistent because browsers
were not capable of rendering full pages and sites made for mobile were rare.

Android web development

Mobile software development has usually required working with a proprietary toolset
to build applications. The term SDK is a familiar one—the majority of this Article
Android software development using the Android SDK. But the Android SDK
isn’t the only way to build and deliver applications for the Android platform. With
the emergence of accessible and powerful mobile browser capabilities, it’s now possible
to develop applications for the Android platform with web technologies such as HTML, CSS, and JavaScript. Opening up the world of mobile development to web technologies introduces a wide array of possibilities for developers and Android users alike. Mobile devices

Implementing the Native Function On C/C++

Now that the makefiles are defined, we need to complete the C implementation by creating fibonacci.c and calling the newly implemented function from the glue layer. Because the function implemented in fibonacci.c needs to be declared before it can be called, a new header file is also created. You will also need to add fibonacci.c to the list of files to compile in
 Implementation of the New Function in fibonacci.c

#include “fibonacci.h” 
uint64_t recursive (unsigned int n) {
     if (n > 1) return recursive(n-2) + recursive(n-1);  
 return n;
 Calling the Function From the Glue Layer
#include “com_apress_proandroid_Fibonacci.h”
#include “fibonacci.h” 
/*  * Class:       com_apress_proandroid_Fibonacci 
* Method:    recursiveNative 
* Signature: (I)J  */
(JNIEnv *env, jclass clazz, jint n) {    
return recursive(n);
Header File fibonacci.h
#ifndef _FIBONACCI_H_
#define _FIBONACCI_H_ 
#include <stdint.h> 
extern uint64_t recursive (unsigned int n); 

NOTE:  "Make sure you use the right types in your C/C++ code as jlong is 64-bit. Use well- defined types such as uint64_t or int32_t when size matters. "

Declaring the Native Method On java

 Declaration of the Native Method in

public class Fibonacci {
    public static native long recursiveNative (int n); // note the ‘native’ keyword

The native method is simply declared with the native keyword, and no implementation is provided in Java. The method shown above is public, but native methods can be public, protected, private, or package-private, just like any other Java method. Similarly, native methods don’t have to be static methods, and don’t have to use primitive types only. From the caller’s point of view, a native method is just like any other method. Once it is declared, you can start adding calls to this method in your Java code, and everything will compile just fine. However, if your application runs and calls Fibonacci.recursiveNative, it will crash with an UnsatisfiedLinkError exception. This is expected because you really haven’t done much so far other than declare a function, and the actual implementation of the function does not exist yet.
Once your native method is declared, you can start writing the JNI glue layer. 

Mixing Java and C/C++ Code On Android Development

Calling a C/C++ function from Java is actually quite easy but requires several steps:
The native method must be declared in your Java code.
The Java Native Interface (JNI) glue layer needs to be implemented.
Android makefiles have to be created.
The native method must be implemented in C/C++.
The native library must be compiled.
The native library must be loaded.
It really is easy in its own twisted way. We will go through each one of these steps, and by the end of this section, you will know the basics of mixing Java and C/C++. We will discuss the more intricate details of the Android makefiles, which allow you to optimize your code even more, in later sections. Since the Android NDK exists for Linux, MacOS X, and Windows (with Cygwin, or without when using NDK revision 7), the specific steps may vary slightly although the overall operations will remain the same. The following steps assume an Android project is already created and you now want to add native code to it.

Tuesday, February 5, 2013

Create Android Hello World Project & AVD

Find The Prime Numbers on Java programming language

When the loop control variable will not be needed elsewhere, most Java programmers declare it inside the for. For example, here is a simple program that tests for prime numbers. Notice that the loop control variable, i, is declared inside the for since it is not needed elsewhere.

// Test for primes.
class FindPrime { 
public static void main(String args[]) { 
  int num;  
  boolean isPrime;

    num = 14;

    if(num < 2)
              isPrime = false; 

             isPrime = true;

    for(int i=2; i <= num/i; i++) { 
    if((num % i) == 0) {

        isPrime = false;



               System.out.println("Not Prime");

Monday, February 4, 2013

A simple knowledge about android game development

I began developing on Android in early 2008 on the beta platform. At the time, no phones were announced for the new operating system and we developers genuinely felt as though we were at the beginning of something exciting. Android captured all of the energy and excitement of the early days of open source development. Developing for the platform was very reminiscent of sitting around an empty student lounge at 2:00 a.m. with a Jolt cola waiting for VAX time to run our latest code. It was an exciting platform to see materialize, and I am glad I was there to see it.

As Android began to grow and Google released more updates to solidify the final architecture, one thing became apparent: Android, being based on Java and including many well known Java packages, would be an easy transition for the casual game developer. Most of the knowledge that a Java developer already had could be recycled on this new platform. The very large base of Java game developers could use that knowledge to move fairly smoothly onto the Android platform.

Wednesday, January 23, 2013

What is MXML ?

MXML is an XML tag-based markup language, used in the layout and design of components and

data assets for Flex-based user interfaces. As an XML format, MXML is also structured, so there

are several rules you have to follow when you write it, and the contents must be well formed and

valid XML to preserve the integrity of the document.

Every MXML fi le is a class, but instead of having a .as fi le extension, it has a .mxml fi le extension.

And instead of simply having AS3 code and syntax, it can contain both the MXML markup and AS3.

A short idea about flex freamwork

Flex is a framework that leverages an underlying library of AS3 classes to provide

UI components that allow developers to build rich media applications and

compile to the SWF format. Adobe Flex builds on top of the core runtime APIs

provided by Flash Player and Adobe AIR.

Flex is available through the Flash Builder IDE, a tool that you will take a look

at in the next chapter. Flex is also available through a software development kit

(SDK) allowing SWF format content to be created through command line tools.

About Flash Player 10.x

Flash Player 10.1 was the fi rst release of Flash Player aimed at supporting the development of

content and SWF format deployment to both traditional web browser and mobile devices.

At the time of writing, a beta for Adobe Flash Player 11 was underway, allowing developers to

preview new and enhanced features targeting the next release of the run time. With the potential for

new APIs to be dropped, none of the features could be covered in this book, but look for an

update. For more information, visit the Adobe Labs website ( and

search for Adobe Flash Player 11.

This book centers on the development of Flash content targeting the latest release, Adobe Flash

Player 10.3 using AS3.

Flash is fully supported on Google Android and the BlackBerry Tablet OS mobile platforms. Unless

you’ve been hiding under a rock for the past few years, you’ll know Flash isn’t supported on the

Apple iOS platform. However, using AS3 and AIR, you can target your applications to run on the

platform via standalone applications.

Types of Mobile Applications

It is important to understand the different types of mobile applications. I will put them in

two categories, according to what they do.

1. Standalone mobile applications

2. Mobile applications (based on web services)

Standalone mobile applications are applications such as alarms, phone dialers, and

offline games. Web service-backed mobile applications are applications like e-mails,

calendars, Twitter clients, online games, and applications that interact with web


This distinction between mobile applications is unique to the context of this book.

Although PhoneGap can be used to implement standalone mobile applications, the

nature of PhoneGap-based mobile applications typically falls into the category of

“service-backed mobile applications.”

Understanding Cross-Platform Mobile Application Development

This book is about mobile application development; more specifically, about easing the

pain of mobile application development. There are many smartphone platforms on the

market: Android, iPhone, BlackBerry, Nokia, the Windows 7 Phone, and WebOS. Newer

platforms are on the rise as well, such as Samsung’s Bada and Meego.

The sheer number of development platforms for mobile applications may seem

overwhelming. This is the first of many points you must keep in mind when dealing with

mobile application development.

Thursday, January 17, 2013

Optimizing Fibonacci

The first optimization we are going to perform eliminates a method call, as

shown in Listing 1–3. As this implementation is recursive, removing a single

call in the method dramatically reduces the total number of calls. For example,

computeRecursively(30) generated 2,692,537 calls while

computeRecursivelyWithLoop(30) generated “only” 1,346,269. However, the

performance of this method is still not acceptable considering the responsetime

criteria defined above, 100 milliseconds or less, as

computeRecursivelyWithLoop(30) takes about 270 milliseconds to complete.

Listing 1–3. Optimized Recursive Implementation of Fibonacci Series

public class Fibonacci {

public static long computeRecursivelyWithLoop (int n)


if (n > 1) {

long result = 1;

do {

result += computeRecursivelyWithLoop(n-2);


} while (n > 1);

return result;


return n;