android jump start
Post on 28-Jan-2015
125 Views
Preview:
DESCRIPTION
TRANSCRIPT
AndroidJump Start
ConFooMar 11, 2011
Mike Willbanks Blog: http://blog.digitalstruct.comTwitter: mwillbanks IRC: lubs on freenode
● Overview● What is Android● Application Components● Application Manifest● Resources and UI● The SDK
Outline
● Building an App● A First App● And many other things
“Android is a software stack for mobile devices that includes an operating system, middleware and key applications.”
What is Android?
ApplicationComponents
● 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 longrunning operations and/or performing work for remote processes.
● Content Providers● A component that manages a shared set of application
data.
Application Components
● 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
Activities: Tasks and Back Stack
Activities: The Lifecycle
● 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
● 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
● 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
Services: The Lifecycle
● 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
ApplicationManifest
● 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
Application Manifest: An Example
● action
● activity
● activityalias
● application
● category
● data
● granturipermission
● instrumentation
● intentfilter
● manifest
● metadata
● permission
Application Manifest: Legal Elements
● permissiongroup
● permissiontree
● provider
● receiver
● service
● supportsscreens
● usesconfiguration
● usesfeature
● useslibrary
● usespermission
● usessdk
ResourcesandUI
● 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
Resources
● 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
Resources: An example layout
● Items in red are generic resources.
● Blue items are qualitative and if not matched fall back to generic.
● 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
● 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
● 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
Resources: Layouts: Example
SDK
● 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
SDK: Navigating the SDK
BuildingAn
App
● 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
● 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: Installing the Project
● 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.
Our First Activity: Changing the Theme and Icon
Our First Activity: Changing the Theme
● 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
Our First Activity: Adding a Logo
● 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
Our First Activity: Updating the Text
AddingA
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: Using res/menu
Adding a Menu: res/menu/main.xml
Adding a Menu: The Activity
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.
AddingA
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.
Adding a TabHost
Making a Browser Activity
Making a Browser Activity
The Tab Host
The Tab Host
Changes to the Manifest
Publishing
● 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
AdvancedStuff
● 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
Gotchas: AsyncTask + Progress Dialog
● 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
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 :)
Gotchas
● 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/androidc2dmwithphpandzendframework/
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
C2dm: The Manifest
C2dm: The Receiver
More Examples....
Questions?
Mike Willbanks Blog : http://blog.digitalstruct.com Twitter : mwillbanks IRC : lubs on freenode
top related