63
 Android Jump Start ConFoo Mar 11, 2011 Mike Willbanks    Blog: http://blog.digitalstruct.com Twitter: mwillbanks      IRC: lubs on freenode

Android Jump Start

  • Upload
    confoo

  • View
    123

  • Download
    4

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Android Jump Start

   

AndroidJump Start

ConFooMar 11, 2011

Mike Willbanks   Blog: http://blog.digitalstruct.comTwitter: mwillbanks     IRC: lubs on freenode

Page 2: Android Jump Start

   

● Overview● What is Android● Application Components● Application Manifest● Resources and UI● The SDK

Outline

● Building an App● A First App● And many other things

Page 3: Android Jump Start

   

“Android is a software stack for mobile devices that includes an operating system, middleware and key applications.”

What is Android?

Page 4: Android Jump Start

   

ApplicationComponents

Page 5: Android Jump Start

   

● Activities● Representative of a single screen UI*.

● Broadcast Receivers● A component that responds to system­wide 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.

Application Components

Page 6: Android Jump Start

   

● 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.

Activities

Page 7: Android Jump Start

   

Activities: Tasks and Back Stack

Page 8: Android Jump Start

   

Activities: The Lifecycle

Page 9: Android Jump Start

   

● 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

Intents

Page 10: Android Jump Start

   

● 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.

Broadcast Receivers

Page 11: Android Jump Start

   

● 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.

Services

Page 12: Android Jump Start

   

Services: The Lifecycle

Page 13: Android Jump Start

   

● 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

Content Providers

Page 14: Android Jump Start

   

ApplicationManifest

Page 15: Android Jump Start

   

● 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

Application Manifest

Page 16: Android Jump Start

   

Application Manifest: An Example

Page 17: Android Jump Start

   

● action

● activity

● activity­alias

● application

● category

● data

● grant­uri­permission

● instrumentation

● intent­filter

● manifest

● meta­data

● permission

Application Manifest: Legal Elements

● permission­group

● permission­tree

● provider

● receiver

● service

● supports­screens

● uses­configuration

● uses­feature

● uses­library

● uses­permission

● uses­sdk

Page 18: Android Jump Start

   

ResourcesandUI

Page 19: Android Jump Start

   

● 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 / search­able configuration

Resources

Page 20: Android Jump Start

   

● 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.

Resources: The Qualifiers

Page 21: Android Jump Start

   

Resources: An example layout

● Items in red are generic resources.

● Blue items are qualitative and if not matched fall back to generic.

Page 22: Android Jump Start

   

● 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)

Resources: Strings

Page 23: Android Jump Start

   

● 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.

Resources: Layouts

Page 24: Android Jump Start

   

● 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.

Resources: Layouts: Elements

Page 25: Android Jump Start

   

Resources: Layouts: Example

Page 26: Android Jump Start

   

SDK

Page 27: Android Jump Start

   

● 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.

SDK: Overview

Page 28: Android Jump Start

   

SDK: Navigating the SDK

Page 29: Android Jump Start

   

BuildingAn

App

Page 30: Android Jump Start

   

● Before we begin anything we need to start an android project.  I prefer ant, so creating this project is quick:

Our First Activity: Building the Project

Page 31: Android Jump Start

   

● 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 start­server

Our First Activity: Installing the Project

Page 32: Android Jump Start

   

● 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/android­ver/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.

Our First Activity: Changing the Theme and Icon

Page 33: Android Jump Start

   

Our First Activity: Changing the Theme

Page 34: Android Jump Start

   

● 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

Our First Activity: Adding a Logo

Page 35: Android Jump Start

   

Our First Activity: Adding a Logo

Page 36: Android Jump Start

   

● 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.

Our First Activity: Updating the Text

Page 37: Android Jump Start

   

Our First Activity: Updating the Text

Page 38: Android Jump Start

   

AddingA

Menu

Page 39: Android Jump Start

   

● 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 android­sdk­dir/platforms/android­ver/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.

Adding a Menu: Using res/menu

Page 40: Android Jump Start

   

Adding a Menu: res/menu/main.xml

Page 41: Android Jump Start

   

Adding a Menu: The Activity

Page 42: Android Jump Start

   

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.

Page 43: Android Jump Start

   

AddingA

TabHost

Page 44: Android Jump Start

   

● 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.

Adding a TabHost

Page 45: Android Jump Start

   

Making a Browser Activity

Page 46: Android Jump Start

   

Making a Browser Activity

Page 47: Android Jump Start

   

The Tab Host

Page 48: Android Jump Start

   

The Tab Host

Page 49: Android Jump Start

   

Changes to the Manifest

Page 50: Android Jump Start

   

Publishing

Page 51: Android Jump Start

   

● 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

Signing your Application

Page 52: Android Jump Start

   

AdvancedStuff

Page 53: Android Jump Start

   

● 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:

Gotchas

Page 54: Android Jump Start

   

Gotchas: AsyncTask + Progress Dialog

Page 55: Android Jump Start

   

● 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 re­create the view and activity.

Gotchas

Page 56: Android Jump Start

   

Gotchas

Page 57: Android Jump Start

   

● 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 :)

Gotchas

Page 58: Android Jump Start

   

● 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­c2dm­with­php­and­zend­framework/

C2dm

Page 59: Android Jump Start

   

● 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

C2dm

Page 60: Android Jump Start

   

C2dm: The Manifest

Page 61: Android Jump Start

   

C2dm: The Receiver

Page 62: Android Jump Start

   

More Examples....

Page 63: Android Jump Start

   

Questions?

Mike Willbanks Blog : http://blog.digitalstruct.com Twitter : mwillbanks IRC : lubs on freenode