Saturday 12 July 2014

3. Android Application Components

Components of Android Application

An android application can have mainly four basic components: Activities, Services, Broadcast Receivers, Content Providers.

Activity

  • Activities are components that are somehow related to dealing with users and seek interaction with the outside world. So whenever we need to present a screen to a user, that screen  is supported by an activity. Again, on navigating from one screen to another, every screen is supported by an activity.
  • An application can have one or more activities and each activity has a default window to draw.
  • Any activity extends the 'Activity' base class.
  • One activity is marked as the main activity (which starts when application begin).
  • Moving from one activity to another is accomplished by having the current activity start the next one.

Services
  • If we need to do something in the background, such things are supported by Services. So generally, services are designed for doing things in the background. For example downloading a file or playing a music file in the background is supported by a service.
  • We need to extend the 'Service' base class.

Broadcast Receivers
  • Now when the background work finishes, it need to notify us that the work is completed and whatever we needed is ready. This back notification is supported by Broadcast Receivers. So job of Broadcast Receivers is to identify the events that happened somewhere. For example, we receive email notification or notification for app installation.
  • These do not have a user interface.
  • In response to received broadcast, these can start an activity.

Content Provider
  • Finally, the job of Content Provider is to manage interaction with the DB in the background and provide us an API to interact with the background.
  • We need to extend 'ContentProvider' base class to implement a standard set of methods that enable other applications to retrieve and store data.

So within our application we might have different functionalities that need to be done and we need to divide these functionalities in pieces based upon the kind of work each one of them is doing and allocate them to one of these components.

Activating Components

We can activate these components using Intents and Content Resolver.


Intents

  • Intents are asynchronous message objects to activate components other than content providers. In simple terms, it a way to trigger somebody to do something.
  • For activities and services, message consist of action being requested (MAIN/VIEW/EDIT/PICK/DELETE/etc) and URI of the data to act upon.
  • For broadcast receivers, message is the action being announced.
  • Explicit Intent - explicitly specify the component's name that should respond to the intent. example:
     public void onClick(View v) {
       startActivity(new Intent(welcome.this,menu.class));
     }

  • Implicit Intent - android locate the best component that can respond to the intent. for example:
     startActivity(new Intent(Intent.ACTION_VIEW,
     Uri.parse("http://google.com")));

Shutting Down Components

  • Activities and services need to be shut down explicitly.
  • No need to explicitly shut down content provider and broadcast receiver.
  • A content provider is active only while responding to a request from a ContentResolver. A broadcast receiver is active only while responding to a broadcast message.
  • Components might also be shutdown by the system when they are no longer be used or when android must reclaim memory for more active components.










No comments:

Post a Comment