ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I) Who am I? Lo Chi Wing, Peter Lecture 1: Introduction to Android Development Email: Peter@Peter-Lo.com Facebook: http://www.facebook.com/peterlo111 Peter Lo 2 Course Outline What Apps can you develop after this course? Lesson 1 Lesson 2 Lesson 3 Lesson 4 Lesson 5 Lesson 6 Introduction to Android Development Android Layout and Components Android Life Cycle and Permission Menu, Action Bar and Dialog Intent and Fragment List and Deployment 3 4
Are you take the Right Course? Where can you find the material? This course is intended for beginner who would like to learn how to develop Android Apps. The code examples are not particularly complex, but it is assumed that you have basic programming knowledge. Workshop Notes and Exercises http://www.peter-lo.com/teaching/4t025-1-a/ Android Developer Official Site http://developer.android.com/ Microsoft DreamSpark http://www.dreamspark.com 5 6 Mobile Phone Evolution What is Android? A Linux-based operating system for mobile devices. An open-source project and is distributed free of charge. Developed by the Open Handset Alliance and Google Inc. Supporting telephony, messaging, emailing, contact management, calendar, entertainment, multimedia experience, location services, mapping, social interaction, etc. 7 8
Android Family Platform Versions Android 1.5 (2009) Android 1.6 (2009) Android 2.0/2.1 (2009) Android 2.2 (2010) Android 2.3 (2010) Android 3.0 (2011) Android 4.0 (2012) Android 4.1 (2012) Android 4.4 (2013) 9 10 Source: http://developer.android.com/about/dashboards/index.html Screen Sizes and Densities Android Architecture 11 12 Source: http://developer.android.com/about/dashboards/index.html
Development Process for Android Apps Creating new Android Project Application Name name appears on device and title bar Minimum Required SDK minimum Android API level required for the app Project Name name for the Eclipse project Package Name Apps on a particular Android device must have unique packages Target SDK Android version that you want to use Theme specifies the Android UI style to apply for your app. Compile With the platform version against which you will compile your app. (By default, set to the latest version of Android available in your SDK). 13 14 Android Developer Tool (ADT) Structure of a Typical Android App File structure Palette, where we can choose different layouts and for the layout design Selector bar Zone where the current layout is rendered Tree View of the hierarchical structure of layouts and elements added to the screen. Object properties Source file with Activities, Services, Broadcast Receivers, and Content Providers Contain 2 auto generated files that should not be touched Provides access to resources (with unique ID) in App Contains text files, database, and the sort Contains file built by the ADT Switch from the graphical editor to the XML text editor 15 Executable generated from compiled classes Zipped archive file that will be shipped to android device Zipped application resources 16
Structure of a Typical Android App (cont.) The Manifest File API that allows you to use the newest API while remaining backward compatible drawable folders store image you need at different resolutions Before the Android system can start an application component, the system must know that the component exists by reading the application's AndroidManifest.xml file. The layout folder contains files that define the apps under interface RelativeLayout LinearLayout GridLayout FrameLayout AndroidManifest defines: App name App icon Activities used Permission required for App android.intent.action.main define the entry point for App Apps must declare all its components in this file, which must be at the root of the application project directory. 17 18 SDK Manager Simple Android App Program 19 package com.example.myfirstapp; import android.os.bundle; import android.app.activity; import android.view.menu; public class MainActivity extends Activity { @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } There is no need to type the import statements by hand. Just use the classes in your code, and when Eclipse marks the line as an error, click on the light bulb at the left, or hit [Ctrl] [1], then choose to have Eclipse insert the import statements for you. Apps are frequently shut down by the device. This lets you remember some info about the previous invocation. You should always call super.oncreate as first line of oncreate. Define the Activity for output Overriding an existing method @Override public boolean oncreateoptionsmenu(menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true; } } Comment, all statement after // will not execute 20
Running Apps How to Testing or Running Apps? During the build process, your Android projects are compiled and packaged into an.apk file. It contains all of the information necessary to run your application on a device or emulator. If you are developing in Eclipse, the ADT plugin incrementally builds your project as you make changes to the source code. Eclipse outputs an.apk file automatically to the bin folder of the project, so you do not have to do anything extra to generate the.apk. On the Android Emulator: Deploy directly from Eclipse during development, do your normal testing here On an Android device: Deploy from your PC via USB Deploy from a Web site Deploy via email Deploy from the Android Market 21 22 Android Virtual Devices Operation of AVD AVD (Android Virtual Device) is an Android Emulator configuration that lets you model an actual device by defining hardware and software options 23 24
Debugging Log Cat The Android SDK provides most of the tools that you need to debug your applications. DDMS (Dalvik Debug Monitor Server) adb (Android Debug Bridge) JDWP debugger Breakpoint Log The Android logging system provides a mechanism for collecting and viewing system debug output. Logs from various applications and portions of the system are collected in a series of circular buffers, which then can be viewed and filtered by the logcat command. 25 26 Logcat Level Dalvik Debug Monitor Service (DDMS) Priority Meaning Description V Verbose Lowest priority. Used for low level debugging. Print stuff like position of your character when you are debugging why the hell it's going off screen etc. D Debug Messages that could be useful to determine where something went wrong. E.g. put a debug level message to each method beginning and end. I Info These messages can be useful in production as well. Log important events in info level, e.g. Engine created W Warning Something not quite right, but it's not a bug. E.g. you can't connect to Facebook to submit High score - not nice, but you can live with it. E Error Log exceptions and errors that you will need to analyze later and solve. F Fatal Fatal exception 4T025-1-A S Silent @ Peter Lo 2014 Highest priority, on which nothing is ever printed 27 DDMS (Dalvik Debug Monitor Service) is a tool that supports many things Simulate incoming calls in emulator Set GPS locations in emulator See print statements and runtime errors Set locations and take screenshots of actual Android device 28
Android Debug Bridge (adb) Break Point A versatile command line tool that lets you communicate with an emulator instance or connected Android-powered device: Client, which runs on your development machine. You can invoke a client from a shell by issuing an adb command. Server, which runs as a background process on your development machine. Daemon, which runs as a background process on each emulator or device instance. You can find the adb tool in <sdk>/platform-tools/. To create a break point, just double click the vertical bar in Java source code can When you run the application in debug mode, the emulator will pause and you will be able to examine your live code in this mode in the Eclipse debugger. 29 30