Application Components

Embed Size (px)

Citation preview

  • 8/12/2019 Application Components

    1/32

    Application Components

    Activitiesvisual user interface focused on a single thing a user

    can do

    Servicesno visual interfacethey run in the background

    Broadcast Receiversreceive and react to broadcast

    announcements

    Content Providersallow data exchange between applications

  • 8/12/2019 Application Components

    2/32

    Activities

    Basic component of most applications

    Most applications have several activities that start each otheras needed

    Each is implemented as a subclass of the base Activity class

    An activitypresents a visual user interface for one focused the

    user.

    The visual content of the window is provided by a hierarchy ofviews.

    objects derived from the base View class.

  • 8/12/2019 Application Components

    3/32

    Continue

    For example, an email application might have one activitythat

    shows a list of new emails, another activity to compose an

    email, and another activity for reading emails.

    Although the activities work together to form a cohesive user

    experience in the email application, each one is independent of

    the others

  • 8/12/2019 Application Components

    4/32

  • 8/12/2019 Application Components

    5/32

    Activities and Tasks

    As, we know An activitypresents a visual user interface

    Task is what the user experiences as an application.

    Task is a group of related activities, arranged in a stack.

    The activity at the top of the stack is one that's currentlyrunning.

    All the activities in a task move together as a unit.

  • 8/12/2019 Application Components

    6/32

    Activity Lifecycle

    An activity has essentially three states

    Active or Running

    Paused

    Stopped

    Activity Lifetime

    Entire Lifetime [onCreate()to onDestroy()]

    Visible Lifetime [onStart()to onStop()]

    Foreground Lifetime [onResume()to onPause()]

    An implementation of any activity lifecycle method shouldalways first call the superclassversion

  • 8/12/2019 Application Components

    7/32

    Activity Lifecycle

    in detail

    ResumedThe activity is in the foreground of the screen and has user

    focus

    Paused

    Another activity is in the foreground and has focus, but thisone is still visible

    Stopped

    The activity is completely replaced by another activity(the activity is now in the "background"). A stoppedactivity is also still alive

    It can be killed by the system when memory is needed

  • 8/12/2019 Application Components

    8/32

  • 8/12/2019 Application Components

    9/32

  • 8/12/2019 Application Components

    10/32

  • 8/12/2019 Application Components

    11/32

    Services

    A Service does not have a visual interface and runs in the

    background.

    Each service extends the Servicebase class.

    It's possible to connect to an ongoing service and communicate

    it through the interface exposed by that service.

    Services run in the main thread of the application process.

  • 8/12/2019 Application Components

    12/32

  • 8/12/2019 Application Components

    13/32

    Service Lifecycle

    A service can be used in two ways

    startService()

    stopService()

    bindService()

    unbindService() Service Lifetime

    Entire Lifetime [onCreate()to onDestroy()]

    Active Lifetime [onStart()]

    The onCreate() and onDestroy() methods are called for all services.

    onStart()is called only for services started by startService().

  • 8/12/2019 Application Components

    14/32

  • 8/12/2019 Application Components

    15/32

    Services

    Runs in the background indefinitely

    Examples

    Network DownloadsPlaying Music

    You can bind to a an existing service and control its operation

  • 8/12/2019 Application Components

    16/32

  • 8/12/2019 Application Components

    17/32

    Broadcast Receivers

    A broadcast receiveris a component that responds to system-wide broadcastannouncements. Many broadcasts originate from the system

    Receive and react to broadcast announcements

    Extend the class BroadcastReceiver

    Examples of broadcasts:

    - Low battery

    - power connected

    - shutdown

    - timezonechanged, etc.

    - Other applications can initiate broadcasts

    Many broadcasts originate in system code.

    Broadcast receivers do not display a user interface but they can start an activity oralert user.

  • 8/12/2019 Application Components

    18/32

  • 8/12/2019 Application Components

    19/32

  • 8/12/2019 Application Components

    20/32

    Content Providers

    A content providermanages a shared set of application data.

    You can store the data in the file system, an SQLite database,

    on the web, or any other persistent storage location your

    application can access.

    For example, the Android system provides a content provider

    that manages the user's contact information.

  • 8/12/2019 Application Components

    21/32

    Content Providers

    Makes some of the application data available to otherapplications

    Its the only way to transfer data between applications in

    Android (no shared files, shared memory, pipes, etc.)

    Extends the class ContentProvider;

    Other applications use a ContentResolverobject to access thedata provided via a ContentProvider

  • 8/12/2019 Application Components

    22/32

    Content Providers

    All content providers extends the ContentProviderbase class.

    Content Providers are accessed through

    ContentResolverobject.

    Content Providers and Content Resolvers enable inter-process

    communication (IPC)

  • 8/12/2019 Application Components

    23/32

  • 8/12/2019 Application Components

    24/32

    Intents Three of the four component typesactivities, services, and

    broadcast receiversare activated by an asynchronousmessage called an intent.

    Intents bind individual components to each other at runtime

    An intent is an Intentobject with a message content.

    An intent is an object of Intent class that holds the contentof the message.

    Activities, services and broadcast receivers are started byintents.

  • 8/12/2019 Application Components

    25/32

    ContentProvidersare started by ContentResolvers:

    An activityis started by Context.startActivity(Intent intent)or

    Activity.startActivityForResult(Intent intent,intRequestCode)

    A serviceis started by Context.startService(Intent service)

    An application can initiate a broadcastby using an Intent inany of

    Context.sendBroadcast(Intent intent)

    Context.sendOrderedBroadcast()

    Context.sendStickyBroadcast()

  • 8/12/2019 Application Components

    26/32

    Intents

    Intents are Asynchronous messages used to convey a requestor message.

    Intent can contain

    Component name

    Action

    Data

    Category

    Flags

  • 8/12/2019 Application Components

    27/32

    Shutting down components

    Activities

    Can terminate itself via finish();

    Can terminate other activities it started via finishActivity();

    ServicesCan terminate via stopSelf(); or Context.stopService();

    Content Providers

    Are only active when responding to ContentResolvers

    Broadcast Receivers

    Are only active when responding to broadcasts

  • 8/12/2019 Application Components

    28/32

    The Manifest File

    Before the Android system can start an application component,

    the system must know that the component exists by reading

    the application's AndroidManifest.xml file

    Your application must declare all its components in this file,

    which must be at the root of the application project directory.

  • 8/12/2019 Application Components

    29/32

  • 8/12/2019 Application Components

    30/32

  • 8/12/2019 Application Components

    31/32

    Explanation

    element

    android:iconattribute points to resources for an icon that

    identifies the application

    element

    android:nameattribute specifies the fully qualified class

    name of the Activitysubclass and the android:labelattributes

    specifies a string to use as the user-visible label for the

    activity.

    http://developer.android.com/guide/topics/manifest/application-element.htmlhttp://developer.android.com/guide/topics/manifest/activity-element.htmlhttp://developer.android.com/reference/android/app/Activity.htmlhttp://developer.android.com/reference/android/app/Activity.htmlhttp://developer.android.com/guide/topics/manifest/activity-element.htmlhttp://developer.android.com/guide/topics/manifest/application-element.html
  • 8/12/2019 Application Components

    32/32

    Declare All Application Components

    elements for activities

    elements for services

    elements for broadcast receivers

    elements for content providers

    Note :

    Activities, services, and content providers that you include inyour source but do not declare in the manifest are not visible to

    the system and, consequently, can never run.

    http://developer.android.com/guide/topics/manifest/activity-element.htmlhttp://developer.android.com/guide/topics/manifest/service-element.htmlhttp://developer.android.com/guide/topics/manifest/receiver-element.htmlhttp://developer.android.com/guide/topics/manifest/provider-element.htmlhttp://developer.android.com/guide/topics/manifest/provider-element.htmlhttp://developer.android.com/guide/topics/manifest/receiver-element.htmlhttp://developer.android.com/guide/topics/manifest/service-element.htmlhttp://developer.android.com/guide/topics/manifest/activity-element.html