29
Basics: Development of Android Apps Architecture and Best practices Vishal Kumar Jaiswal IIEST Shibpur June 29, 2016 Vishal Kumar Jaiswal (IIEST Shibpur) 1/ 29 June 29, 2016 1 / 29

Basics of Android App Development

Embed Size (px)

Citation preview

Page 1: Basics of Android App Development

Basics: Development of Android AppsArchitecture and Best practices

Vishal Kumar Jaiswal

IIEST Shibpur

June 29, 2016

Vishal Kumar Jaiswal (IIEST Shibpur) 1/ 29 June 29, 2016 1 / 29

Page 2: Basics of Android App Development

Table of Contents

1 Introduction

2 Architecture

3 Libraries

4 Runtime

5 Framework

6 Components

Vishal Kumar Jaiswal (IIEST Shibpur) 2/ 29 June 29, 2016 2 / 29

Page 3: Basics of Android App Development

Introduction

Overview

Free and open source linux based operating system,Developed by Open Handset Alliance,Provides unified approach to application development,Provides SQLite relational database, webkit open source webbrowser,App development in JAVA programming language using AndroidSDK,Primary app store Google Play,Software required: JAVA JDK, Android SDK

Vishal Kumar Jaiswal (IIEST Shibpur) 3/ 29 June 29, 2016 3 / 29

Page 4: Basics of Android App Development

Architecture

Vishal Kumar Jaiswal (IIEST Shibpur) 4/ 29 June 29, 2016 4 / 29

Page 5: Basics of Android App Development

Architecture

Architecture

Linux Kernel:A level of abstraction between the device hardware.Contains all the essential hardware drivers like camera, keypad,display etc.Interfacing with hardware.

Libraries:Provides JAVA - based set of libraries like libc, SQLite, SSL, Webkitweb browser engine and C/C++ libraries to facilitate interfacebuilding, graphics drawing, and database access.

Vishal Kumar Jaiswal (IIEST Shibpur) 5/ 29 June 29, 2016 5 / 29

Page 6: Basics of Android App Development

Libraries

Android Library

android.app:Provides access to application model, cornerstone for all apps.android.content:Facilitate content access, publishing, and messaging between,applications and application components,android.database:Used to access data from database also includes SQLite DBMSclasses,android.opengl:JAVA interface to the OpenGL 3D graphics rendering API,

Vishal Kumar Jaiswal (IIEST Shibpur) 6/ 29 June 29, 2016 6 / 29

Page 7: Basics of Android App Development

Libraries

Android Library[Contd....]

android.os:provides applications with access to standard operating systemservices like interprocess communication, system services,android.text:renders and manipulates text on a device display,android.view:building block of application user interface,android.widget:provides pre-built UI components viz. Buttons, labels, list views,radio buttons,android.webkit:allow web browsing to be built into applications.

Vishal Kumar Jaiswal (IIEST Shibpur) 7/ 29 June 29, 2016 7 / 29

Page 8: Basics of Android App Development

Runtime

Android Runtime

Provides Dalvik Virtual Machine which is a kind of JAVA virtualmachine specifically designed and optimized for Android.The Dalvik VM enables every Android applications to run its ownprocess, with its own instance of the Dalvik virtual machine.Write Android applications using JAVA programming language.

Vishal Kumar Jaiswal (IIEST Shibpur) 8/ 29 June 29, 2016 8 / 29

Page 9: Basics of Android App Development

Framework

Application Framework

Provides higher level services to applications in form of JAVA classes.Android developers are allowed to make use of these services in theirapplications.It includes:

Activity Manager: Controls all aspects of the application lifecycle.Content Providers: Allows applications to publish and share datawith other applications.Resource Manager: Provides access to color settings, UI layouts.Notification Manager: Allows applications to display alerts andnotifications to the user.View System: Used to create application UI.

Vishal Kumar Jaiswal (IIEST Shibpur) 9/ 29 June 29, 2016 9 / 29

Page 10: Basics of Android App Development

Components

Application components

Loosely coupled by the application manifest file AndroidManifest.xmlthat describes each component of the application and their interactionwith each other.

Activity,Service,Broadcast Receiver,Content Provider;

Vishal Kumar Jaiswal (IIEST Shibpur) 10/ 29 June 29, 2016 10 / 29

Page 11: Basics of Android App Development

Components

Activity

Vishal Kumar Jaiswal (IIEST Shibpur) 11/ 29 June 29, 2016 11 / 29

Page 12: Basics of Android App Development

Components

Activities

Represents a single screen with a UI.For example, an email application might have one activity thatshows a list of emails, another activity to compose an email, and yetanother activity for reading emails.Similar to main function in C/C++,onCreate will be called first ofall by Android system on launch of application.The activity class defines following callbacks i.e. events.The paused activity does not receive any user input and can notexecute any code,

Vishal Kumar Jaiswal (IIEST Shibpur) 12/ 29 June 29, 2016 12 / 29

Page 13: Basics of Android App Development

Components

Activity lifecycle

Vishal Kumar Jaiswal (IIEST Shibpur) 13/ 29 June 29, 2016 13 / 29

Page 14: Basics of Android App Development

Components

Activities[Contd..]

Every activity should be declared in AndroidManifest.xml file.The main activity should be listed with an intent-filter that includesthe MAIN action and LAUNCHER category. If any of this is notincluded, app icon will not appear in home screen’s list of apps.If an application has multiple activities, one activity should bemarked as the activity to be launched, when the application islaunched.

Vishal Kumar Jaiswal (IIEST Shibpur) 14/ 29 June 29, 2016 14 / 29

Page 15: Basics of Android App Development

Components

Services

Runs in the background to perform long running operations withoutneeding to interact with the user and it works even if application isdestroyed.For example, a service might play music in the background while theuser is in a different application or it might fetch data over thenetwork without blocking user interaction with an activity.Possible states: Started, Bound

Vishal Kumar Jaiswal (IIEST Shibpur) 15/ 29 June 29, 2016 15 / 29

Page 16: Basics of Android App Development

Components

Lifecycle of Services

Vishal Kumar Jaiswal (IIEST Shibpur) 16/ 29 June 29, 2016 16 / 29

Page 17: Basics of Android App Development

Components

Broadcast Receivers

Responds to broadcast messages from other applications or from thesystem,Also known as events or intents,For example, Application can initiate broadcasts to let otherapplications know that some data has been downloaded to thedevice and available for use. So, Broadcast Receiver will interceptthis communication and takes appropriate action.Create broadcast receiver and register it.

Vishal Kumar Jaiswal (IIEST Shibpur) 17/ 29 June 29, 2016 17 / 29

Page 18: Basics of Android App Development

Components

Content Providers

Supplies data from one application to others on request.Data may be stored in the filesystem, the database or somewhereelse.

Vishal Kumar Jaiswal (IIEST Shibpur) 18/ 29 June 29, 2016 18 / 29

Page 19: Basics of Android App Development

Components

Additional Components

The following components are used in construction of previously listedentities, their logic, and wiring between them. These components are :

Fragments: Represents a portion of user interface in an Activity,Views: UI elements that are drawn on-screen including buttons, listforms etc;Layouts: View hierarchies that control screen screen format andappearance of the views,Intents: Messages to any interested BroadcastReceiver on launch ofstartActivity to communicate with a broadcast service,Resources: External elements, such as drawable pictures, stringsetc;Manifest: Configuration file for the application.

Vishal Kumar Jaiswal (IIEST Shibpur) 19/ 29 June 29, 2016 19 / 29

Page 20: Basics of Android App Development

Components

Anatomy of Android application

src: Contains java source files of the project. By default, it containsMAinActivity.java file having an activity class that runs when yourapp is launched using the app icon.gen: Don’t modify it. Contains .R files that references all resourcesfound in your project.bin: Contains .apk file built by the ADT during the build processand everything else needed to run an android application.res/drawable/hdpi: Directory for drawable objects that aredesigned for high density screens.res/layout: Files defining app user interface.res/value: directory for XML files containing references toresources e.g. strings, color definitions.

Vishal Kumar Jaiswal (IIEST Shibpur) 20/ 29 June 29, 2016 20 / 29

Page 21: Basics of Android App Development

Components

Anatomy[Contd...]

AndroidManifest.xml: Describes the fundamental characteristics ofapp and it’s all components.MainActivity.java: Actual application file that gets converted toDalvik executable file and runs your application.strings.xml is located in the res/values folder and it contains all thetext that application uses. For example, the names of buttons,labels go into this file. Responsible for textual content.gen/com.example.helloworld/R.java works like glue between theactivity Java files like MainActivity.java and the resources likestrings.xml. This file will be automatically generated and don’tmodify it.The activity.xml is a layout file available in res/layout directory, thatspecifies UI of application.TextView is an Android content used to build the GUI.

Vishal Kumar Jaiswal (IIEST Shibpur) 21/ 29 June 29, 2016 21 / 29

Page 22: Basics of Android App Development

Components

Manifest.xml

Declare all components in this file. Works as interface betweenandroid OS and your application. If you don’t declare an elementhere, it will not be considered by your application.icon - points to the application icon available underres/drawable-hdpi. The application uses the image namediclauncher.png located in the drawable folder.name - specifies fully qualified class name of the Activity subclass.label - specifies a string to use as the label of the activity.You can define multiple activities using activity tags.

Vishal Kumar Jaiswal (IIEST Shibpur) 22/ 29 June 29, 2016 22 / 29

Page 23: Basics of Android App Development

Components

Manifest.xml [contd..]

The action for the intent filter is named android.intent.action.MAINto indicate that this activity serves as the entry point for theapplication.The category for the intent-filter is namedandroid.intent.category.LAUNCHER to indicate that application canbe launched from the device’s launcher icon.The @string refers to the strings.xml file. Hence, @string/appnamerefers to the appname string in the strings.xml, which is“HelloWorld”.The tags, permitted in manifest file, include: activity, service,receiver, provider.

Vishal Kumar Jaiswal (IIEST Shibpur) 23/ 29 June 29, 2016 23 / 29

Page 24: Basics of Android App Development

Components

All resources are maintained in res/ directory of your project.drawable: image viz .png, .jpg, .gif accessed from R.drawable class.layout: UI layout accessed from R.layout class. e.g. array, integer,boolean, color, dimensions, strings, styles.anim: animation , accessed from R.anim class.color: state list of color, accessed from R.color class.menu: option menu, context menu, sub menu accessed usingR.menu class.raw: To save arbitrary files.xml: XML and configuration files.On compilation of android application, a R class file gets generated,which lists resource IDs of all resources available in res/ directory.You can use R class to access that resource using sub-directory andresource name or directly resource ID.

Vishal Kumar Jaiswal (IIEST Shibpur) 24/ 29 June 29, 2016 24 / 29

Page 25: Basics of Android App Development

Components

Layout

View object represents rectangular area on screen and is responsiblefor drawing and event handling.Widgets viz. buttons, text fields etc. can be created with the help ofview class.Viewgroup is an invisible container of views that holds views anddefine their properties.Layouts are subclass of ViewGroup and defines the visual structureof an Android UI.Specify layout/view in terms of dp(density independent pixels).A view object will have unique id assigned to it. + symbol indicatesthis is new resource to be created and added.

Vishal Kumar Jaiswal (IIEST Shibpur) 25/ 29 June 29, 2016 25 / 29

Page 26: Basics of Android App Development

Components

Layout

Vishal Kumar Jaiswal (IIEST Shibpur) 26/ 29 June 29, 2016 26 / 29

Page 27: Basics of Android App Development

Components

Scroll View

Vishal Kumar Jaiswal (IIEST Shibpur) 27/ 29 June 29, 2016 27 / 29

Page 28: Basics of Android App Development

Components

UI controls and Event Handling

Handles button presses or screen touch.Android framework maintains event queue in FIFO basis.Listen these events and take appropriate actions.Define event listener, event listener registration, event handler.Create customn style using style tag inside resources tag.

Vishal Kumar Jaiswal (IIEST Shibpur) 28/ 29 June 29, 2016 28 / 29

Page 29: Basics of Android App Development

Components

Thank You

Vishal Kumar Jaiswal (IIEST Shibpur) 29/ 29 June 29, 2016 29 / 29