Mobile Application Development
Activity & Services
Android User Interfaces
1. Activities
2. Layouts
3. Menus
4. Dialogs
5. Intents
6. Notifications
Creating an Activity
• Create a new Activity by extending the Activity class.
• Override the onCreate() method to set the view for the
activity
• Set the user interface to an Activity using the
setContentView() method in the onCreate method of
the Activity.
public class MyActivity extends Activity {
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.main);
}
}
• Activities in the system are managed as an activity stack.
• The Android memory manager uses this stack to determine
the priority of applications based on their Activities when
deciding which application to terminate to free resources.
• An activity has essentially four states:
 Active : An activity in the foreground of the screen.
 Paused : An activity has lost focus but is still visible.
 Stopped : An activity is completely obscured by another
activity.
 Inactive : After an Activity has been killed or before it has
been launched.
Activity Life Cycle
Activity Lifetimes
• Entire lifetime :
Happens between the first call to onCreate(Bundle) through
to a single final call to onDestroy(). An activity will do all
setup of "global" state in onCreate(), and release all remaining
resources in onDestroy().
• Visible lifetime :
Happens between a call to onStart() until a corresponding call
to onStop(). During this time the user can see the activity on-
screen, though it may not be in the foreground and
interacting with the user.
• Foreground lifetime :
Happens between a call to onResume() until a corresponding
call to onPause().
Layouts
• Layout is the design plan or the architecture for the
user interface in an activity.
• Layout is required to arrange or organize the widgets
inside an activity.
• Layout defines the activity structure & holds all the
elements that are part of the user interface.
• Layouts can be nested.
A Layout can be declared in two ways:
• Declaring in XML:
Android provides a straightforward XML tags that
corresponds to the View classes and subclasses, such as
those for widgets and layouts that can be added to the
layout.
• Instantiating at runtime:
Your application can create View and ViewGroup objects
(and manipulate their properties) programmatically.
Declaring Layout
• Linear Layout
A layout that organizes its children into a single horizontal or
vertical row.
• Table Layout
A layout that positions its children into rows and columns.
• Relative Layout
A layout that allows to organize child elements in positions
relative to the parent or siblings elements.
• Frame Layout
It is designed to block out an area on the screen to display a
single item.
Layout Types
Menus
• Menus provide familiar interfaces to expose
application functions without sacrificing screen
space.
• Android offers an easy programming interface to
provide standardized application menus.
• Android offers three fundamental types of
application menus:
 Options Menu
 Context Menu
 Sub-menu
• By default, every Activity supports an Options menu. It
is revealed by pressing the device MENU key.
• Options Menu has two groups of menu items:
Icon Menu
 Collection of maximum of six menu items
 Supports Icons and short-cuts
Expanded Menu
 Exposed by the 'More' menu item
 Displayed when the icon menu becomes over-loaded
 Comprised of sixth options menu item and the rest.
Options Menu
Options Menu
Context Menu
• Context menu is a floating menu that is associated
with a control.
• Context menu is launched when the control has the
focus and the D pad is pressed.
• Context menus can be assigned to any View within
an Activity.
• It provides functions relating to the view, to which it
is registered.
• It supports submenus, checkboxes, radio buttons.
• It does not support shortcuts and icons.
Context Menu
• Sub menu is a floating menu, it can be a child menu of
options menu or context menu.
• It supports checkboxes, radio buttons and shortcut
keys.
• It does not support item icons, or nested sub menus.
Sub Menu
Sub Menu
• A dialog is a small window that appears in front of an
Activity.
• The underlying Activity loses focus and the dialog
accepts all user interaction.
• Normally used for notifications and short activities that
directly relate to the application in progress.
• Android supports following types of dialogs.
Alert Dialog
Progress Dialog
Date Picker Dialog
Time Picker Dialog
Toast
Dialog
• A dialog that can manage zero, one, two or three
buttons.
• It can also manage a list of selectable items that can
include checkboxes or radio buttons.
• The AlertDialog is capable of constructing most
dialog user interfaces and is the suggested dialog
type.
• AlertDialog class is a subclass of Dialog class and
has a nested subclass AlertDialog.Builder to
construct a dialog.
Alert Dialog
Other Dialogs
• Progress Dialog
A dialog that displays a progress wheel or progress
bar.
It supports buttons like in AlertDialog.
• DatePicker Dialog
A dialog that allows the user to select a date.
• TimePicker Dialog
A dialog that allows the user to select a time.
• A toast is a transient Dialog box containing a
quick little message for the user.
• Toasts never receive focus and they don’t
interrupt the active application.
• They provide an ideal mechanism for alerting
users to events occurring in background Services
without interrupting foreground applications.
Toast
• Intents are the messages that are used to activate
following application components:
Activities
Services
Broadcast Receivers
• Intent is an object, which has a data structure holding
any one of the following data :
Abstract description of an operation to be performed
In the case of broadcasts, a description of something
that has happened and is being announced.
Intents
Application components are invoked through intents
in the following way :
• Activity : Intent object is passed to following
functions to invoke the Activity.
Context.startActivity (Intent intent)
Activity.startActivityForResult (Intent intent, int
requestCode)
• Service : Intent object is passed to following functions
to invoke the Service.
Context.startService (Intent)
Context.bindService (Intent)
Invoking Components
• Broadcast Receivers : Intent object is passed to
following functions to invoke the Broadcast
Receiver.
 Context.sendBroadcast ( Intent intent, String
receiverPermission)
 Context.sendOrderedBroadcast ( Intent intent,
String receiverPermission)
 Context.sendStickyBroadcast ( Intent intent )
Invoking Components
• A status bar notification adds an icon to the system's
status bar (with an optional ticker-text message) and an
expanded message in the "Notifications" window.
• When the user selects the expanded message, Android
fires an Intent that is defined by the notification
(usually to launch an Activity).
• You can also configure the notification to alert the user
with a sound, a vibration, and flashing lights on the
device.
• This kind of notification is ideal when your application
is working in a background Service and needs to notify
the user about an event.
Notification
• To create a notification, following classes should be
used:
Notification
NotificationManager
• Notification is a class that represents how a persistent
notification is to be presented to the user using the
NotificationManager.
• NotificationManager is an Android system service used
to handle notifications.
Notification (Continued)
Notification (Continued)
Notification (Continued)
• A Service is an application component that runs in the
background for an indefinite period of time.
• Services are specifically used to handle operations and
functionality that should run silently, without any User
Interface.
• Services are generally used to perform following
operations in the background
 Updating the Content Providers
 Firing Intents
 Triggering Notifications etc
• Services, like other application objects, run in the main
thread of their hosting process.
Services
• Services are started, stopped, and controlled from
other application components including other
Services, Activities, and Broadcast Receivers.
• Service is involved in any CPU intensive (such as MP3
playback) or blocking (such as networking)
operations, should spawn its own thread to do that
work.
• Android platform has following services:
 Location Manager
 Media Controller
 Notification Manager
Services (Continued)
• Service supports following lifecycle methods :
 OnCreate
 OnStart
 OnBind
 OnUnbind
 OnRebind
 OnDestroy
• Service is controlled using following methods:
 Context.startService
 Context.stopService
 Service.stopSelf
 Context.bindService
 Context.UnbindService
Service Life Cycle
1. Professional Android Application Development, Reto
Meier, Wrox, November 2008
2. Beginning Android, Mark Murphy, Apress, June 2009
3. Pro Android, Sayed Y Hashimi, Apress, June 2009
4. Android – Application Development, Rick Rogers et.al,
O’Reilly, May 2009
5. “Hello, Android”, Introducing Google’s Mobile
Development Platform, Ed Burnette, The Pragmatic
Bookshelf, 2008
6. developer.android.com
References

Android - Activity, Services

  • 1.
  • 2.
    Android User Interfaces 1.Activities 2. Layouts 3. Menus 4. Dialogs 5. Intents 6. Notifications
  • 3.
    Creating an Activity •Create a new Activity by extending the Activity class. • Override the onCreate() method to set the view for the activity • Set the user interface to an Activity using the setContentView() method in the onCreate method of the Activity. public class MyActivity extends Activity { public void onCreate(Bundle b) { super.onCreate(b); setContentView(R.layout.main); } }
  • 4.
    • Activities inthe system are managed as an activity stack. • The Android memory manager uses this stack to determine the priority of applications based on their Activities when deciding which application to terminate to free resources. • An activity has essentially four states:  Active : An activity in the foreground of the screen.  Paused : An activity has lost focus but is still visible.  Stopped : An activity is completely obscured by another activity.  Inactive : After an Activity has been killed or before it has been launched. Activity Life Cycle
  • 6.
    Activity Lifetimes • Entirelifetime : Happens between the first call to onCreate(Bundle) through to a single final call to onDestroy(). An activity will do all setup of "global" state in onCreate(), and release all remaining resources in onDestroy(). • Visible lifetime : Happens between a call to onStart() until a corresponding call to onStop(). During this time the user can see the activity on- screen, though it may not be in the foreground and interacting with the user. • Foreground lifetime : Happens between a call to onResume() until a corresponding call to onPause().
  • 7.
    Layouts • Layout isthe design plan or the architecture for the user interface in an activity. • Layout is required to arrange or organize the widgets inside an activity. • Layout defines the activity structure & holds all the elements that are part of the user interface. • Layouts can be nested.
  • 8.
    A Layout canbe declared in two ways: • Declaring in XML: Android provides a straightforward XML tags that corresponds to the View classes and subclasses, such as those for widgets and layouts that can be added to the layout. • Instantiating at runtime: Your application can create View and ViewGroup objects (and manipulate their properties) programmatically. Declaring Layout
  • 9.
    • Linear Layout Alayout that organizes its children into a single horizontal or vertical row. • Table Layout A layout that positions its children into rows and columns. • Relative Layout A layout that allows to organize child elements in positions relative to the parent or siblings elements. • Frame Layout It is designed to block out an area on the screen to display a single item. Layout Types
  • 11.
    Menus • Menus providefamiliar interfaces to expose application functions without sacrificing screen space. • Android offers an easy programming interface to provide standardized application menus. • Android offers three fundamental types of application menus:  Options Menu  Context Menu  Sub-menu
  • 12.
    • By default,every Activity supports an Options menu. It is revealed by pressing the device MENU key. • Options Menu has two groups of menu items: Icon Menu  Collection of maximum of six menu items  Supports Icons and short-cuts Expanded Menu  Exposed by the 'More' menu item  Displayed when the icon menu becomes over-loaded  Comprised of sixth options menu item and the rest. Options Menu
  • 13.
  • 14.
    Context Menu • Contextmenu is a floating menu that is associated with a control. • Context menu is launched when the control has the focus and the D pad is pressed. • Context menus can be assigned to any View within an Activity. • It provides functions relating to the view, to which it is registered. • It supports submenus, checkboxes, radio buttons. • It does not support shortcuts and icons.
  • 15.
  • 16.
    • Sub menuis a floating menu, it can be a child menu of options menu or context menu. • It supports checkboxes, radio buttons and shortcut keys. • It does not support item icons, or nested sub menus. Sub Menu
  • 17.
  • 18.
    • A dialogis a small window that appears in front of an Activity. • The underlying Activity loses focus and the dialog accepts all user interaction. • Normally used for notifications and short activities that directly relate to the application in progress. • Android supports following types of dialogs. Alert Dialog Progress Dialog Date Picker Dialog Time Picker Dialog Toast Dialog
  • 19.
    • A dialogthat can manage zero, one, two or three buttons. • It can also manage a list of selectable items that can include checkboxes or radio buttons. • The AlertDialog is capable of constructing most dialog user interfaces and is the suggested dialog type. • AlertDialog class is a subclass of Dialog class and has a nested subclass AlertDialog.Builder to construct a dialog. Alert Dialog
  • 21.
    Other Dialogs • ProgressDialog A dialog that displays a progress wheel or progress bar. It supports buttons like in AlertDialog. • DatePicker Dialog A dialog that allows the user to select a date. • TimePicker Dialog A dialog that allows the user to select a time.
  • 23.
    • A toastis a transient Dialog box containing a quick little message for the user. • Toasts never receive focus and they don’t interrupt the active application. • They provide an ideal mechanism for alerting users to events occurring in background Services without interrupting foreground applications. Toast
  • 24.
    • Intents arethe messages that are used to activate following application components: Activities Services Broadcast Receivers • Intent is an object, which has a data structure holding any one of the following data : Abstract description of an operation to be performed In the case of broadcasts, a description of something that has happened and is being announced. Intents
  • 25.
    Application components areinvoked through intents in the following way : • Activity : Intent object is passed to following functions to invoke the Activity. Context.startActivity (Intent intent) Activity.startActivityForResult (Intent intent, int requestCode) • Service : Intent object is passed to following functions to invoke the Service. Context.startService (Intent) Context.bindService (Intent) Invoking Components
  • 26.
    • Broadcast Receivers: Intent object is passed to following functions to invoke the Broadcast Receiver.  Context.sendBroadcast ( Intent intent, String receiverPermission)  Context.sendOrderedBroadcast ( Intent intent, String receiverPermission)  Context.sendStickyBroadcast ( Intent intent ) Invoking Components
  • 27.
    • A statusbar notification adds an icon to the system's status bar (with an optional ticker-text message) and an expanded message in the "Notifications" window. • When the user selects the expanded message, Android fires an Intent that is defined by the notification (usually to launch an Activity). • You can also configure the notification to alert the user with a sound, a vibration, and flashing lights on the device. • This kind of notification is ideal when your application is working in a background Service and needs to notify the user about an event. Notification
  • 28.
    • To createa notification, following classes should be used: Notification NotificationManager • Notification is a class that represents how a persistent notification is to be presented to the user using the NotificationManager. • NotificationManager is an Android system service used to handle notifications. Notification (Continued)
  • 29.
  • 30.
  • 31.
    • A Serviceis an application component that runs in the background for an indefinite period of time. • Services are specifically used to handle operations and functionality that should run silently, without any User Interface. • Services are generally used to perform following operations in the background  Updating the Content Providers  Firing Intents  Triggering Notifications etc • Services, like other application objects, run in the main thread of their hosting process. Services
  • 32.
    • Services arestarted, stopped, and controlled from other application components including other Services, Activities, and Broadcast Receivers. • Service is involved in any CPU intensive (such as MP3 playback) or blocking (such as networking) operations, should spawn its own thread to do that work. • Android platform has following services:  Location Manager  Media Controller  Notification Manager Services (Continued)
  • 33.
    • Service supportsfollowing lifecycle methods :  OnCreate  OnStart  OnBind  OnUnbind  OnRebind  OnDestroy • Service is controlled using following methods:  Context.startService  Context.stopService  Service.stopSelf  Context.bindService  Context.UnbindService Service Life Cycle
  • 35.
    1. Professional AndroidApplication Development, Reto Meier, Wrox, November 2008 2. Beginning Android, Mark Murphy, Apress, June 2009 3. Pro Android, Sayed Y Hashimi, Apress, June 2009 4. Android – Application Development, Rick Rogers et.al, O’Reilly, May 2009 5. “Hello, Android”, Introducing Google’s Mobile Development Platform, Ed Burnette, The Pragmatic Bookshelf, 2008 6. developer.android.com References