Monday 18 January 2016

Android Components and Intents- Part-2

 Android Components and Intents

This tutorial begins where tutorial-1 left off. If you have not read then have a look and continue.

Android Manifest


Every android application must have an “AndroidManifest.xml” file in its root directory. This file specifies all the essential information about the app to the system before running the app. Some of the things that manifest file include are:
It contains the package name (unique identifier), version code and version name for the application.
It contains the minimum sdk version that the application requires.
It describes the components of the application i.e. activity, service, broadcast receiver or content provider.
It declares the required permissions for accessing the specific API’s.
It lists the libraries that the application must be linked against.

Structure of Manifest file



Android Application Components


Andriod basically has four components:
  • Acitivity
  • Service
  • BroadcastReceiver
  • ContentProvider


Activity : An activity represents a single screen with which the user interacts. An application can have n number of activities depending upon the functionality. Eg: An app for educational institute can have an activity for student registration, other activity to view student records etc. For more than one activities, one of them should be declared as launcher activity in manifest file which will be presented first to the user on app launch.
An activity is declared as a subclass of “Activity” class.
Syntax:


public class StartActivity  extends Activity
{
  //activity logic
}

Service: A service is an application component that runs in background to perform long running operation. Eg: to download a file without interrupting the user interaction with activity. It usually takes two form:

Started:  A service is started when an application component such as activity starts it by calling startService(). Once started the service can run in background even if the component that started it is destroyed. Eg: to upload or download a file over network and the service will stop automatically when the operation is done.

Bound: A service is started when an application component such as activity starts it by calling bindService().A bound service runs only as long as another application component is bound to it. Eg: bound service might be playing an audio file and sending data regarding audio start/pause/stop and the time elapsed to the launching Activity component so that the UI can be updated accordingly
A service is declared as a subclass of “Service” class

Syntax:


public class MyService extends Service
{
  //service logic
}

BroadcastReceiver: This component simply responds to the system or application events. Eg: an application can register for “ACTION_BOOT_COMPLETED” or  “ACTION_POWER_CONNECTED” events which is fired once the boot is completed or power is connected.

A broadcast receiver is declared as a subclass of “BroadcastReceiver” class and each message is broadcasted as an Intent object.

Syntax:


public class MyReceiver extends BroadcastReceiver
{
  public void onReceive(context, intent)
     {
      }
}

Content Providers: A content provider component supplies data from one application to others on request. Such requests are handled by the methods of the “ContentResolver” class.

Syntax:


public class MyContentProvider extends  ContentProvider
{
   public void onCreate(){}
}

Intents and Intent Filters


Intents are basically used to communicate among the android application components. Intents are used to:

Start an Activity: If user want to switch from one activity to other, he can specify the activity name explicitly in the startActivity() method.
Eg: If I want to switch from LoginActivity to WelcomeActivity and pass user name also.



Intent intent = new Intent(LoginActivty.this, WelcomeActivty.class);
Intent.putExtra(“username”, “abc”); // here username is the key and abc is the value
startActivity(intent);

If user wants to receive result from the activity when it finishes, call startActivityForResults().
To use this method we need to override onActivityResult(). Eg: To start a camera and receive clicked image on image click.

Start a Service: We can start a service by passing intent to startService().
Eg:



startService(new Intent(getBaseContext(), MyService.class));

Start a broadcastReceiver: We can deliver a broadcast to other app by passing an intent to sendBroadcast().
Eg:



Intent intent = new Intent();
intent.setAction("com.tutorialspoint.CUSTOM_INTENT");
sendBroadcast(intent);

Intents are classified into two types:

Explicit Intent: Intents in which the component name is specifies explicitly is called explicit intents.
Eg:



Intent intent = new Intent(LognActivity.this, WelcomeActivity.class);
startActivty();

Implicit Intents: Intents in which we specify the action to be performed and content for the action is called implicit intent.
Eg: to open a website



Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);

0 comments:

Post a Comment

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel