1. Android
Jump Start
ConFoo
Mar 11, 2011
Mike Willbanks
Blog: http://blog.digitalstruct.com
Twitter: mwillbanks
IRC: lubs on freenode
2. Outline
● Overview ● Building an App
● What is Android ● A First App
● Application Components ● And many other things
● Application Manifest
● Resources and UI
● The SDK
3. What is Android?
“Android is a software stack for mobile devices that includes an
operating system, middleware and key applications.”
5. Application Components
● Activities
● Representative of a single screen UI*.
● Broadcast Receivers
● A component that responds to systemwide broadcast
announcements.
● Services
● A component running in the background handling long
running operations and/or performing work for remote
processes.
● Content Providers
● A component that manages a shared set of application
data.
6. Activities
● Provides a screen where users can interact to do
“something”
● Generally correspond to a single screen in a UI
● Exceptions: Faceless or Floating Windows
● Think of it as a Controller in MVC
● However, it does include much of the View.
9. Intents
● Events / Messages
● The Application Glue
● Intents can be thought of as a hyperlink in a web
application.
● Launching new activities requires an Intent.
● Intents also describe what is to be done.
● Such as playing a video, making a call, etc.
● A good list can be found at http://openintents.org/
● IntentFilters determine which Activities can be
serviced via AndroidManifest.xml
10. Broadcast Receivers
● Components that respond to Broadcast Intents
● Examples include C2dm, Low Battery, Screen
Turned Off/On, Change in Timezone, Wifi Turned
Off/On, etc.
● Essentially an external event or an alarm
● You can also create and initiate your own Broadcast
Intents
● Intended to do a minimal amount of work
● Longer running processes should either launch or
utilize a Service component.
11. Services
● Components that run in the background
● No UI (faceless)
● Poll requests, Downloads, MP3 Playback
● Bound and Unbound
● Bound services are managed by an Activity
● Unbound services are expected to manage
themselves.
● Runs in the main thread, unless otherwise specified
● In the event of CPU intensive or blocking operations
create a new thread.
13. Content Providers
● The only way to share data between applications.
● You must ask for permission to these assets.
● Access to several types of data
● Alarm Clock, Browser, Contacts, Media (Music,
Video, Images), System Level Settings, User
Dictionary
● Each application can utilize exiting and define their
own.
● Generally utilized through SQLite
15. Application Manifest
● Every application MUST have an
ApplicationManifest.xml file.
● It tells Android about your application
● The package name
● Application components (Activities / Services /
Broadcast Receivers / Content Providers)
● Permissions required / requested
● Minimum and Target SDK versions
● Linked Libraries
19. Resources
● Resources are contained in the /res folder
● Resource Types:
– anim: Tween Animations
– color: State List of Colors
– drawable: Bitmaps aka Images
– layout: UI on Screen
– menu: Options Menu / Context Menu / Sub Menu
– raw: Files you'd open with Input Stream
– values: Arrays, Colors, Dimensions, Strings,
Styles
– xml: configuration / searchable configuration
20. Resources: The Qualifiers
● Basic resources are stored at the root folders
● Resources have qualifiers that allow you to customize
the experience which separate the folders by a dash.
● Localization (Country / Region / Language)
● Screen (Size / Touch / Orientation / Ratio / Density)
● Navigation (State / Method)
● Dock or Night mode
● API Version
● Generally, you'll keep it simple but have adjustments
for: Orientation, Size and Language.
21. Resources: An example layout
● Items in red are generic
resources.
● Blue items are qualitative and if
not matched fall back to generic.
22. Resources: Strings
● The string XML file defines a name (key) for your text
● Stylization is possible with some HTML elements
– Bold, Italic, Underline, Monospace, Superscript,
Subscript and Strikethrough (b, i, u, tt, big, small,
sup, sub, strike)
23. Resources: Layouts
● The Layout is the screen view of your application.
● There are several types of layouts:
● FrameLayout: The simplest layout
● LinearLayout: Aligns children in a single direction
● TableLayout: Most frustrating layout
● RelativeLayout: The most common layout
● There are several other specialty views available to
wrap the layouts
● ScrollView, TabHost, ViewFlipper, GridView, etc.
24. Resources: Layouts: Elements
● Contained in each layout is generally XML defining
each type of element of widget you want to use.
● From a high level view these include
– Button, Checkbox, DatePicker, EditText,
ImageView, ListView, TextView
● You can find all of these in the android.widget
package.
27. SDK: Overview
● The SDK contains everything you need to build an
android application, however, to aide you they also
made an eclipse plugin ADT.
● After installing the SDK, I recommend you install the
eclipse plugin – it will save you time later.
● Launch the SDK and install all available packages
● This is useful for testing the emulator with different
versions as well as ensuring you have the Google
API's as well.
● Windows users: You need to ensure you download the
USB driver if you plan to use your phone for testing.
31. Our First Activity: Installing the Project
● We now have an application, that is installable.
● If our emulator is running – we can just run:
– ant install
● Note: If you are using an actual device and
you are on linux, you must start the “adb”
server as root: sudo adb startserver
32. Our First Activity: Changing the Theme and Icon
● I am not a fan of the black background and how the
interface looks, luckily android supports themes!
● Canned Themes can be found in the sdk folder:
platforms/androidver/data/res/values/themes.xml
– We'll use Theme.Light
● Themes can be applied to a whole application, an
activity or an element.
– We will change it on the whole application, which
simply requires a change to the
AndroidManifest.xml file.
● Want to make your own theme?
– Extend one of the ones already made.
34. Our First Activity: Adding a Logo
● Let's take a confoo logo and put it at the top of the
activity.
● Since there is no generic “drawable” resource we'll
make one and store it there.
● We can then reference the image in our xml as
@drawable/confoo
● The activity's layout is current set as “main.xml” in
res/layout
36. Our First Activity: Updating the Text
● So the canned text is currently hard coded and not
using the res/values/strings.xml file. Let's change that
and update it. While we are there we will update the
application name.
● Referencing strings in our application is as easy as
@string/name we'll make this change in our layout
after we add the keys.
39. Adding a Menu: Using res/menu
● To build a menu, we simply need to add an xml file to
res/menu and incorporate it into our code.
● Using the stock icons you can find under the
androidsdkdir/platforms/androidver/res/drawable
*
● We also need to add in code to our activity to make
this menu load.
● We will add in a menu item for the Schedule,
Joind.in and Twitter.
42. Adding a Menu: Incorporate WebKit
● To make this menu functional let's introduce a browser.
● Here we can use Intents to launch a specific URL in the
browser.
44. Adding a TabHost
● A TabHost allows us to have several open actions at
once and keep the state of them during that time.
● It also lets us navigate the app slightly easier.
● Let's put the same things that we have in the menu
bar there for now.
– But, we need to modify this as well. Let's use
webkit in our own internal browser.
● This also means we now need to create a tabhost
activity and change the manifest to go to the tab host
activity first.
51. Signing your Application
● First step in publishing is generating a private key:
keytool -genkey -v -keystore my-release-key.keystore
-alias alias_name -keyalg RSA -keysize 2048 -validity
10000
● Secondly, if you are using ant and want to automate
release builds modify local.properties with your
passphrase:
● Now, let it rip: ant release
● Now it is off to market.android.com/publish
53. Gotchas
● When using an AsyncTask
● Always ensure you check you're still in the view you
need. Crashing issues apply.
● When using a Progress Dialog
● Ensure it still exists and that it is showing – never
assume it is still existing.
● Some code to give you a better idea:
55. Gotchas
● If your configuration changes (Orientation Change),
ensure that you stated it in your AndroidManifest as
well as your Activity to handle things such as Progress
Dialogs
● Android by default will recreate the view and
activity.
57. Gotchas
● If your server utilizes tokens or some form of sessions
● When you close the application, it will remain in a
background process until android needs memory.
Ensure that you handle this – users become
unhappy otherwise :)
● Before you use the internet make sure that it does
exist :)
58. C2dm
● Cloud 2 Device Messaging works basically the same
as Apple Push Notification Service, except far more
flexible.
● Your application registers a broadcast receiver and
fires off an intent to register the device. You receive
back an ID that you subsequently store on your own
server.
● We're talking about how to handle this in your app –
the web server part is far more complex:
● See:
http://blog.digitalstruct.com/2010/11/21/android
c2dmwithphpandzendframework/
59. C2dm
● What needs to happen
● Ask for permissions in the manifest, publish a
broadcast receiver and ask google for the
registration ID
● Before that although, you do need to sign up:
http://code.google.com/android/c2dm/index.html