android sdk - lecture 2elf.cs.pub.ro/ndk/wiki/_media/lecture-2.pdf · android sdk, lecture 2 6/36....

Post on 31-Jul-2020

11 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Android SDKLecture 2

Android and Low-level Optimizations Summer School

15 July 2015

This work is licensed under the Creative Commons Attribution 4.0 International License. To view a copy of thislicense, visit http://creativecommons.org/licenses/by/4.0/.

Android SDK, Lecture 2 1/36

Applications

Activities

Services

Intents

Broadcast Receivers

Content Providers

Bibliography

Keywords

Android SDK, Lecture 2 2/36

Outline

Applications

Activities

Services

Intents

Broadcast Receivers

Content Providers

Bibliography

Keywords

Android SDK, Lecture 2 3/36

Android Manifest

I xml file

I In the root of an app’s directoryI Describes application components and resources

I Application name and Java package name (unique)I Activities, Services, Broadcast Receivers, Content ProvidersI Main(default) activityI PermissionsI LibrariesI Target/Minimum API level

Android SDK, Lecture 2 4/36

Permissions

I Request access to resources and APIs for the application

I Provide security through sandboxingI Declared in the Manifest

I <uses-permissionandroid:name="android.permission.INTERNET" />

I Given to an app at install time: all or nothing

I Cannot be granted afterwardsI Control who can access your components and resources

I Start activity, start/bind Services, send broadcasts, access datain Content Providers

I URI permissions

Android SDK, Lecture 2 5/36

Resources

I res/ directoryI Each resource type in a different subdirectory

I Specific nameI drawable/, layout/, values/, menu/, xml/, etc.

I Different configurations may require different resourcesI Bigger screen -> different layoutI Different language -> different stringsI Subdirectory for each alternative set of resourcesI <resources_name>-<config_qualifier>I drawable-hdpi/ for High Density ScreensI Resource chosen at runtime based on device configuration

I An ID is generated for each resource name in gen/

Android SDK, Lecture 2 6/36

Layouts

I Resources from res/layouts/

I Describe the UI of an activity or part of the UII res/layout/filename.xml

I filename is used as resource ID

I UI elementsI ViewGroups and Views

I Can be edited as xml or using graphical tools

I Easy to learn, hard to master

Android SDK, Lecture 2 7/36

Drawables

I Resources from res/drawables/

I Element that can be drawn on the screen

I Can be images (.png, .jpg, or .gif) or xmls

I xmls describe how an UI element reacts to input (pressed,focused)

I xmls point to images

I Visual feedback for interaction

Android SDK, Lecture 2 8/36

Outline

Applications

Activities

Services

Intents

Broadcast Receivers

Content Providers

Bibliography

Keywords

Android SDK, Lecture 2 9/36

Activity

I Application component

I User interface window, provide user interaction

I Require a layoutI Can only draw and change UI from the Looper thread

I Computationally intensive or wait based tasks on separatethreads

I An application may include multiple activitiesI Only one is the main activityI Activities can start each other -> the previous one is stoppedI Activity stack (”back stack”)I Back -> activity destroyed and previous one resumed

Android SDK, Lecture 2 10/36

Activity Lifecycle

Source: http://developer.android.com

Android SDK, Lecture 2 11/36

Activity Lifecycle

I Activities can be killed after onPause(), onStop() in lowmemory situations

I The activity state (objects) are lostI Can preserve state by saving objectsI User interaction can be saved and restoredI onSaveInstanceState() callback

I Save information in a Bundle

I onCreate(), onRestoreInstanceState()I Restore the activity state

I Threads can be stopped graciouslyI In onPause() threads should be signaled to stop

Android SDK, Lecture 2 12/36

Save Activity State

Source: http://developer.android.com

Android SDK, Lecture 2 13/36

Fragments

I Represent portions of UI in an ActivityI Can be combined to build a multi-pane UI

I Same code, different layout for phone / tablet

I Can be reused in multiple ActivitiesI Added or removed as needed while Activity is running

I FragmentTransactionsI FragmentManager used to add, replace and remove

Fragments at runtimeI separate back stack for Fragments within the Activity

Android SDK, Lecture 2 14/36

Fragment Lifecycle

Source: http://developer.android.com

Android SDK, Lecture 2 15/36

UI

I UI is a hierarchy of views

I View: rectangular space, provides user interaction

I Buttons, Lists, Images, TextViews, EditTextsI Callbacks for actions

I onTouch(), onClick(), onLongClick()

I A ViewGroup is a container for other Views or ViewGroups

I View / ViewGroup classes can be extended to create complexviews

I Adapters allows for more complex data types to be displayed

Android SDK, Lecture 2 16/36

Outline

Applications

Activities

Services

Intents

Broadcast Receivers

Content Providers

Bibliography

Keywords

Android SDK, Lecture 2 17/36

Services

I Perform operations in the background

I Do not provide a UI

I Continue to run even if another application is in foreground

I Able to perform network transactions, file I/O operations,interact with content providers, etc.

I Run in the main thread of the hosting processI A separate thread should be created if the service performs

CPU intensive or blocking operations

Android SDK, Lecture 2 18/36

Types of services

I StartedI An application component calls startService()I Performs a single operation, then stops itself and does not

return a result to the callerI Runs even if the caller component is destroyed

I BoundI An application component binds to it by calling

bindService()I Provides a client-server interface - send requests, return resultsI Runs as long as the application component is bound to itI Multiple components can bind to a service at onceI Service destroyed after all components unbind

Android SDK, Lecture 2 19/36

Service Life Cycle

Source: http://developer.android.com

Android SDK, Lecture 2 20/36

Outline

Applications

Activities

Services

Intents

Broadcast Receivers

Content Providers

Bibliography

Keywords

Android SDK, Lecture 2 21/36

Intent

I An object used for delivering a messageI Used for

I Starting an activityI Starting or binding a serviceI Delivering a broadcast message

I Includes component name, action and dataI Intent filters

I Declare the types of intents that a component can receiveI Specified in the manifest - <intent-filter>I <action>, <data>

Android SDK, Lecture 2 22/36

Types of Intents

I Explicit intentsI Specify exactly which component to start (the class name)I Typically used to start components in your own appI Will be delivered even if there is no intent filter declared

I Implicit intentsI Do not specify the exact componentI Declare a general action to be performedI The Android system finds the appropriate componentI Compares the intent to the intent filters in the manifest of the

apps

Android SDK, Lecture 2 23/36

Implicit Intents

Source: http://developer.android.com

Android SDK, Lecture 2 24/36

Outline

Applications

Activities

Services

Intents

Broadcast Receivers

Content Providers

Bibliography

Keywords

Android SDK, Lecture 2 25/36

Broadcast Receiver

I Responds to system-wide broadcast announcementsI The system generates many broadcasts

I Example: battery is low, screen has turned off, etc.

I Apps can generate broadcasts - send an announcement forother apps

I No UI, may create a notification in the status bar to alert theuser

I The receiver lets other components perform the work basedon the event

Android SDK, Lecture 2 26/36

Broadcast Receiver

I Each broadcast is delivered as an IntentI Intent passed to startBroadcast() or

startOrderedBroadcast()

I Local broadcasts using LocalBroadcastManagerI More efficientI Data does not leave the appI Other apps cannot send the broadcast - no security holes

I Register a receiver in two waysI Statically in the manifest using the <receiver> tagI Dynamically using Context.registerReceiver()

Android SDK, Lecture 2 27/36

Types of Broadcasts

I Normal broadcastsI Completely AsynchronousI All receivers run in an undefined order

I Ordered broadcastsI Delivered to one receiver at a timeI Each receiver executes and may propagate the result to the

next or abort the broadcastI The order is determined using the android:priority in the

<intent-filter> of the receiver

Android SDK, Lecture 2 28/36

Outline

Applications

Activities

Services

Intents

Broadcast Receivers

Content Providers

Bibliography

Keywords

Android SDK, Lecture 2 29/36

Content Provider

I Provides access to a repository of dataI To access a provider you have to request specific permissions

(in the manifest)I <uses-permission

android:name="android.permission.READ_USER_-

DICTIONARY">

I Two ways of storing dataI File data - audio, video, photosI Structured data - database, array, etc.

I Form compatible with tables of rows and columnsI Usually a SQLite database

Android SDK, Lecture 2 30/36

Content Provider

I Interface for accessing data in one process from anotherprocess

I Provider and provider clientI The application that owns the data includes the providerI The client application owns the provider client

I Access data using a ContentResolver client objectI Its methods provide CRUD (create, retrieve, update, delete)

functionsI Calls the methods with the same name in the ContentProvider

object

Android SDK, Lecture 2 31/36

Content URIs

I Identify data in the providerI Include a symbolic name for the provider (authority) and a

name for the table (path)I Example: content://user_dictionary/wordsI The ContentResolver uses the authority for identifying the

providerI From a system table with all known providersI The ContentResolver sends a query to the providerI The ContentProvider uses the path to identify the table

Android SDK, Lecture 2 32/36

Outline

Applications

Activities

Services

Intents

Broadcast Receivers

Content Providers

Bibliography

Keywords

Android SDK, Lecture 2 33/36

Outline

Applications

Activities

Services

Intents

Broadcast Receivers

Content Providers

Bibliography

Keywords

Android SDK, Lecture 2 35/36

Keywords

I Manifest file

I Permissions

I Resources

I Layouts

I Drawables

I Activity

I Service

I Intent

I Broadcast Receiver

I Content Provider

I Content URI

Android SDK, Lecture 2 36/36

top related