Saturday 7 January 2017

Android Core Building Blocks

  An android component is simply a piece of code that has a well defined life cycle e.g. Activity,Receiver,Service etc.These components are loosely coupled by the application manifest file AndroidManifest.xml that describe each component of the application and how they interact.

Following are main building blocks :
  • Activity
  • View
  • Service
  • Broadcast Receivers
  • Content Providers

Activity :

Activity represents one screen for example login screen.An single activity is implement Activity class as follows:

public class MainActivity extends AppCompatActivity{
//block of code...
}

View : 

A view is single UI element, such as a Button,TextView,EditText etc.

Service :

  A service is background process which is run in background. To execute Service application not need to open it will runs when application is closed.for example playing music in background is one type of service, another example is show reminders.A Service class implemented as follows :

public class Service extends Service{
//block of code...
}

Broadcast Receivers : 

  Broadcast Receivers use to get broadcast messages from devices.For example,if your application register for the ACTION_BOOT_COMPLETED system event which is fired once the Android system has completed the boot process.

  To get broadcast messages your want to register your application into Broadcast Receivers by adding following code into AndroidManifest.xml file.

<application>
<receiver android:name=".AlarmReciever"/>
</application>

Following class which is extends BroadcastReceiver class:

public class AlarmReciever extends BroadcastReceiver{
//block of code...
}

Content Providers :

Content Provider are used to share data between applications on requests.

A content provider is implemented as a subclass of ContentProvider class.

public class MyAppContentProvider extends ContentProvider{
//block of code...

Additional Components:

Fragments : 
Represent portion of user interface in an activity.It is like part of activity. 
Intents :
        Intents mainly used to : 
- Start new activity
- Start service
- Open device application like email app,Phone Dial pad etc.
- Broadcast a message.
Following example start new activity within your app.

startActivity(new Intent(getApplicationContext(),SecondActivity.class));
AndroidManifest.xml : 
    AndroidManifest file is a heart of android application.It contains information about activities,services,permissions etc.

No comments:

Post a Comment