Saturday, April 13, 2013

Memory Management in Android

What is Memory?
What is Management ?
What is Memory Management in Android ?
Why we take care of memory management related problems in Android ?
When we take care of memory management in Android ?
What is heap size and how to manage heap size ?
What is memory leak ?
What is MAT (Eclipse Memory Analyzer tool)
What is GC (Garbage collection) and use of gc ?

Android Memory

Android is a Linux based OS with 2.6.x kernel, stripped down to handle most tasks pretty well. It uses native open source C libraries that have powered Linux machines for years. All the basic OS operations like I/O, memory management, and so on, are handled by the native stripped-down Linux kernel

Friday, April 5, 2013

How to use JSON in Android ?

JSON - JavaScript Object Notification.

JSON (JavaScript Object Notation) is an independent data exchange format. JSON is limited to text and numeric values. Binary values are not supported.

JSON is a subset of the JavaScript Specification (ECME-Script) and it is therefore directly supported in JavaScript.

Data structures in JSON are based on key / value pairs. The key is a string, the value can be a numerical value, a Boolean value (true or false) or an object.

Thursday, April 4, 2013

What is REST ?

Web services is a generic name for web-based RPC. This includes SOAP-based web services, which normally go by the proper name "Web Services"; but now, REST is also considered a web-service architecture.

REST is a non-XML-based, web-based RPC; but it is also a design philosophy and architecture.

REST stands for Representational State Transfer. (It is sometimes spelled "ReST".) It relies on a stateless, client-server, cacheable communications protocol -- and in virtually all cases, the HTTP protocol is used.

REST is a lightweight alternative to Web Services and RPC.

Wednesday, April 3, 2013

What is ANR issue in Android ?

If you getting ANR issue in Apps, try to understand what does it means ?

01-12 18:00:57.048 E/ActivityManager( 1709): ANR in com.kt.twwidget
01-12 18:01:18.700 E/ActivityManager( 1709): ANR in  com.kt.twwidget
01-12 18:01:40.800 E/ActivityManager( 1709): ANR in system
01-12 18:02:59.280 E/ActivityManager( 1709): ANR in  com.kt.twwidget
01-12 18:03:21.010 E/ActivityManager( 1709): ANR in  com.kt.twwidget

This issue is well-known in the Android community. Using part of its terminology, this issue is called an ANR or Application Not Responding. Thus, the application “system” appears to stop working or there is something that makes it to wait for a huge period of time,that huge, that an ANR is fired by the system. Because this happens at boot time, the entire system seems to freeze and it is not until the user kills this application that the terminal finishes its booting procedure and makes itself available to the user once again. From time to time, though not always, this odd behaviour can end up in an unexpected reboot.

An ANR happens when some long operation takes place in the "main" thread. This is the event loop thread, and if it is busy, Android cannot process any further GUI events in the application, and thus throws up an ANR dialog.

Many way of ANR issue throwing - 

1. Longer operation of method
2. Blocking the thread permanently


Detecting from where ANRs issues happening is easy.
1. if it is a permanent block (deadlock situation is coming from CODE) , but harder if it's just a temporary delay. 
2. First, go over your code and look for vunerable spots and long running operations.
For examples may include using sockets, locks, thread sleeps, and other blocking operations from within the event thread. 
You should make sure these all happen in separate threads. If nothing seems the problem, use DDMS and enable the thread view. This shows all the threads in your application similar to the trace you have. Reproduce the ANR, and refresh the main thread at the same time. That should show you precisely whats going on at the time of the ANR

The stack trace shows that the main thread is in the Looper (the message loop implementation) and doing a timed wait through Object.wait. This means the message loops does not currently have any messages to dispatch, and is waiting for new messages to come in. An ANR happens when the system realizes a message loop is spending to much time processing a message, and not processing other messages in the queue. If the loops is waiting for messages, obviously this is not happening.

Approch - 

You can enable StrictMode in API level 9 and above - 

StrictMode is most commonly used to catch accidental disk or network access on the application's main thread, where UI operations are received and animations take place. By keeping your application's main thread responsive, you also prevent ANR dialogs from being shown to users.
public void onCreate() {
    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
using penaltyLog() you can watch the output of adb logcat while you use your application to see the violations as they happen.

What is a Hybrid App?

Hybrid is anything derived from heterogeneous sources, combination of different sources, in different manner, or composed of elements of different or incongruous kinds.

A hybrid app is one that is written with the same technology used for websites and mobile web implementations, and that is hosted or runs inside a native container on a mobile device.

Hybrid apps uses a web view control for corresponding OS (UIWebView on iOS, WebView on Android and others) to present the HTML and JavaScript files in a full-screen format, using the native browser rendering engine (not the browser itself).

WebKit is the browser rendering engine that is used on iOS, Android, Blackberry, i.e. HTML and JavaScript used to design & develop hybrid app, will rendered/processed by the WebKit rendering engine and displayed to the user in a full-screen web view control, not in a browser. 

 Apache Cordova (formerly PhoneGap) is an example of a JavaScript abstraction layer over native APIs. Through this abstraction layer (between native & hybrid) a common set of APIs is exposed in JavaScript, and these JavaScript APIs work on any device supported by the framework (Cordova that is seven mobile platforms including iOS, Android, Blackberry and Windows Phone 7). 

When the native wrapper is compiled around the HTML, CSS and JavaScript resources, there is an interop layer added that connects the JavaScript APIs with the platform specific APIs.

For Example - 

Working with Camera, when we want to take a image through camera, how  it is working in different platform (iOS or Android) -

Under the covers the JavaScript is making an interop call that access the native API for the camera. 

That means that on an iOS device this JavaScript is calling into the native layer to instantiate a  UIImagePickerController and on Android it creates an Intent to use MediaStore.ACTION_IMAGE_CAPTURE to take a picture.

As a developer, no worry about all above stuff (internal calling or internal working process), you just need to call the JavaScript function ( in this case), and respond to the outcome (the imageData passed to the onCameraSuccess call back function in this case).

So, what we conclude - 

Hybrid apps are a great option for you if you:
  1. Want to target multiple mobile platforms
  2. Want to take advantage of device capabilities like geolocation, accelerometer or the camera
  3. Want the app to be useable when the device is offline
  4. Don’t need the advanced graphics performance that you can only get from a native app
Hybrid apps are built with web technologies which means there are millions of web developers who already have the base skill set to build mobile apps.

What is Hybrid mobile apps, Native apps and Mobile apps ?

For a developer, before moving to any particular language, or area, s/he should think about all pros & cons.
When we are talking to Mobility, things is coming into mind is –

1.      Android
2.      iOS
3.      BlackBerry
4.      Windows

But, if we are talking about Android, then the area comes into picture is –

1.      Native apps development
2.      Hybrid apps development
3.      Mobile web apps development

Native apps are built for a specific platform with the platform SDK, tools and languages, typically provided by the platform vendor (e.g. xCode/Objective-C for iOS, Eclipse/Java for Android, Visual Studio/C# for Windows Phone).

- Always provide the fasted performance
- Cost will little bit high, if it is single platform development
- Always good for games

Mobile Web apps are server-side apps, built with any server-side technology (PHP, Node.js, ASP.NET) that render HTML that has been styled so that it renders well on a device form factor.

Hybrid apps, like native apps, run on the device, and are written with web technologies (HTML5, CSS and JavaScript). Hybrid apps run inside a native container, and leverage the device’s browser engine (but not the browser) to render the HTML and process the JavaScript locally. A web-to-native abstraction layer enables access to device capabilities that are not accessible in Mobile Web applications, such as the accelerometer, camera and local storage.

Hybrid app will be easier to build,
- Used HTML5 , CSS, JavaScript, JQuery mobile
- Cost will less if developed for multiple platforms
- For game it is not good

What should I Choose Native, Hybrid or Mobile web application development?
Answer will be depends L, so before committing down a single path consider the user experience and what each option provides you.

Depends upon –

1.      Depends on budget
2.      Depends on apps type(game or application)
3.      Depends on scarify (performance, look & feel)
4.      Time
5.      And lots of thing, on which developer can choose the development tools or way

Like any other technology choice, deciding between native and hybrid requires you to look at the user experience and decide on the level of investment you need to make to achieve the goal.

They also need to be written/rewritten for each mobile platform you are targeting. Hybrid apps will always enable you to build for more platforms faster, if you are willing to sacrifice small amounts of performance (e.g. game-level responsiveness).