intro to android development
DESCRIPTION
Intro to Android Development. Written by Keren Kalif, Edited by Liron Blecher Contains slides from Google I/O presentation. Content. Android development environment Android project structure Example with threads Example with networking. Android Development Tools. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/1.jpg)
© Keren Kalif
Intro to Android DevelopmentWritten by Keren Kalif, Edited by Liron Blecher
Contains slides from Google I/O presentation
![Page 2: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/2.jpg)
Content
• Android development environment• Android project structure• Example with threads• Example with networking
2
![Page 3: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/3.jpg)
Android Development Tools
The Android Development Tools (ADT) is a collection of classes and utilities needed to develop for Android
It’s basically Eclipse + Android SDK + Android Plugin for Eclipse
Download the SDK from: http://developer.android.com
Extract the zip file into a folder on your hard drive
3
![Page 4: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/4.jpg)
Android Emulator
The Android emulator is a software that runs the Android OS on your local (Windows) OS.
Go into the Android SDK directory and run the program Manager.exe
The Manager will enable you to create Android Virtual Devices on your system.
4
![Page 5: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/5.jpg)
Android Virtual Device Manager
5
![Page 6: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/6.jpg)
Android Virtual Device Manager – cont.Name of the device
Android Version
Memory on the device
Screen Type (affects resolution)
Additional Hardware
6
![Page 7: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/7.jpg)
Running the Emulator
7
![Page 8: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/8.jpg)
Create a new Android Project
Go to File New Project Android Project
Project Name
Android OS Version
Application Name
Package Name (must have at least two levels)
Startup Activity
8
![Page 9: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/9.jpg)
The created projectThis is the top
“frame” of the project
9
![Page 10: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/10.jpg)
What is an Android Application
An Android application is actually a collection of several components, each defined in AndroidManifest.xml
10
![Page 11: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/11.jpg)
Anatomy of an AppAnatomy of an App
• Activity• Service• Content Provider• Broadcast Receiver• Intents• Manifest
11
![Page 12: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/12.jpg)
Anatomy of an App - Activity
• A single screen with a user interface• Independent but work together to form a
cohesive whole• Possible to invoke from other
applications• Extends the Activity class
12
![Page 13: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/13.jpg)
Anatomy of an App - Service
• Perform long-running operations in the background• Does not provide a user interface• Other components can bind/interact• Extends the Service class
13
![Page 14: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/14.jpg)
Anatomy of an App - Content provider
• Manages a shared set of application data• Consistent interface to retrieve/store data
o RESTful modelo CRUD operations
• Can be backed by different storeso e.g. File System, SQLite DB, Web
• Can expose your data to other applications• Can consumer data from other Content Providers
o e.g. Contacts or Call Log• Extend the ContentProvider class
14
![Page 15: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/15.jpg)
Anatomy of an App - Broadcast receiver
• Respond to system wide messages• Messages can be initiated by the system or an app• 2 type of broadcast:
o Normal - delivered async to all receiverso Ordered - delivered in priority order & can be aborted
• Can programatically register or statically register via the manifest
• Should be very light, pass any work onto a Service• Extend the BroadcastReceiver class
15
![Page 16: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/16.jpg)
Anatomy of an App - Intents
• Intents are the messages that link app components together
• Explicit / implicit• An abstract description of an operation to be performed
o An Action to be performedo The Data to operate upono Extra metadata
• Standardise on a common vocabulary of Actionso e.g. 'View', 'Edit', 'Send'
• Apps register their ability to handle Actions for a given data type via IntentFilter
16
![Page 17: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/17.jpg)
Anatomy of an App - Intents
• Publish an 'Intent API'o Specify Action & Extras to invoke your component
17
![Page 18: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/18.jpg)
• Achieve complex tasks by calling other Application's Intents, e.g. scanning a barcode
Anatomy of an App - Intents
18
![Page 19: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/19.jpg)
Anatomy of an App - Activity lifecycle
• Running in a multitasking environment• Users switch apps, calls come in,
system runs low on memory• System invokes callbacks in your app• The system will kill your app• Be sure to save state!
19
![Page 20: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/20.jpg)
Activity created
Anatomy of an App - Activity lifecycle
20
![Page 21: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/21.jpg)
onCreate()
Activity created
onResume()
Activity running
Anatomy of an App - Activity lifecycle
21
![Page 22: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/22.jpg)
onPause()
Call comes in
onResume()
Activity running
Anatomy of an App - Activity lifecycle
22
![Page 23: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/23.jpg)
onPause()
Call comes in
onResume()
Activity running
Return to app
Anatomy of an App - Activity lifecycle
23
![Page 24: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/24.jpg)
onPause()
Call comes in
onResume()
Activity running
Return to app
Low Memory
Activity destroyed
Anatomy of an App - Activity lifecycle
24
![Page 25: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/25.jpg)
Resources
In Android, anything that is not pure code is written in a separate resource file (not a java file), for example strings, images, etc.
This separation enables easier multi-language support since only the resource file needs to be changed – not the code itself
Resource files are saved as XML file and the Resource Complier generates a Java file which reference the data in these XML files.
The generated Java file is called R.java
Using this file you can access the data in java code
25
![Page 26: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/26.jpg)
Resources – cont.
Resources live in the res folder
Qualifiers provide specific resources for different device configuration
e.g. layout-land, drawable-hdpi
Resources IDs automatically generated in R.java
e.g. R.layout.main
26
![Page 27: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/27.jpg)
Resources – cont.
When creating UI elements in Android, the components and their layout are saved in an XML file
The Android compiler generates the R.java file under the “gen” folder
Whenever a new resource is changed, the R file will be re-generted
![Page 28: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/28.jpg)
(Resources) משאבים
![Page 29: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/29.jpg)
main.xml contains the layout in a form of XML declaration.
It will be used in setContentView
main.xml
![Page 30: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/30.jpg)
Resources – cont.
![Page 31: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/31.jpg)
Resources – cont.
main.xml defines all the components that will be displayed in the activity
Each view will have its own ID in order to be able to access it directly
strings.xml defines all the strings in the system
31
![Page 32: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/32.jpg)
Views
In Android, all UI elements are Views (or inherit from the View class) – it’s kind of like JComponent in Swing.
The Activity class has a method called setContentView which sets the View that will be displayed in the Activity.
The XML file is translated into a View instance
32
![Page 33: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/33.jpg)
Permissions
Each Android application must declare what external actions/resources its going to access (GPS, address book, phone, etc)
Required permissions are declared in the manifest.xml file.
To be able to access the internet:
AndroidManifest.xml:• To allow an Android application access to the internet, add the
following tag under the permissions tag:
<uses-permission android:name="android.permission.INTERNET" />
33
![Page 34: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/34.jpg)
Default Activity
You can define the default Activity in the manifest.xml file
34
![Page 35: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/35.jpg)
Accessing localhost of the host machine
Since the Android emulator runs as a virtual machine, trying to access localhost or 127.0.0.1 will direct you to the Android VM, and not your host machine (your computer)
To access your host machine from within the Android VM, use the IP address: 10.0.2.2 (example: http://10.0.2.2:8084/chat)
35
![Page 36: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/36.jpg)
Toast - Popups replacement
To show a popup use: Toast.makeToast(…).show()
36
![Page 37: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/37.jpg)
Switching to another Activity
Each Activity is like a card with a specific logic to handle.
To switch to another Activity (for example, when a user click a login screen):
Intent myIntent = new Intent(view.getContext(), Activity2.class);startActivityForResult(myIntent, 0);
37
![Page 38: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/38.jpg)
Installing an Android Application
Under the “dist” directory in your project there will be a *.apk file (which is like a war file or jar file = zip file with a different extension)
Send this file as an email attachment and that’s it!
38
![Page 39: Intro to Android Development](https://reader036.vdocument.in/reader036/viewer/2022062521/56816935550346895de08fd6/html5/thumbnails/39.jpg)
Links
• Intro: http://www.io-bootcamp.com/sessions#TOC-Android
• http://developer.android.com/sdk/installing.html • http://developer.android.com/guide/index.html• http://www.vogella.de/articles/Android/article.html• http://thenewboston.org/tutorials.php• http://www.androidhive.info/• http
://blog.js-development.com/2009/10/accessing-host-machine-from-your.html
39