Thursday, November 7, 2013

Google Maps Android API v2 : mapFragment.getMap() returns null


If mapFragment.getMap() returns null then map is not verified from the GooglePlayServices.

Google Play Services
Give apps more features to attract users on a wider range of devices. With Google Play services, our app can take advantage of the latest, Google-powered features such as Maps, Google+, and more, with automatic platform updates distributed as an APK through the Google Play store.

This type of exception occurs when there are no proper GooglePlayServices updates.
We can check using GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) method, Which returns status code indicating whether there was an error or not.

public static int isGooglePlayServicesAvailable (Context context)
Verifies that Google Play services is installed and enabled on this device, and that the version installed on this device is no older than the one required by this client.
Returns
status code indicating whether there was an error. Can be one of following in ConnectionResult: SUCCESS, SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED, SERVICE_DISABLED, SERVICE_INVALID, DATE_INVALID.

Map the output of GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) return value with ConnectionResult class Constants. We can find the response of GooglePlayServices.

Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment = (SupportMapFragment) fragment;
map = mapFragment.getMap();

if (map != null) {
// The Map is verified. It is now safe to manipulate the map.
}else {
int checkGooglePlayServices = GooglePlayServicesUtil
              .isGooglePlayServicesAvailable(this);
       if (checkGooglePlayServices != ConnectionResult.SUCCESS) {
              // google play services is missing!!!!
              /*
               * Returns status code indicating whether there was an error.
               * Can be one of following in ConnectionResult: SUCCESS,
               * SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED,
               * SERVICE_DISABLED, SERVICE_INVALID.
               */
              GooglePlayServicesUtil.getErrorDialog(checkGooglePlayServices,
                     this, REQUEST_CODE_RECOVER_PLAY_SERVICES).show();
       }
}

Below are the error codes used by GooglePlayServicesClient.OnConnectionFailedListener

Constants
int
The device date is likely set incorrectly.
int
The application is misconfigured.
int
The Drive API requires external storage (such as an SD card), but no external storage is mounted.
int
An internal error occurred.
int
The client attempted to connect to the service with an invalid account name specified.
int
The application is not licensed to the user.
int
A network error occurred.
int
Completing the connection requires some form of resolution.
int
The installed version of Google Play services has been disabled on this device.
int
The version of the Google Play services installed on this device is not authentic.
int
Google Play services is missing on this device.
int
The installed version of Google Play services is out of date.
int
The client attempted to connect to the service but the user is not signed in.
int
The connection was successful.


Note: Do Google Play Services Verification when we are using services of Google.

Origin References:

Tuesday, November 5, 2013

Android Comparision Chart : AsyncTask Vs Service




AsyncTask
Service
Purpose ?
It enables proper and easy use of the UI thread. This class allows performing background operations and publishing results on the UI thread without having to manipulate threads and/or handlers. It should ideally be used for short operations (a few seconds at the most.)
A service can run in the background to perform work even while the user is in a different application
It is an application component?
No
Yes
How to invoke?
The task instance must be created on the UI thread.
execute(Params...) must be invoked on the UI thread.
The task can be executed only once (an exception will be thrown if a second execution is attempted.)
Services can be started with Context.startService() and Context.bindService().
How to stop?
A task can be cancelled at any time by invoking cancel(boolean)
After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns
stopSelf() or stopService(), the system destroys the service as soon as possible.
Is a thread?
Yes
No
Runs on UI Thread?
Yes
Yes
Whether other application can access?
No
Yes, Global access to a service can be enforced when it is declared in its manifest's tag.
Whether  it reduce the risk of ANR
No
Yes
How task execution happens?
The task can be executed only once (an exception will be thrown if a second execution is attempted.)
If this service is not already running, it will be instantiated and started (creating a process for it if needed); if it is running then it remains running.


AsyncTask

Class Overview


AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent pacakge such as Executor, ThreadPoolExecutor and FutureTask.
An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate andonPostExecute.

Developer Guides

For more information about using tasks and threads, read the Processes and Threads developer guide.

Usage


AsyncTask must be subclassed to be used. The subclass will override at least one method (doInBackground(Params...)), and most often will override a second one (onPostExecute(Result).)

Service

Class Overview

A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. Each service class must have a corresponding  declaration in its package'sAndroidManifest.xml. Services can be started with Context.startService() and Context.bindService().
Note that services, like other application objects, run in the main thread of their hosting process. This means that, if your service is going to do any CPU intensive (such as MP3 playback) or blocking (such as networking) operations, it should spawn its own thread in which to do that work. More information on this can be found in Processes and Threads. The IntentService class is available as a standard implementation of Service that has its own thread where it schedules its work to be done.

Developer Guides

For a detailed discussion about how to create services, read the Services developer guide.

What is a Service?

Most confusion about the Service class actually revolves around what it is not:
·         A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.
·         A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).
Thus a Service itself is actually very simple, providing two main features:
·         A facility for the application to tell the system about something it wants to be doing in the background (even when the user is not directly interacting with the application). This corresponds to calls to Context.startService(), which ask the system to schedule work for the service, to be run until the service or someone else explicitly stop it.
·         A facility for an application to expose some of its functionality to other applications. This corresponds to calls to Context.bindService(), which allows a long-standing connection to be made to the service in order to interact with it.
When a Service component is actually created, for either of these reasons, all that the system actually does is instantiate the component and call itsonCreate() and any other appropriate callbacks on the main thread. It is up to the Service to implement these with the appropriate behavior, such as creating a secondary thread in which it does its work.
Note that because Service itself is so simple, you can make your interaction with it as simple or complicated as you want: from treating it as a local Java object that you make direct method calls on (as illustrated by Local Service Sample), to providing a full remoteable interface using AIDL.


Origin: http://developer.android.com