Lab Project Geo Quiz Spans the first six
Lab Project Geo. Quiz • Spans the first six chapters of the text • You will create an application with a GUI user interface for a true/false application • This project introduces many of the basic concepts the apply to building and debugging Android applications • The technology changes fast; you’ll likely notice differences from the text. For example, I had to change vertical 24 dp in the text to 60 dp so text would not be hidden behind the action bar. I also had to move the leftright buttons from the right to the left; they were hidden by a mail icon.
Geo. Quiz Screen Shots Main Activity Cheat Activity
drawable vs mipmap • Under the app/src/res folder, observe various mipmap folders – mipmap-mdpi, mipmap-hdpi, mipmap-xxhdpi • The text discusses various drawable folders – drawable, drawable-mdpi, drawable-hdpi, drwable-xhdpi, drawable-xxhdpi • Why the difference? – – – Device screen density don’t exactly match the provided densities Drawable images scale up, when necessary, which can reduce quality Mipmap Images scale down, preserving quality Use mipmap for the launcher icon and application resized images Use drawable for images used as-is, to avoid scaling overhead
Auto Getter and Setter Generation • Text variable naming conventions – Public non-static variables start with ‘m’ – Public static variables start with ‘s’ • Why? To avoid this. name = name in methods • To generate getters and setters – Right click after the constructor amd choose getters and setters • Result – The method names will include the ‘s’ and ‘m’ – To avoid this: file=>settings->expand editor->expand code style>java->code generation and set field prefix to m and static field preference to s
Android Emulator • Start the Emulator from Eclipse – – Right click on the application Select run as and then Android application To debug: Select debug as and then Android Application After the first time, just click the run icon at the top of the Eclipse window • Important notes – The emulator takes quite a bit of time to launch; be patient – Don’t shut down the emulator between application runs. Closing leads to long start up times, and sometimes confuses Studio – Every Studio run reinstalls and re-launches the application
Connect to Android Device • Requirements – Android tablet or phone – USB or micro-USB cable • Steps – Activate developer mode and enable device debugging o Settings->about device->tap build number seven times o Settings->applications->development->enable device debugging • Simply run from studio Note for Windows: Acording to the text, you may have to install the ADB bridge and possibly have to install an appropriate device driver. I did not have to do this, so this may no longer be necessary.
Android Terminology • Component: Activities, content providers, broadcast receivers, and services that together make up an application • • • Application: A group of launchable components Content Provider: A component that serves data to other applications Broadcast receiver: Component receiving notifications from other activities Service: Background component responding local or remote application requests Context: Object containing the global state of an application environment • Intent: Asynchronous launching (or communication) message • Intent Filter: Criteria an Android device uses to find matching activities • Bundle: A set of key/value pairs used to persist the state of an activity
Android Terminology (cont. ) • Preference: A key/value pair, which defines a user option • Context Menu: A menu executing after a long click • Long Click: Analogous to right clicks; Hold click for approximately two seconds • Modal: Dialog requiring a response before continuing an activity • Localize: Separate data from an application, so changes don't require changing source. Refer to the data by an identifier name. • Faded Edge: An edge of a scrollable area that is faded to indicate that more information exists • Fragment: An independent module launched without the manifest and tightly bound to an application to execute sub-tasks
Android Terminology (cont. ) • Gravity: Determine alignment of text within a view of different size. Values can include: top, bottom, left, center, fill • Content Provider: Wrapping component for data, exposing data (SQL for example) to multiple applications, using a standard API. • Cursor: An object containing rows extracted from an SQL table • Projection: The columns of an SQL table of interest • Live Folder: Deprecated mechanism to display data on home page – Developers now use Widgets – Live: updates display automatically • Widgets: Miniature icon-based application view (clock, battery charge percent) embedded on the home screen or application • Live Wallpaper: Dynamic interactive home screen backgrounds
Intent Definition: Messages that asynchronously launch or communicate with other applications/activities • Android Intent Resolver – Applications sends intents to the Android system – The Android intent resolver searches all application manifests to find the most suitable matching activity – Intents can target a single application or multiple applications using broadcasts • Types of Intents – Explicit intents: Directly launch using class names – Implicit Intents: Launch based on criteria – Send/Receive information; launch activities More Details later
The Manifest • The new project wizard – Automatically Creates a default Android. Manifest. xml file – Creates a default activity in the project directory • Purpose: – Register activities, services, content providers with launching intent filters – Request permissions • To alter the manifest: There are two possibilities – Edit the manifest directly – Use the Manifest Editor 3
Hello. World Manifest <? xml version="1. 0" encoding="utf-8"? > <manifest xmlns: android="http: //schemas. android. com/apk/res/android" package="com. paad. helloworld" android: version. Code="1" android: version. Name="1. 0" > <uses-sdk android: min. Sdk. Version="1" // User cannot install if less (1=default) android: target. Sdk. Version="15" /> // Verify features between min & target <application android: icon="@drawable/ic_launcher" android: label="@string/app_name" > <activity android: label="@string/app_name" android: name=". My. Activity" > <intent-filter > <action android: name="android. intent. action. MAIN" /> <category Some of the above no /> android: name="android. intent. category. LAUNCHER" </intent-filter>
Implicit Intent Filter Example Note: An intent launches an activity <intent-filter> The Main Entry when a user launches the application <action android: name="android. intent. action. MAIN" /> <category android: name="android. intent. category. LAUNCHER" /> </intent-filter><intent-filter> Launched by other applications for list access <action android: name="android. intent. action. VIEW" /> <action android: name="android. intent. action. EDIT" /> <action android: name="android. intent. action. PICK" /> <category android: name="android. intent. category. DEFAULT" /> <data android: mime. Type="vnd. android. cursor. dir/vnd. google. note" /> </intent-filter><intent-filter> Launched by other applications for single item access <action android: name="android. intent. action. GET_CONTENT" /> <category android: name="android. intent. category. DEFAULT" /> <data android: mime. Type="vnd. android. cursor. item/vnd. google. note" /> </intent-filter>
Another Android Manifest <? xml version="1. 0" encoding="utf-8"? > <manifest xmlns: android="http: //schemas. android. com/apk/res/android" package="com. bikerolas" android: version. Code="30" android: version. Name="1. 2"> <uses-permission android: name="android. permission. RECEIVE_BOOT_COMPLETED" /> <uses-permission android: name="android. permission. ACCESS_LOCATION /> <uses-permission android: name="android. permission. ACCESS_GPS" /> <uses-permission android: name="android. permission. ACCESS_CELL_ID /> <application android: icon="@drawable/flingicn 1" android: label="@string/app_name" android: debuggable="false"> <activity android: name=". Fling" android: label="@string/app_name"> <intent-filter><action android: name="android. intent. action. MAIN" /> <category android: name="android. intent. category. LAUNCHER" /> </intent-filter> </activity> <service android: name=". Fling. Service" /> <receiver android: name=". Fling. Service. Manager" android: permission="android. permission. RECEIVE_BOOT_COMPLETED"> <intent-filter><action android: name="android. intent. action. BOOT_COMPLETED" /> </intent-filter> </receiver></application><uses-sdk android: min. Sdk. Version="2"></uses-sdk> </manifest> Note: ACCESS_LOCATION and ACCESS_GPS were used by earlier Android versions and are now deprecated
app. build. gradle and build. gradle • build. gradle (some of these are moved into the manifest during the build process) – – Configuration parameters for building an application Min, max, compile, target sdk versions Version number Dependencies • app. build. gradle – Configuration parameters for application build parameters
Localize Strings How? Create XML files in the res/values directory Why? Promotes Internationalize and easier maintenance Example (Strings. xml) <? xml version="1. 0" encoding="utf-8"? > <resources> <string name="hello">Hello World, My. Activity!</string> <string name="app_name">PA 4 AD_Ch 02_Hello_World</string> </resources> • Build: Android creates gen/<package. Name>/R. java automatically from the information in res/Strings. xml • Java Access: set. Title(get. Text(R. string. hello)); • Manifest XML Access: android: label="@string/app_name"
Res directories • They are not nested (no res/directory/subdirectory) • Their names give a hierarchical appearance layout, layout-en-w 720 dp • Files contained in all of the variant files have the same name • Android tries the most specific one first and works backwards to the most general. If the file name is not found, it is an error
match_parent or wrap_content How? Store xml text files in the res/layouts directory <? xml version="1. 0" encoding="utf-8"? > <Linear. Layout xmlns: android="http: //schemas. android. com/apk/res/android" android: layout_width=“match_parent" android: layout_height=“match_parent“ android: orientation="vertical" > <Text. View android: layout_width=“match_parent" android: layout_height="wrap_content“ android: text="@string/hello" /> </Linear. Layout> Notes • fill_parent (or match_parent Android 2. 0 and later) fill the entire space of the parent layout • Wrap_content uses as little space as needed
Layout Components within a View • Frame. Layout Each widget must specify where it is to appear • Linear. Layout Widgets are arranged vertically or horizontally (similar to Box in Swing) • Relative. Layout Widgets are arranged relative to the position of others within the View
Relative. Layout Example
The Java Code package com. paad. helloworld; import android. app. Activity; import android. os. Bundle; public class My. Activity extends Activity { /** Called when the Activity is created or restarted. **/ @Override public void on. Create(Bundle saved. State) { super. on. Create(saved. State); set. Content. View(R. layout. main); } } A Bundle is a bunch of key/value pairs for Applications to restore their state after an Android termination
Debugging Techniques • Tracing the execution – Start: android. os. Debug. startmethod. Tracing("foo"); – Stop: android. os. Debug. stop. Method. Tracing(); – Writes output to: foo. trace on the emulator or device • Log class: (Log. i, Log. d, Log. e, Log. v, Log. w) – Analogous to Java: System. out. println or Javascript: Console. log – Example: Log. v("category string", "message"); • Asynchronous Alert Dialogs Alert. Dialog. Builder build = new Alert. Dialog(context); build. set. Title("Some Message"); Alert. Dialog ad = builder. create(); ad. show(); • Breakpoints and Watch expressions: Very similar to Eclipse
Strict Mode (Android 2. 3 and newer) Developer tool: detects questionable operations and performs Specified actions that flag these to facilitate correction • Report violations and IO Operations: Disk reads, disk writes, etc. • Common options when detection occurs: Write a Log message, display a dialog message, or crash the application • Example Strict. Mode. set. VMPolicy(new Strict. Mode. VMPolicy. builder(). detect. Disk. Writes(). detect. Leaked. Sql. Lite. Objects(). penalty. Log(). penalty. Death(). build()); Note: The build retuns a Thread. Policy object to the VM Policy builder • Detect if in debug mode Application. Info info = get. Context(). get. Application. Info(); if (info. flags & Application. Info. FLAG_DEBUGGABLE) != 0) { /* code here */}
- Slides: 24