Monday, 29 December 2014

What are the Exceptions occurs while using HTTP services ?

Dealing with Exceptions is part of any program,bur software that makes use of external services
(such as HTTP services) must pay additional attention to exceptions because the potential for
errors is magnified.

There are several types of exceptions that you can expect while making use of HTTP services.
- Transport Exceptions
- Protocol Exceptions
- Timeouts.

You should understand when these exceptions could occur.

* Transport Exceptions can occur due to no. of reasons,but the most likely scenario with a mobile
device is poor network connectivity.

* Protocol Exceptions at the HTTP protocol layer.These include authentication errors,invalid cookies, and so on.

* Timeouts,with respect to HTTP calls,come in two flavors :
- Connection Timeouts.
- Socket Timeouts.

* A Connection Timeout can occur if the HttpClient is not able to connect to the HTTP server.

* A Socket Timeout can occur if the HttpClient fails to recieve a response within a defined time period.

Sunday, 28 December 2014

What about SOAP Services ?

There are lots of SOAP-based web services on the internet,but to date Google has not provided direct
support in Android for calling SOAP web services.

Google instead prefers REST-like web services,seemingly to reduce the amount  of computing required on the client device.

However,the tradeoff is that the developer must do more work to send data and to parse the returned

Some developers have used the kSOAP2 developer kit to build SOAP clients for Android.

One approach that's been used successfully is to implement your own services on the internet,which can talk SOAP ( or whatever ) to the destination service.Then your Android application only needs
to talk to your services,and you have complete control .

If the destination services change,you might be able to handle that without having to update and
release a new version of your application.You'd only have to update the services on your server.

A side benefit of this approach is that you could more easily implement a paid subscription model
for your application .If a user lets their subscription lapse , you can turn them off at your server.

Friday, 26 December 2014

How to use the HttpClient for HTTP POST Requests ?

Making an HTTP POST call is very similar to making an HTTP GET call.

HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost("your URL");
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("first","param value one"));
postParameters.add(new BasicNameValuePair("username","dave"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
HttpResponse response = client.execute(request);

- We create an HttpClient and then instatntiate the HttpPost with the URL of the HTTP endpoint.
- Next we create a list of NameValuePair objects and populated it with several name/value parameters.
- We then create a UrlEncodedFormEntity instance,passing the list of NameValuePair objects to its
- Finally,we called the setEntity() method of the POST request and then executed the request using the HttpClient instance.

* HTTP POST supports another request-body format known as a multipart POST.With this type of
POST ,you can send name / value parameters as before,along with arbitrary files.

* To do multipart POST calls,you need to get three additional Apache open source projects :
i)- Apache Commons IO.
ii)- Mime4j
iii)- HttpMime.


Wednesday, 24 December 2014

How to use the HttpClient for HTTP GET Requests ?

Here's one of the general patterns for using the HttpClient  :

1- Create an HttpClient (or get an existing reference).

2- Instantiate a new HTTP method , such as PostMethod or GetMethod.

3- Set HTTP parameter names/values.

4- Execute the HTTP call using the HttpClient.

5- Process the HTTP response.

* You will need to add android.permission.INTERNET to your manifest file when making HTTP
calls using the HttpClient.

Tuesday, 23 December 2014

How to build and consume Services in Android ?

The Android Platform provides a complete software stack. This means you get an operating system
and middleware,as well as working applications (such as a phone dialer).

Alongside all of this,you have an SDK that you can use to write applications for the platform.

Thus far,we've seen that we can build applications that directly interact with the user through
a user interface.We have not , however , discussed background services or the possibilities of
building components that run in the background.

It includes HTTP services,background tasks and interprocess communication.

What are the Steps to Implement Search View on ActionBar ?

A Search View widget is a search box that fits between your tabs and the menu icons in the
action bar.

You need to do the following to use search in your action bar :

1- Define a menu item pointing to a search view provided by the SDK.You also need an activity into
which you can load this menu.This is often called the search invoker activity.

2- Create another activity that can take the query from the search view in step 1 and provide results.
This is often called the search results activity.

3- Create an XML file that allows you to customize the search view widget.This file is often called
searchable.xml and resides in the res / xml subdirectory.

4- Declare the search results activity in the manifest file.This definition needs to point to the XML file defined in step 3.

5- In your menu setup for the search invoker activity,indicate that the search view needs to target
the search results activity from step 2.

Monday, 22 December 2014

What are you learn about an ActionBar .

You learn the following about an ActionBar :

* An ActionBar is owned by an Activity and follows its life cycle.

* An ActionBar can take one of the three forms:
i)- Tabbed ActionBar.
ii)- List ActionBar.
iii)- Standard ActionBar.

you see how these various action bars looks and behave in each of the modes.

* You learn how tabbed listeners allow you to interact with a tabbed action bar.

* You see how spinner adapters and list listeners are used to interact with the list action bar.

* You learn how the Home icon of an action bar interacts with the menu infrastructure.

* You see how icon menu items can be shown and reacted to on the action bar real estate.

* You see how to place a custom search widget in the action bar.

- You explore these concepts by planning three different activities.Each Activity supports an
action bar in a different mode.This gives you an opportunity to examine the behaviour of the
action bar in each mode. 

Friday, 19 December 2014

About ActionBar in Android.

- ActionBar was introduced in the Android 3.0 SDK for tablets and is now available for phones as
well in 4.0 and above.

- It allows you to customize the title bar of an activity.

- Prior to the 3.0 SDK release,the title bar of an activity merely contained the title of an activity.

- Android ActionBar is modeled similar to the menu/title bar of a web browser.

- The 3.0 SDK is optimized and available only for tablets.

- This means the action bar API is not available for phones that run Android versions prior to 4.0

- With the 4.0 SDK,the phone and tablet aspects of the SDK are merged to provide a uniform API.

- A key goal of the action bar design is to make the frequently used actions easily available to the
user without searching through option menus or context menus.

Gravity Sensors in Android.

- Android 2.3 introduced the Gravity sensor.

- This isn't really a separate piece of hardware.It's a virtual sensor based on the accelerometers.

- In fact, this sensor uses logic similar to accelerometers to produce the gravity component of the forces acting on a device.

- The virtual sensor will take advantage of other hardware such as a gyroscope to help it
calculate gravity more accurately.

- The values array for this sensor reports gravity just like the accelerometer sensor reports its values. 

Thursday, 18 December 2014

Orientation Sensors in Android .

* We avoided talking about orientation sensors until now because they were deprecated as of  Android 2.2 and you're not supposed to use them anymore. However,this sensor is very easy to use.

* The Orientation Sensor is actually a combination of the magnetic field and accelerometers sensors
at the driver level of Android.

* In other words,there is no extra hardware for the orientation sensor,but within the Android OS, there is code to expose these two sensors as if they were another sensor for orientation.

Monday, 15 December 2014

Using Accelerometers and Magnetic Field Sensor Together.

- The SensorManager provides some methods that allow us to combine the compass sensor and accelerometers to figure out orientation.

- You can't use just the compass sensor alone to do the job.So SensorManager provides a method
called getRoatationMatrix(),which takes the values from the accelerometers and from the compass
and returns a matrix that can be used to determine orientation.

- Another SensorManager method,getOrientation(), takes the rotation matrix from the previous step
and gives an orientation matrix.

- The values from the orientation matrix tell us our device's rotation to the Earth's Magnetic north,
as well asthe device's pitch and roll relative to the ground.

Sunday, 14 December 2014

What are the effects of Magnetic Field Sensor in Android ?

- The Magnetic Field Sensor measures the ambient magnetic field in the x,y,z axis.

- This Coordinate System is aligned just like the accelerometers sensors.

- The Unit of Magnetic Field Sensor are microteslas(uT).

- This Sensor can detect the Earth's Magnetic Field and therefore tell us where north is.

- This Sensor is also referred to as the compass,and in fact the <uses-feature> tag uses
android.hardware.sensor.compass as the name of this Sensor.

- Because the sensor is so tiny and sensitive,it can be affected by magnetic fields generated
by things near the device,and even to some extent to components within the device.

- Therefore the accuracy of the magnetic field sensor may at times be suspect.

Accelerometers Sensors in Android.

* Accelerometers are probably the most interesting of the sensors on a device.

* Using these sensors, our application can determine the physical orientation of the device in space relative to gravity's pull straight down,plus be aware of forces pushing on the device.

* Providing this information allows an application to do all sorts of interesting things,from game play
to augmented reality and of course,the accelerometers tell Android when to switch the orientation
of the user interface from portrait to landscape back again.

The Accelerometer coordinate system works like this :

* The accelerometer's x-axis originates in the bottom-left corner of the device and goes across
the bottom to the right.

* The y-axis also originates in the bottom-left corner and goes up along the left of the display.

* The z-axis originates in the bottom-left corner and goes up in space away from the device.

Friday, 12 December 2014

What is Gyroscope Sensors in Android, and How it is combined with Accelerometer Sensors ?

* Gyroscopes are very cool components that can measure the twist of a device about a reference
   frame.It measures the rate of rotation about an axis.

* When the device is not rotating,the sensor values will be zeroes and when there is rotation in
   any direction,you'll get non-zero values from the gyroscope.

* A gyroscope can't tell you everything you need to know, and unfortunately errors creep in over
   time with gyroscopes.But coupled with accelerometers,you can determine the path of movement
   of the device.

Combined with Accelerometer Sensor:

   Kalman filters can be used to link data from the two sensors together.Accelerometers are not
   terribly accurate in the short term,and gyroscope are not very accurate in the long term,so
   combined they can be reasonably accurate all the time.While Kalman filters are very complex,
   there is an alternative called Complementry filters that are easier to implement in code and
   produces results that are pretty good.

* The gyroscope sensor returns three values in the values array for the x,y and z axis.
* The units are radians per second,and the values represent the rate of rotation around each of
   those axis.

* One way to work with these values is to integrate them over time to calculate an angle change.
   This is a similar calculation to integrating linear speed over time to calculate distance.

Thursday, 11 December 2014

Pressure Sensor in Android.

* This Sensor measures barometric pressure,which could detect altitute for example or be used for
weather predictions.

* This sensor should not be confused with the ability of a touchscreen to generate
a MotionEvent with a pressure(The pressure of the touch).

* Touchscreen pressure sensing doesn't use the Android sensor framework.

* The unit of measurement for a pressure sensor is atmospheric pressure in hPa(millibar) and this
measurement is delivered in values[0].

Wednesday, 10 December 2014

Temperature Sensors in Android.

* The old temperature sensor provided a temperature reading and also returned just a single value
in values[0].This sensor usually read an internal temperature,such as at the battery.

* There is a new temperature sensor called TYPE_AMBIENT_TEMPERATURE.The new value
represents the temperature outside the device in degrees Celsius.

* The placement of the temperature sensor is device dependent,and it is possible that the temperature
readings could be impacted by the heat generated by the device itself.

Tuesday, 9 December 2014

Using the Media API's.

Android supports playing audio and video under the package.

At the heart of the package is the class.
The MediaPlayer class is responsible for playing both audio and video content.The content for
this class can come from the following sources:

* Web: You can play content from the web via a URL.

* .apk file: You can play content that is packaged as part of your .apk file. You can package the media
content as a resource or as an asset ( with the assets folder ).

* SD card: You can play content that resides on the device's SD card.

The MediaPlayer is capable of decoding quite a few different content formats,including 3rd Generation
Partnership Project(3GPP, .3gp),MP3(.mp3),MIDI(.mid and others),Ogg Vorbis(.ogg),PCM/WAVE
(.wav), and MPEG-4(.mp4),RTSP,HTTP/HTTPS live streaming, and M3U playlists are also supported,

For a complete list of supported media formats,go to

Proximity Sensors in Android .

* The Proximity Sensors either measures the distance that some object is from the device ( in

centimeters ) or represents a flag to say whether an object is close or far.

* The Proximity sensor is sometimes the same hardware as the light sensor.

* The Proximity is often used in the phone application to detect the presence of a person's head

next to the device.If the head is that close to the touchscreen,the touchscreen is disabled so no

keys will be accidently pressed by the ear or cheek while the person is talking on the phone.

Sunday, 7 December 2014

Light Sensors in Android .

The Light Sensor is one of the simplest sensors on a device. This sensor gives a reading of the

light level detected by the light sensor of the device. As the light level changes,the sensors

readings change. The units of the data are in SI lux units.

For the values array in the SensorEvent object, a light sensor uses just the first element values[0].

This value is a float and ranges technically from 0 to the maximum value for the particular sensor.

We say technically because the sensor may only send very small values when there's no light,and

never actually send a value of 0.

For Example:

SensorManager has a constant called LIGHT_SUNLIGHT_MAX, which is float value of 120,000.

Friday, 5 December 2014

Interpreting Sensor Data.

We Understand how to get data from a sensor,we must do something meaningful with the data.

The data we get, however , will depend on which sensor we're getting the data from. Some

sensors are simpler than others. As new devices come into being,new sensors will undoubtedly

be introduced as well.the sensors framework is very likely to remain the same.

Thursday, 4 December 2014

What can we know about a Sensor ?

While using the uses-feature tags in the manifest file lets you know that a Sensor your

application requires exists on a device,it doesn't tell you everything you may want to

know about the actual sensor.

Detecting Sensor:

If your application needs a Proximity Sensor,you specify that in your manifest file


<uses-feature android:name="android.hardware.sensor.proximity" />

Wednesday, 3 December 2014

What is a Sensor in Android ?

A Sensor is a piece of Hardware that has been wired into device to feed data from the physical

world to applications. Applications use the sensor data to inform the user about the physical world,

to control game play,to do augmented reality,or to provide useful tools for working in the real world.

Some of the Sensor types that can appear in an Android device :

- Light Sensor.
- Proximity Sensor.
- Temperature Sensor
- Pressure Sensor
- Gyroscope Sensor
- Accelerometer
- Magnetic Field Sensor
- Orientation Sensor
- Gravity Sensor
- Linear Acceleration Sensor
- Rotation Vector Sensor
- Relative Humidity Sensor
- Near Field Communication ( NFC ) Sensor.

Monday, 1 December 2014

ViewGroups in Android .

ViewGroups are Views that contain child Views.

Each ViewGroup class embodies a different set of assumptions about how to display its child views.

All ViewGroup descend from the android.view.ViewGroup class.

Subset of ViewGroups are:

- Gallery and GridView.
- ListView and ListActivity.
- ScrollView
- TabHost