Top Banner
24
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Android
Page 2: Introduction to Android

CCNPO DevTeam

Today’s Manifest

● An app has several interfaces to interact with

the Android system.

● Basic building blocks we’d discusso Activity

o Context

o Intent

o Service

o Broadcast and BroadcastReceiver

o Storage

Page 3: Introduction to Android

CCNPO DevTeam

Activity

● Application component which interacts with

the user

● Interaction happens via the Android UIo i.e. the screen

● An app usually starts with a default Activity

● It can ask Android to start new Activities

http://developer.android.com/guide/components/activities.html

Page 4: Introduction to Android

CCNPO DevTeam

Context

● The interface to the environment of the

application.

● Allows access to resourceso R’s resources, System resources (e.g. Content

providers)

● Allows calling new activitieso new Activities, Services

● Brings in new requestso Broadcasts

Page 5: Introduction to Android

CCNPO DevTeam

Intent

● A messaging object which can be used to

request action from another application

component

● Can be used too Start Activity

o Start Service

o Deliver a broadcast

Page 6: Introduction to Android

CCNPO DevTeam

Intent

● Two typeso Explicit

o Implicit

● Contains >>

Intent

Component Name

Action

Data

Category

Extras

Flags

Page 7: Introduction to Android

CCNPO DevTeam

Explicit Intent

● Specifies which component to use / starto Starting new Activity in the app

o Starting new Service

Intent mainShowIntent = new Intent(

getApplicationContext(), ScreenMain.class);

startActivity(mainShowIntent);

Page 8: Introduction to Android

CCNPO DevTeam

Implicit Intent

● Declares a general action

to performo Send SMS, email

o Share something

● When the intent is

delivered to Android

System, it either decides

which app to use, or asks

the user if there are

multiple

Page 9: Introduction to Android

CCNPO DevTeam

Service

● Application component which runs in the

background

● Continues to run even if the user switches to

another app.

● Can be started from an Activity using an

Intent

What do you

mean what are

they?

I just told you!

Page 10: Introduction to Android

CCNPO DevTeam

Broadcast

● System-wide message sent to apps

● Broadcast contains an Intent and a Context

it was fired.

● Two types

o Un-Ordered (Normal)

o Ordered

Page 11: Introduction to Android

CCNPO DevTeam

Broadcast Receiver

● Listens to a broadcast

● The broadcast can be triggered by the

system, another app and the same app.

● Application can opt-in to listen using an

Intent Filter specified in;o Manifest entry

o By creating a runtime object

Page 12: Introduction to Android

CCNPO DevTeam

Storage

● Google lists the following storage optionso Internal Storage

o External Storage

o Shared Preferences

o Database

o Network

Page 13: Introduction to Android

CCNPO DevTeam

Storage - Internal Storage

● Private to the application

● Read / Write as a File

● Stored in a safe app-limited location

● Deleted when the app is uninstalled

Page 14: Introduction to Android

CCNPO DevTeam

Storage - External Storage

● Stores data on the external disk

● Can be SD card / USB

● Use with care. The user could

o Un-mount the volume

o Connect the card to a PC

Which would make this unavailable for

app’s usage

Page 15: Introduction to Android

CCNPO DevTeam

Storage - Shared Preferences

● App-limited data space

● Uses key-value pairs

● Only primitive data types can be stored

● Great for small persistent values in app

Note: Settings has a separate set of functions to make

things easy. Don’t manually store user’s preferences

Page 16: Introduction to Android

CCNPO DevTeam

Storage - Database

● Android comes with a sqlite database

● Apps have their own databases

● Simple queries can be executed

Page 17: Introduction to Android

CCNPO DevTeam

Storage - Network

● Use the internet / Wifi to send data to a

server

● How? Anyway you like

o JSON

o XML

o HTTP GET, POST

o or whatever method you want… just decide…

Page 18: Introduction to Android
Page 19: Introduction to Android

CCNPO DevTeam

Code Demo

Simple app to:

● Record the battery level every 10s

● View the current level

● View recorded values

● Share recorded values

Page 20: Introduction to Android

CCNPO DevTeam

Stage 1

● Setup the Main Activity

● Setup the Serviceo Code the required startup and looping in the service

o Code the broadcast to send

● Start the service from the activity

● Register for broadcasts from the service

● See the initial results

Page 21: Introduction to Android

CCNPO DevTeam

Stage 2

● Setup a BroadcastReceiver and add it in the

manifest

● Use that class to record the samples

● Create an additional Activity to view samples

Page 22: Introduction to Android

CCNPO DevTeam

Stage 3

● Configure a share menu item

● Share using intents

Page 23: Introduction to Android

CCNPO DevTeam

Finalized Code

Code is available at

https://github.com/tdevinda/DialogAndroidIntro_2.git

Page 24: Introduction to Android

Thanks for your participation!