Android Jump Start ConFoo Mar 11, 2011 Mike Willbanks Blog: http://blog.digitalstruct.com Twitter: mwillbanks IRC: lubs on freenode
Outline ● Overview ● Building an App ● What is Android ● A First App ● Application Components ● And many other things ● Application Manifest ● Resources and UI ● The SDK
What is Android? “Android is a software stack for mobile devices that includes an operating system, middleware and key applications.”
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.
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.
Activities: Tasks and Back Stack
Activities: The Lifecycle
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
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.
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.
Services: The Lifecycle
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
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
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 youd open with Input Stream – values: Arrays, Colors, Dimensions, Strings, Styles – xml: configuration / searchable configuration
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, youll keep it simple but have adjustments for: Orientation, Size and Language.
Resources: An example layout ● Items in red are generic resources. ● Blue items are qualitative and if not matched fall back to generic.
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)
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.
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.
Resources: Layouts: Example
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 APIs as well. ● Windows users: You need to ensure you download the USB driver if you plan to use your phone for testing.
SDK: Navigating the SDK
Building An App
Our First Activity: Building the Project ● Before we begin anything we need to start an android project. I prefer ant, so creating this project is quick:
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
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 – Well 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
Our First Activity: Adding a Logo ● Lets take a confoo logo and put it at the top of the activity. ● Since there is no generic “drawable” resource well make one and store it there. ● We can then reference the image in our xml as @drawable/confoo ● The activitys layout is current set as “main.xml” in res/layout
Our First Activity: Adding a Logo
Our First Activity: Updating the Text ● So the canned text is currently hard coded and not using the res/values/strings.xml file. Lets 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 well make this change in our layout after we add the keys.
Our First Activity: Updating the Text
Adding A Menu
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.
Adding a Menu: res/menu/main.xml
Adding a Menu: The Activity
Adding a Menu: Incorporate WebKit ● To make this menu functional lets introduce a browser. ● Here we can use Intents to launch a specific URL in the browser.
Adding A TabHost
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. ● Lets put the same things that we have in the menu bar there for now. – But, we need to modify this as well. Lets 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.
Making a Browser Activity
Making a Browser Activity
The Tab Host
The Tab Host
Changes to the Manifest
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
Gotchas ● When using an AsyncTask ● Always ensure you check youre 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: AsyncTask + Progress Dialog
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.
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 :)
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. ● Were 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/
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
C2dm: The Manifest
C2dm: The Receiver
Questions? Mike Willbanks Blog : http://blog.digitalstruct.com Twitter : mwillbanks IRC : lubs on freenode