Android Tutorial
Android Tutorial
Android Tutorial
References
This tutorial is a brief overview of some major conceptsAndroid is much richer and more complex Developers Guide
http://developer.android.com/guide/index.html
API Reference
http://developer.android.com/reference/packages.html
Tools
Android SDK
Once installed open the SDK Manager Install the desired packages Create an Android Virtual Device (AVD)
SDK Manager
AVD
In Eclipse, go to Help -> Install New Software Click Add in top right Enter:
Click OK, then select Developer Tools, click Next Click Next and then Finish Afterwards, restart Eclipse Specify SDK location (next 3 slides)
Must do this every time start a new project in a new location (at least in Windows)
Project Components
src your source code gen auto-generated code (usually just R.java) Included libraries Resources
Manifest file
XML
Manifest file Shouldnt usually have to edit it directly, Eclipse can do that for you Preferred way of creating UIs
Separates the description of the layout from any actual code that controls it Can easily take a UI from one platform to another
R Class
Auto-generated: you shouldnt edit it Contains IDs of the project resources Enforces good software engineering Use findViewById and Resources object to get access to the resources
Layouts (1)
Composed of View objects Can be specified for portrait and landscape mode
Use same file name, so can make completely different UIs for the orientations without modifying any code
Layouts (2)
Layouts (3)
Click Create to make layout modifications When in portrait mode can select Portrait to make a res sub folder for portrait layouts
Likewise for Landscape layouts while in landscape mode Will create folders titled layout-port and layout-land
Note: these port and land folders are examples of alternate layouts, see here for more info
http://developer.android.com/guide/topics/resources/providing-resources.html
Avoid errors by making sure components have the same id in both orientations, and that youve tested each orientation thoroughly
Layouts (4)
Strings
In res/values
strings.xml
Application wide available strings Promotes good software engineering UI components made in the UI editor should have text defined in strings.xml
Strings are just one kind of Value there are many others
Contains characteristics about your application When have more than one Activity in app, NEED to specify it in manifest file
Go to graphical view of the manifest file Add an Activity in the bottom right Browse for the name of the activity
Need to specify Services and other components too Also important to define permissions and external libraries, like Google Maps API
Activity
http://developer.android.com/guide/topics/fundamentals/activities.html
Service
http://developer.android.com/guide/topics/fundamentals/services.html
http://developer.android.com/guide/topics/fundamentals.html
Activities (1)
The basis of android applications A single Activity defines a single viewable screen
Can have multiple per application Each is a separate entity They have a structured life cycle
Different events in their life happen either via the user touching buttons or programmatically
Activities (2)
Services (1)
Run in the background Can continue even if Activity that started it dies Should be used if something needs to be done while the user is not interacting with application
Should create a new thread in the service to do work in, since the service runs in the main thread Can be bound to an application In which case will terminate when all applications bound to it unbind Allows multiple applications to communicate with it via a common interface Needs to be declared in manifest file Like Activities, has a structured life cycle
Services (2)
Similar to launching a regular Java app, use the launch configurations Specify an Android Application and create a new one Specify activity to be run Can select a manual option, so each time program is run, you are asked whether you want to use the actual phone or the emulator
USB Debugging
Should be enabled on phone to use developer features In the main apps screen select Settings -> Applications -> Development -> USB debugging (it needs to be checked)
Read from the log file Show what android devices are available Install android applications (.apk files)
Recommend putting this directory and the tools directory on the system path
adb.exe
Debugging
Instead of using traditional System.out.println, use the Log class Imported with android.util.Log Multiple types of output (debug, warning, error, ) Log.d(<tag>,<string>) Can be read using logcat. Print out the whole log, which auto-updates
adb logcat adb logcat c adb logcat <tag>:<msg type> *:S can have multiple <tag>:<msg type> filters <msg type> corresponds to debug, warning, error, etc. If use Log.d(), then <msg type> = D
Erase log
Reference
http://developer.android.com/guide/developing/debugging/debugging-log.html
Screen Shots
Some say you need to root the phone that is not true One option: Android Screen Capture
http://www.mightypocket.com/2010/08/androidscreenshots-screen-capture-screen-cast/ Its slow, but fine for screenshots of applications whose screens arent changing fast Read their installation help, following the extra steps if need be (I had to copy adb.exe and some dll files, as they explain)
Using Google Maps in your app Setup project to use Google API version Edit Manifest file To indicate the app will use maps and the internet Get a maps API key Note: Google Maps API can display a map and draw overlays, but is not the full Google Maps experience you enjoy on the web For example, there does not seem to be inherent support for drawing routes between points (if you find it let me know)however, you can draw lines between points and almost any type of overlay, but thats different than street routes The directions API is a web service, which is different, among several other Google web services Read the Google API terms of use
End goal is to add the following two lines to XML file, under the <manifest> and <application tags>, respectively
Select Add under Uses Library (last slide) Then select Uses Library at this prompt Set name as: com.google.android.maps (next slide) and save
Select Permissions and then Add (last slide) Select Uses Permissions at this prompt Set name to: android.permission.INTERNET and save (next slide)
The debug mode signs for you with special debug certificate
All MapView elements in map applications need to have an API key associated with them
That key must be registered with the certificate used to sign the app
When releasing app, need to sign with a release certificate and get a new API Key
For debug mode, get the MD5 fingerprint of the debug certificate Locate the keystore
Use Keytool (comes with Java, in the bin directory with the other Java tools, should put that dir on system PATH) to get fingerprint
keytool -list v -alias androiddebugkey -keystore <path_to_debug_keystore> -storepass android -keypass android
If dont include v option, then will probably get only 1 fingerprint, and if its not MD5, then need v (Java 7 needs v)
Go to https://code.google. com/android/maps-api-signup.html , agree to terms and paste MD5 fingerprint, you will then be given an API Key
com.google.android.maps.MapView MapView is the basic view that represents a Google Map display Must include API Key in XML, inside a layout
http://code.google.com/android/add-ons/google-apis/reference/index.html
Acknowledgements
Android Developers Website Activity and Service life-cycle flow charts Tons of other Android info Google Maps API external library
http://code.google.com/android/add-ons/google-apis/maps-overview.html
http://www.mightypocket.com/2010/08/android-screenshots-screen-capture-screen-cast/
MightyPocket
http://www.javacodegeeks.com/2011/02/android-google-maps-tutorial.html http://efreedom.com/Question/1-6070968/Google-Maps-Api-Directions http://www.mail-archive.com/[email protected]/msg28487.html http://android.bigresource.com/ threads http://groups.google.com/group/android-developers threads Many http://stackoverflow.com threads
http://www.anddev.org/google_driving_directions_-_mapview_overlayed-t826.html