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 Android.mk.
 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  */
jlong JNICALL
 Java_com_apress_proandroid_Fibonacci_recursiveNative  
(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); 
#endif

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 Fibonacci.java

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:
1.
The native method must be declared in your Java code.
2.
The Java Native Interface (JNI) glue layer needs to be implemented.
3.
Android makefiles have to be created.
4.
The native method must be implemented in C/C++.
5.
The native library must be compiled.
6.
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; 

   else
             isPrime = true;

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

        isPrime = false;
        break;

        }
    }

    if(isPrime)

                System.out.println("Prime");
    else
               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.