Android App Basics Slides from developer.android.com and adopted from Janzen

Embed Size (px)

DESCRIPTION

Android Architecture  A set of C/C++ libraries exposed to developers through the application framework  System C library - a BSD-derived implementation of the standard C system library (libc), tuned for embedded Linux-based devices  Media Libraries - based on PacketVideo's OpenCORE; the libraries support playback and recording of many popular audio and video formats, as well as static image files, including MPEG4, H.264, MP3, AAC, AMR, JPG, and PNG 3

Citation preview

Android App Basics Slides from developer.android.com and adopted from Janzen Android Architecture Android built on Linux 2.6 kernel What is a kernel? Provides security, memory management, process management, network stack, and driver model Abstraction layer between hardware and the software stack 2 Android Architecture A set of C/C++ libraries exposed to developers through the application framework System C library - a BSD-derived implementation of the standard C system library (libc), tuned for embedded Linux-based devices Media Libraries - based on PacketVideo's OpenCORE; the libraries support playback and recording of many popular audio and video formats, as well as static image files, including MPEG4, H.264, MP3, AAC, AMR, JPG, and PNG 3 Android Architecture A set of C/C++ libraries exposed to developers through the application framework Surface Manager - manages access to the display subsystem and seamlessly composites 2D and 3D graphic layers from multiple applications FreeType - bitmap and vector font rendering 4 Android Architecture A set of C/C++ libraries exposed to developers through the application framework LibWebCore - a modern web browser engine which powers both the Android browser and an embeddable web view SGL - the underlying 2D graphics engine 5 Android Architecture A set of C/C++ libraries exposed to developers through the application framework 3D libraries - an implementation based on OpenGL ES 1.0 APIs; the libraries use either hardware 3D acceleration (where available) or the included, highly optimized 3D software rasterizer SQLite - a powerful and lightweight relational database engine available to all applications 6 Android Architecture Runtime libraries provide most of the functionality in the core Java libraries Each app has a process and an instance of Dalvik VM Uses Dalvik Executable (.dex) format low memory footprint VM is register based runs compiled Java classes The linux kernel provides Dalvik with needed functionality 7 Android Architecture Named for Dalvik in Iceland, a fishing village where ancestors of Dan Bornstein lived. Dalvik is discontinued process virtual machine as of Android version 4.4 (KitKat) The successor of Dalvik is Android Runtime (ART), which uses the same bytecode and.dex files, aiming at performance I improvements transparent to end users. 8 Android Architecture The framework allows developers to build cool apps 9 Android Architecture Set of services and systems Views to build apps: lists, grids, text boxes, buttons, browser Content providers enable apps to share data between each other Resource manager for access to non-code resources such as localized strings, graphics, and layout files A Notification manager that enables all applications to display custom alerts in the status bar Activity manager manages lifecycle of apps and provides a common navigation backstack 10 Views Basic block for a UI component, occupying a rectangle on the screen It draws and handles events Widgets interactive UI components Buttons, text fields, spinners, date picker, map,etc. 11 Views ViewGroup ways to group other views Layouts invisible containers holding views and define properties 12 Android Architecture Core applications ship with android. 13 Resources Value Strings, colors, dimensions, arrays (strings, integers, typed), bool, ID, Drawable something drawn to the screen Bitmaps, NinePatch (stretchable) images PNG is preferred, JPG, and GIF supported Layout Layouts define the look of your activity, can specify in XML or with code, XML often easier Animations Property modify objects property values over time Tween rotate, move, stretch, and fade a view Frame to display a sequence of images Color State List change color based on state Example: button is pressed, focused, or neither, define color for each Menu defines app menu (options, context, submenu, ) that can be inflated Style defines the format and look of a UI for view or activity 14 Application Fundamentals Written in Java, using Android SDK to compile into an Android Package Each app lives in its own security sandbox (principle of least privilege: only access to what it needs) Android OS is multi-user with each app a user Each app gets a user ID Can be shared between apps (also share same process and VM) Each process has own VM runs in isolation Each app has its own process started for any component Can request permission to access device data (SMS, camera) Granted at install time 15 Application Components Intents and Intent Filters Activities Services Content Providers Broadcast Receivers App Widgets Processes and Threads 16 Intents and Intent Filters Intents and Intent Filters An Intent is a messaging object you can use to request an action from another app component. Intents (an asynchronous message) are used to start components (except content providers) Content providers are activated when targeted by a request from a ContentResolver. Call query() on a ContentResolver to query a content Provider Start Activity - An Activity represents a single screen in an app. You can start a new instance of an Activity by passing an Intent to startActivity(). Start Service - A Service is a component that performs operations in the background without a user interface. You can start a service to perform a one- time operation (such as download a file) by passing an Intent to startService(). Deliver Broadcast - A broadcast is a message that any app can receive. The system delivers various broadcasts for system events, such as when the system boots up or the device starts charging. You can deliver a broadcast to other apps by passing an Intent to sendBroadcast(), sendOrderedBroadcast(), or sendStickyBroadcast(). An Intent filter specifies the types of intents that an activity, service, or broadcast receiver can respond to. An intent filter declares the capabilities of its parent component. 17 Activities Activities A single screen with a user interface Example text message screen, inbox, compose, etc. They are independent An app may have more than one Typically, one activity in an application is specified as the "main" activity, which is presented to the user when launching the application for the first time. Can be started by other applications (if allowed) 18 Activities Activities Fragments - A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. Loaders They make it easy to asynchronously load data in an activity or fragment. They are available to every Activity and Fragment. Monitor the source of their data and deliver new results when the content changes Tasks and Back Stack - A task is a collection of activities that users interact with when performing a certain job. The activities are arranged in a stack (the "back stack"), in the order in which each activity is opened. When the user presses the Back button, the current activity is popped from the top of the stack (the activity is destroyed) and the previous activity resumes (the previous state of its UI is restored). Activities in the stack are never rearranged, only pushed and popped from the stack 19 Services Services Runs in the background for long-running operations or remote processes Has no user interface Example play music, fetch data An activity can start a service, or bind to a service for interaction. 20 Content Providers Content Providers Manages a shared set of application data Stored in the file system, an SQLite database, on the web, any other persistent location you have access to Other apps can query or modify data (if allowed) Example: contact information Can also be used for private app data 21 Broadcast Receivers Broadcast Receivers Responds to system-wide broadcasts Generally originate from system, i.e. low battery, screen off, picture taken. Apps can also broadcast, i.e. data ready for use No user interface but may create a status bar notification Generally a gateway to other components and light weight i.e. start a service to perform work based on an event Any application can start another applications component Will start a process for the components app, not run in yours Means no single entry point (i.e main()) You cant start it, need to have system deliver a message with your intent to start a component Makes it much easier to add functionality to your app 22 App Widgets App Widgets are miniature application views that can be embedded in other applications (such as the Home screen) and receive periodic updates. These views are referred to as Widgets in the user interface, and you can publish one with an App Widget provider. 23 Processes and Threads When an application component starts and the application does not have any other components running, the Android system starts a new Linux process for the application with a single thread of execution. By default, all components of the same application run in the same process and thread (called the "main" thread). If an application component starts and there already exists a process for that application (because another component from the application exists), then the component is started within that process and uses the same thread of execution. You can arrange for different components in your application to run in separate processes, and you can create additional threads for any process. 24 Manifest Each app has an AndroidManifest.xml file Declare all the components in this file In root of app project directory Identify user permissions required by app (ie internet access, rw contacts,) Declare minimum API level required Declare hardware and software features used or required i.e camera, bluetooth services, camera, multitouch screen API libraries needed by app for linking And more 25 An example Adapted from Janzen 26 A First Example: Advent Devotions 27 UML Class Diagram Generated by Android External Activity 28 Two Activities in Advent Devotions AdventDevos displays the calendar of dates Devo displays a single devotion Intent myIntent = new Intent(AdventDevos.this, Devo.class); myIntent.putExtra("ButtonNum", "" + index); startActivity(myIntent); 29 Two Activities in Advent Devotions AdventDevos displays the calendar of dates Devo displays a single devotion Bundle extras = getIntent().getExtras(); String value = extras.getString("ButtonNum"); Integer buttonNum = Integer.valueOf(value); 30 Launching an Intent you didnt write Devos has button to URL Browser launched Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.biblegateway.com/passage/?search="+ passage +"&version=NIV")); startActivity(i); 31 Android Activity An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View).Activity.html#ActivityLifecycle 32 Android Manifest.xml