Programming for mobile devices
1
• Programming for mobile devices or Mobile application
development is the process of making software (applications) for
smartphones, tablets and digital assistants, most commonly for
the Android and iOS operating systems.
• The software can be preinstalled on the device, downloaded from
a mobile app store or accessed through a mobile web browser.
• Mobile applications frequently serve to provide users with similar
services to those accessed on PCs.
• Applications are generally small, individual software units with
limited function.
2
• A mobile application also may be known as an applications, Web
applications, online applications, iPhone applications or
smartphone applications.
• A mobile Operating System (OS) allows smartphones, tablet PCs
and other devices to run applications and programs.
• A mobile OS typically starts up when a device
powers on, presenting a screen with icons or tiles that present
information and provide application access. Mobile operating
systems also manage cellular and wireless network
connectivity, as well as phone access.
3
• Mobile operating systems are available for smartphones or/
and tablet PCs, including:
Android,
BlackBerry OS,
webOS,
Apple iOS,
Symbian,
Windows Mobile Professional (touch screen),
Windows Mobile Standard (non-touch screen), and
Bada.
4
Android OS
• Android is an open source and Linux-based OS for mobile devices
such as smartphones and tablet computers.
• Android is developed by a consortium of developers known as the
Open Handset Alliance, though its most widely used version is
primarily developed by Google.
• It was unveiled in November 2007, with the first commercial
Android device, the HTC Dream, launched in September 2008.
• At its core, the operating system is known as Android Open Source
Project (AOSP) and is free and open-source software (FOSS)
primarily licensed under the Apache License. However most
devices run on the proprietary Android version developed by
Google.
5
Android Studio
• Android Studio is the official integrated development
environment (IDE) for Google's Android operating
system, built on JetBrains' IntelliJ IDEA software and
designed specifically for Android development.
• It is available for download on Windows, macOS and
Linux based operating systems.
• It is a replacement for the Eclipse Android
Development Tools (E-ADT) as the primary IDE for
native Android application development.
6
Android Studio
• Basic system requirements for Android Studio
• And other requirements e.g., for IDE + Android SDK + Android Emulator
7
Mobile Applications
• What are they?
• Any application that runs on a mobile device
• Types
• Web apps: run in a web browser
• HTML, JavaScript, Flash, server-side components, etc.
• Native: compiled binaries for the device
• Often make use of web services
8
Mobile Devices: Advantages
• Always with the user
• Typically have Internet access
• Typically GPS enabled
• Most have cameras & microphones
• Many apps are free or low-cost
9
Mobile Devices: Disadvantages
• Limited screen size
• Limited battery life
• Limited processor speed
• Limited and sometimes slow network access
• Limited input: phone keypad, touch screen, stylus etc
• Limited web browser functionality
• Range of platforms & configurations across devices
10
Why Mobile App Development?
• Transferring app to phone is trivial
• Can distribute by putting it on the web
• Android Market (now Google Play) for wider distribution
11
Android Apps
• Built using Java and new SDK libraries
• No support for some Java libraries like Swing & AWT
• Java code compiled into Dalvik byte code (.dex)
• Optimized for mobile devices (better memory management, battery
utilization, etc.)
• Dalvik VM runs .dex files
12
Android Apps
• Written in Java or Kotlin (it’s possible to write native code)
• Good separation (and corresponding security) from other
applications:
• Each application runs in its own process
• Each process has its own separate VM
• Each application is assigned a unique Linux user ID – by default files of that
application are only visible to that application
13
Major Components
14
Image source: http://developer.android.com/guide/basics/what-is-android.html
Major Components
15
Applications
• Is the top layer of android architecture.
• Pre-installed applications like home, contacts, camera, gallery etc and
third party applications downloaded from the play store like chat
applications, games etc. will be installed on this layer only.
• It runs within the Android run time with the help of the classes and
services provided by the application framework.
16
Application framework
• Provides important classes which are used to create Android apps. It
provides a generic abstraction for hardware access and also helps in
managing the user interface with application resources.
• It includes different types of services activity manager, notification
manager, view system, package manager etc. which are helpful for
the development of apps.
17
Application runtime
• Android Runtime environment is one of the most important part
of Android. It contains components like core libraries and the
Dalvik virtual machine(DVM). Mainly, it provides the base for the
application framework and powers our application with the help
of the core libraries.
• Like Java Virtual Machine (JVM), Dalvik Virtual Machine (DVM) is a
register-based virtual machine and specially designed and
optimized for android to ensure that a device can run multiple
instances efficiently. It depends on the layer Linux kernel for
threading and low-level memory management. The core libraries
enable us to implement android applications using the standard
JAVA or Kotlin programming languages.
18
Platform libraries
• Includes various C/C++ core libraries and Java based libraries such as Media,
Graphics, Surface Manager, OpenGL etc. to provide support for android
development.
• Media library provides support to play and record an audio and video formats.
• Surface manager responsible for managing access to the display subsystem.
• SGL and OpenGL both cross-language, cross-platform application program
interface (API) are used for 2D and 3D computer graphics.
• SQLite provides database support and FreeType provides font support.
• Web-Kit This open source web browser engine provides all the functionality to
display web content and to simplify page loading.
• SSL (Secure Sockets Layer) is security technology to establish an encrypted link
between a web server and a web browser.
19
Linux Kernel
• Is the heart of the android architecture. It manages
all the available drivers such as display drivers,
camera drivers, Bluetooth drivers, audio drivers,
memory drivers, etc. required during the runtime.
• Will provide an abstraction layer between the
device hardware and the other components of
android architecture. It is responsible for
management of memory, power, devices etc.
20
Linux Kernel
• It does the following:
• Security: handles the security between the application and the
system.
• Memory Management: efficiently handles the memory
management thereby providing the freedom to develop our apps.
• Process Management: manages the process well, allocates
resources to processes whenever they need them.
• Network Stack: effectively handles the network communication.
• Driver Model: It ensures that the application works properly on
the device and hardware manufacturers responsible for building
their drivers into the Linux build.
21
Main Components of Android
• Activities
• Services
• Content Providers
• Broadcast Receivers
22
Activity
• Single page user interface
• Most apps have multiple activities
• Callable from other apps (if you allow it)
23
Activity
• Unlike most programming languages, in which the main() method is the
entry point for that program or application to start its execution, the
android operating system initiates the code in an Activity instance by
invoking specific callback methods that correspond to specific stages of
its Lifecycle.
• Every activity contains the layout, which has a user interface to
interact with the user.
• An activity is one screen of an app.
• The most specific block of the user interface is the activity. An
Android app contains activities, meaning one or more screens.
• Examples: Login screen, sign up screen, and home screen.
• Any app has at least one Activity class.
24
Activity
• In simple words Activity is a screen that user interact with. Every
Activity in android has lifecycle like created, started, resumed,
paused, stopped or destroyed. These different states are known as
Activity Lifecycle. In other words we can say Activity is a class pre-
written in Java Programming.
25
Activity
• Short description of Activity Lifecycle example:
• onCreate() – Called when the activity is first created, when activity gets memory in
the OS.
• onStart() – Called just after it’s creation or by restart method after onStop(). Here
Activity start becoming visible to user.
• onResume() – Called when Activity is visible to user and user can interact with it
• onPause() – Called when Activity content is not visible because user resume
previous activity
• onStop() – Activity is called stopped when it’s not visible to user. Any activity gets
stopped in case some other activity takes place of it. For example, if a user was on
screen 1 and click on some button and moves to screen 2. In this case Activity
displaying content for screen 1 will be stopped.
• onRestart() – Activity is called in restart state after stop state. So activity’s
onRestart() function gets called when user comes on screen or resume the activity
which was stopped.
• onDestroy(): Any activity is known as in destroyed state when it’s not in background.
26
Service
• No user interface
• Runs in background
• Started and stopped by activities
27
Service
• Android service is a component that is used to perform operations on
the background such as playing music, handle network transactions,
interacting content providers etc. It doesn't has any UI (user
interface).
• The service runs in the background indefinitely even if application is
destroyed.
28
Service
• Life Cycle of Android Service
• There can be two forms of a service. The lifecycle of service can follow two
different paths: started or bound.
1.Started
2.Bound
• 1) Started Service
• A service is started when component (like activity) calls startService()
method, now it runs in the background indefinitely. It is stopped by
stopService() method. The service can stop itself by calling the stopSelf()
method.
• 2) Bound Service
• A service is bound when another component (e.g. client) calls bindService()
method. The client can unbind the service by calling the unbindService()
method.
• The service cannot be stopped until all clients unbind the service.
29
Content Provider
• Manages shared set of application data
• Data may be shared between apps or be private
• Performs data handling functions
• Content providers are useful because:
• Apps cannot share data in Android—except through content
providers.
• Content providers allow multiple apps to securely access, use,
and modify a single data source. Examples: Contacts, game
scores.
• You can specify levels of access control (permissions) for your
content provider.
30
Broadcast Receiver
• Broadcast in android is the system-wide events that can occur
when the device starts, when a message is received on the
device or when incoming calls are received, or when a device
goes to airplane mode, etc.
• Broadcast Receivers are used to respond to these system-wide
events. Broadcast Receivers allow us to register for the system
and application events, and when that event happens, then the
register receivers get notified.
• Listens for system wide (intent) broadcasts
• Intent filter limits which intents cared about
• Similar to an interrupt handler
• Redirects to appropriate activity or service
31
Intents
• Android application components can connect to other Android
applications. This connection is based on a task description
represented by an Intent object.
• Intents are asynchronous messages which allow application
components to request functionality from other Android
components.
• Intents allows interaction with components from the same
applications as well as with components contributed by other
applications.
• E.g., an activity can start an external activity for taking a
picture.
32
Intents
33
User Interfaces
Images from Android Design
34
http://developer.android.com/design/get-started/ui-overview.html
User Interface Basics
• Recall the basic unit of an Android application is an Activity
• An Activity displays the user interface
• User Interfaces are built from View and ViewGroup object instances of the View
class
• View objects are data structures that store content and layout parameters
• controls a specific rectangular region of the screen
• responsible for drawing itself
• handles events
• Subclass “widgets” provide user interface objects:
• text fields, buttons, labels
• serves as means of interaction with user
Images from Android Design
35
http://developer.android.com/design/building-blocks/index.html
View & ViewGroup
• ViewGroup objects act like containers for View objects
• one or more Views are grouped together
• Subclass “layouts” provide different layout architectures:
• LinearLayout – displays Views in linear direction either horizontally or vertically
• default is horizontal
• text field, two radio buttons, and button
in a vertical orientation
• RelativeLayout – displays Views in relative position to each other
• an id needs to be assigned to elements for
reference
• text field, two radio buttons, and button
in relative position
Images from Android Basics & User Interfaces
36
http://www3.ntu.edu.sg/home/ehchua/programming/android/Android_BasicsUI.html
ViewGroup Layout
• TableLayout – displays Views in table form with rows and columns
• buttons for the calculator are in table form
• Some other layouts from base class android.view.ViewGroup:
• AbsoluteLayout
• FrameLayout
• ScrollView
Image from Android Basics & User Interfaces
37
http://www3.ntu.edu.sg/home/ehchua/programming/android/Android_BasicsUI.html
Common Attributes
• Android layout_width
• The Android layout_width attribute is used to define the basic width of a
view element you have in your layout.
• The width can be specified in any valid dimension measure (dp, in, mm, pt,
sp, px).
• Or you can also use the special constant for the width: match_parent and
wrap_content.
• The following code defines the width of a TextView as 300px (fixed):
<TextView
android:layout_width="300px"
android:background="@color/teal_200"
android:layout_height="wrap_content"
android:text="Hello World!" />
38
Common Attributes
• The previous code will generate the following output in your layout:
39
Common Attributes
• The wrap_content value will expand the width of the TextView element to
wrap the content of the element.
40
Common Attributes
• You can add horizontal paddings to your view as shown below:
<TextView
android:layout_width="wrap_content"
android:paddingHorizontal="30dp"
android:background="@color/teal_200"
android:layout_height="wrap_content"
android:text="Hello World!"/>
41
Common Attributes
• The match_parent constant will use the parent’s width excluding the
parent’s padding.
<TextView
android:layout_width="match_parent"
android:background="@color/teal_200"
android:layout_height="wrap_content"
android:text="Hello World!"/>
42
Common Attributes
• If the parent element has paddings, then the width of the view will be
reduced by the padding’s value.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingHorizontal="50dp"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:background="@color/teal_200"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</LinearLayout>
43
Common Attributes
• As you can see, paddings in the parent element reduces the width of the
view.
44
Common Attributes
• View and ViewGroup attributes:
• layout_width
• layout_height
• layout_margintop
• layout_marginbottom
• layout_marginleft
• layout_marginright
• layout_x
• layout_y
• LinearLayout and TableLayout attributes:
• layout_gravity – specifies how child Views are positioned
• layout_weight – specifies how much space is to be allocated, total must
equal 1
Images from Understanding User Interface In Android
45
http://mobiforge.com/designing/story/understanding-user-interface-android-part-1-layouts
View Hierarchy
• Parent nodes contain and organize layout of child nodes
• Child nodes are responsible for drawing themselves at the request of the parent
• control and respond to user input contained within their region of the display
• The Activity that displays a particular screen calls the setContentView() method to
draw the screen defined by the hierarchy
• XML layout files typically used for defining layouts and expressing view hierarchies
• each element in XML is a View or ViewGroup object
Image from Android Developers
46
http://developer.android.com/guide/topics/ui/index.html
Input Events
• Define to inform the system of user interaction
• Event listener is defined and registered with the View object
• View class has a collection of On<SomeEvent>Listener:
• View.OnClickListener
• View.OnTouchListener
• View.OnKeyListener
• Example: OnTouch, when the user touches a defined View object on the screen
• define View.OnTouchListener
• register the View object with setOnTouchListener()
47
Application Menus
• Two types of menus
• Options menu – typically when an application is running
• accessed by pressing the MENU button on the device
• Context menu – typically used for displaying specific information about an item
• accessed by pressing and hold down on an object
Images from Android User Interfaces
48
http://eagle.phys.utk.edu/guidry/android/androidUserInterface.html
Application Menus
• Options menus allow users quick access to an application’s functions,
preferences and settings
• Structured using a View hierarchy
• define onCreateOptionsMenu()
• define onCreateContectMenu()
• Handle their own events, no need to register event listeners
• Selections handled by methods
• onOptionsItemSelected()
• onContextItemSelected()
• Items for menus can be declared in an XML file like an application layout
49
References
50