Thursday, 22 December 2016

Basic Building Blocks of Android

Building Blocks in Android are :

  1. Activities
  2. Services 
  3. BroadCast Receiver
  4. Content Provider
Activity :
Activity is just a class . A class which extends from Activity, for the purpose of GUI.
Example :  class ExampleActivity extends Activity { }
It also saves as .java file .
Activity is used for interaction with GUI . Like if we have to show button , textview, list view in our android app then we have to present it on Activity .

We have to declare Activity in Manifest file .

<manifest ... >
  <application ... >
      <activity android:name=".ExampleActivity" />
      ...
  </application ... >
  ...</manifest >

Example: Like if are using Music Player  ,  all the song content which shows in list,  it is GUI and it is shown on Activity/Fragment.

music owl music player
Music Owl Music Player

Services :
It is used when we have to perform long running operation in background / or without blocking UI .
Like if we have to upload large file we cannot block ui for 1min or may be more so in that case we have to use Service .

Take another example of Music Player, if you download  this app, then after playing song, you move back or close the activity, still the player play song, this is because of service.

If we want to execute any code, in background, then the solution is Service .

It is also a java class . A class which extend from Service .

Example : class ExampleService extends Service.
We have to declare Service in Manifest  file .


<manifest ... >
  <application ... >
      <service android:name=".ExampleService" />
      ...
  </application ... >
  ...</manifest >


Broadcast Receiver :
This is also a java class but it extends from BroadCast Receiver .
Example : class ExampleBroadCast extends BroadCastReceiver {} .

BroadCast Receiver is used to listen the broadcast event generated by system or android app .

<manifest ... >
  <application ... >
      <receiver android:name=".ExampleBroadCast" />
      ...
  </application ... >
  ...</manifest >
We have to define it in Android Menifest file with receiver tag .

Content Provider :
Content Provider is used to provide the content by using content resolver . Like if we have to get all contact from our app. We will use Content Provider .

Content provider  provide data from one application to others on request, and these requests are handled by the methods of the ContentResolver class. The data may be stored  the db or file .













3 comments: