• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Android Application Development Basic
 

Android Application Development Basic

on

  • 1,073 views

OESF Authorized Training Course official textbook ...

OESF Authorized Training Course official textbook
Course: Android Application Development Basic
FULL VERSION
Language: English


Contributed by
Created by: UL Systems Inc.
Edited by: Leading Edge Co.,Ltd.
Translated by: ISB Vietnam Co.,Ltd.

[Attention!]
This textbook is licensed under the Creative Commons License BY-NC-SA 4.0. It is prohibited to use this material for commercial use otherwise you are OESF member or OESF education consortium member.

Statistics

Views

Total Views
1,073
Views on SlideShare
483
Embed Views
590

Actions

Likes
2
Downloads
0
Comments
0

2 Embeds 590

http://oesf-edu.com 587
https://translate.googleusercontent.com 3

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Android Application Development Basic Android Application Development Basic Presentation Transcript

    • Android Application Development Basic Training This material is licensed under the Creative Commons License BY-NC-SA 4.0. Ver1.02(01)
    • About usage of this Textbook ・ This textbook is licensed under the Creative Commons License BY-NC-SA 4.0. It is prohibited to use this material for business use otherwise you are OESF member or OESF education consortium member. ・This training program, includes entire of training materials, will be provided without any guarantee from OESF. OESF has no responsibility if it occurred damages or legal violation behaviors which be caused of using or misusing any content or detail information of this course. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • Purpose of Training • Equip the necessary knowledge for developing application based on Android. – – – – The functions provided by Android Environment development The usage of Development Tools How to implement/debug the Application • Experience application development through practice exercises. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • Training Schedule • The 1st day – – – – Chapter 1 : Chapter 2 : Chapter 3 : Chapter 4 : What is Android? The architecture of Android Application Environment development preparation Tutorial • The 2nd day – Chapter 5 : Practice exercises – Chapter 6 : Training summarize This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • Skill requirement to attend training course • Have experience in Java – Actually use Java to do programming in practice exercises in the 2nd day. – Have understanding of basic Java language specification. • Have experience in using Eclipse – Using Eclipse to practice exercises. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1. What is Android? This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • Chapter 1: Overview • • • • • • What is Android? Why has Android attracted attention? What can you do with Android? Android from the technical aspect Application development environment How to distribute Applications? This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.1. What is Android? • Android is – Platform for Mobile devices – Mainly used as Platform for Mobile phones • The history of Android Date Content 10/2003 Android Company established 08/2005 Google bought Android Company 11/2007 Open Handset Alliance (OHA) established Development Tool Android SDK 1.0 released 10/2008 Started to sell loading Android- Smart Phone 「T-Mobile G1」 in the world 04/2009 Development Tool Android SDK 1.5r1 released 05/2009 Development Tools Android SDK 1.5r2 released 07/2009 NTT Docomo started to sell Android Mobile 「HT-03A」 in Japan Development Tool Android SDK 1.5r3 published This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.1. What is Android? • The history of Android (Continue) Date Content 09/2009 Development Tool Android SDK 1.6 published 10/2009 Development Tool Android SDK 2.0 published 01/2010 Development Tool Android SDK 2.1 published 05/2010 Development Tool Android SDK 2.2 published 12/2010 Development Tool Android SDK 2.3 published (Gingerbread) 02/2011 Development Tool Android SDK 2.3.3 published Development Tool Android SDK 3.0 published (Honeycomb) This material is licensedEmbedded Creative Commons License BY-NC-SA 4.0. Copyright 2009, Open under the Software Foundation, All rights reserved 9
    • 1.1. What is Android? • The main organizations related to Android Organization Name Overview Open Handset Alliance (OHA) • This organization was established to promote popularization of Android. Organize the activities of compatible handsets/software development and services popularization. • Android specifications settlement, deliverables control are done by the OHA. • 71 companies joined (until 05/2010) such as T-mobile (USA), Motorola (USA), NTT Docomo (Japan), etc… • http://www.openhandsetalliance.com/index.html Open Embedded Software Foundation (OESF) • This organization was established to develop and popularize the common framework, platform for Android based Embedded System. • Working groups that are organized according to business fields take the lead in implementing activities. • 75 companies joined (until 05/2010) • http://www.oesf.jp This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.2. Why has Android attracted attention? • At the present, Android has attracted a lot of attention – How much attention has it attracted? • Android based hardware are being continuously launched to the market. Hardware Overview Mobile Phone 05/2009 NTT Docomo launched HT-03A (HTC Magic) 06/2009 HTC (Taiwan) launched HTC Hero 07/2009 T-Mobile myTouch 3G was on sale 01/2010 Google NEXUS ONE was on sale 04/2010 NTT Docomo Xperia was on sale Media Player Creative Zii EGG was on sale This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.2. Why has Android attracted attention? • At the present, Android has attracted a lot of attention. – How much attention has it attracted? • The number of web search about Android from the second haft of 2008 has been increasing. (Google Trend Research) ■ Major Events A: T-Mobile G1 on sale Android Market opened B: OHA released Android SDK 2.0 C: Google Nexus One on sale D: Small size version of Sony Ericsson Xperia X10 on sale E: Microsoft criticized for patent infringement of Android This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.2. Why has Android attracted attention? • There are 4 reasons why Android receives much attention 1. Android is available for free, which is expected to reduce cost for development platform. 2. Everyone can get Development Tools, Technical documents and Source code in free. 3. The training cost is low if you know the Java SE. 4. Being deployed in embedded development field besides Mobile Phone. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.2. Why has Android attracted attention? • Android is available for free, which is expected to reduce cost for development platform. – Expense for development platform is expected to reduce by adopting Android platform. ※Development software combined with hardware, such as Driver, is necessary. Reduced cost Application Development Fee Platform Development Fee Hardware Development Fee Before use Android After use Android This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.2. Why has Android attracted attention? • Everyone can get Development Tools, Technical documents and source code in free – Under Apache License 2.0, Android’s documents, source code have been published openly. • The source of Proprietary modules such as driver, GoogleMapsAPI is not provided. – Development Tools are freely received from OHA. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.2. Why has Android attracted attention? • The training cost is low if you know the Java SE – Because Android Application can be developed by Java SE and Eclipse: • It is unnecessary for learning development language if you know Java language. • The cost of learning how to use Development Tools is low for the developer who has used Eclipse. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.2. Why has Android attracted attention? • Being deployed in embedded development field besides Mobile Phone – Besides Mobile phone, the utilization on various equipments is being researched and developed • Besides Mobile phone, some hardware which Android has been transplanted into are: – Net book – Navigation System (Car Navi, etc.) – TV Recorder (Record/play Digital broadcast) – Other digital home appliances » Be exhibited many times in the Embedded System development technology Expo (ESEC2010) This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.3. What can you do with Android? • Developer can create the applications that using the following functions provided by Android – View Website by Full browser – Drawing 2D/3D Graphic – Show/Play sound, image, still media • Corresponding format (MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, GIF) – – – – – – Voice call by GSM (2G), 3G Data transmission by Bluetooth, EDGE, WiFi Camera (still image/ video) GPS Various sensors Data storage by SQLite This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.3. What can you do with Android? • Sensor devices that can be controlled by Android # Sensor Name Overview 1 Acceleration sensor Detect acceleration 2 Tilt sensor Detect azimuth, inclination (the above and below slope of horizontal condition), rotation angle (the left and right slope of horizontal condition) 3 Temperature sensor Detect temperature 4 Magnetic sensor Detect magnetic 5 Light sensor Detect light 6 Approach sensor Detect the distance between objects 7 Gyroscope Detect angular velocity 8 Pressure sensor Detect the pressure put on real machine ※ Need the hardware when using sensors This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.3. What can you do with Android? • Introduce Android applications Web Browser Telephone This material is licensed under the Creative Commons License BY-NC-SA 4.0. Google Maps
    • 1.3. What can you do with Android? – Using Web Browser 1. View weather information – Use Google Weather API to view weather information ① – Input Japanese (IME) 2. Simeji – Software keyboard allows Flick input ※ Flick input is a method that input to chose character by slide the key of software keyboard ② This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.3. What can you do with Android? – Using sensors 3. 4. Google Sky Map – Use inclination sensor and acceleration sensor to view the night sky in the direction toward the terminal Labyrinth lite – The game uses inclination sensor ③ – Using camera 5. Layer – Browser displays the digital information of the reality picture taken by camera This material is licensed under the Creative Commons License BY-NC-SA 4.0. ④ ⑤
    • 1.4. Android from the technical aspect • The execution environment for Android applications – Android applications run on Android run-time virtual machine – Linux has adopted as Android run-time platform, the applications run on Linux Software(Android Platform) Application Application Android Runtime Linux kernel Hardware This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.4. Android from the technical aspect •Android is composed of 5 elements Components of Android Application ⑤ Home Address Call Application Browser Application framework ④ Activity Window manager Content provider View system Package management Acknowledgement management Call management Resource management Position management Sensor management Library ② Surface manager libc Media framework SQLite OpenGL I ES FreeType Webkit Android run-time SGL SSL ① ③ Core library Dalvik VM Linux kernel Display driver Bluetooth driver Camera driver Flash memory driver Binder ( IRC) Keypad driver USB driver Hardware This material is licensed under the Creative Commons License BY-NC-SA 4.0. WiFi driver Audio driver Electric power management
    • 1.4. Android from the technical aspect • Components overview ①.Linux kernel • Linux kernel is based on Linux 2.6 and added some Android features • Include the hardware that provides the necessary functions to run applications ②.Library • Library is created by C/C++ – Webkit (Web browser Draw), SQLite (Database), OpenGL/SE (3D draw), … • Library is used through application framework by application. ③.Android runtime • The execution environment of Android applications • Core Java Libraries includes Virtual machine execution environment (Dalvik VM) This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.4. Android from the technical aspect • Components overview ④. Application Framework • Provides necessary Java API to use applications behavior/operation and Libraries ⑤. Application • Android applications are developed by using application framework – Such as: Telephone, Web browser, Address book, etc… This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.4. Android from the technical aspect • The lightweight runtime environment for Mobile – Dalvik virtual machine (Dalvik VM) • Optimization of Java VM for small-memory environment – Google has developed its own virtual machine – Be designed to allow simultaneity execution of multiple applications even in low-memory environment – Dalvik executables format (.dex) • In Dalvik VM, the standard .class, .jar are converted into .dex to run • Compare with .class, the memory size used when running is small Java VM .class Dalvik VM convert .dex .jar This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.4. Android from the technical aspect – Dalvik VM is not compatible with Java VM • The binary format of Executable files are different – In Dalvik VM, file format .class, .jar can not run – In Java VM, file format .dex can not run Java VM Dalvik VM .class .dex .jar This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.4. Android from the technical aspect • If there is no comparison about portable limit, Android applications is almost the same with Java applications operating on PC. i - Application (Doja-5.x)※1 Android Application Java support Java ME Java SE Application size limit 1MB (depend on model) None Heap memory limit 6MB~15MB (depend on models) 16MB (SDK setting value) ※2 Transmit limit Unable to transmit except application download origin None HTTP transmit size limit Request body: 80KB Response body: 150KB None Cookies Available with model released from 2009 summer Available Character code SJIS UTF-8 Persistence method ScratchPad File, SQLite ※1. i - Application Content Development Guide for Doja-5.x/5.x LE~ Detail Part ~Version 3.00 ※2. Default setting value in SDK, may be changed according to models. Fro example, Nexus One is 24MB. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.5. Application development environment Item Necessary environment OS Windows XP, Windows Vista (32 / 64 bit) From Mac OS X 10.5.8 onwards (x86 only) Linux (already tested on Ubuntu Hardy Herson) Java SDK Java SDK 5, Java SDK 6 Android Development Tools Android SDK Integrated Development Environment Eclipse 3.4 (Ganymede), 3.5 (Galileo) Android Development Tools (ADT) Plug-in Text-based Development environment Apache Ant (From Windows version 1.7, Mac/Linux version 1.6.5 onwards)  Actual equipment is not required. Emulator for development is available.  Emulator may have some parts (such as sensor, etc.) unavailable. Refer for details: http://developer.android.com/intl/ja/sdk/requirements.html This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.6. How to distribute Applications? • There are 2 main ways to distribute Applications – Distribute via Android Market – Distribute without using Android Market ※ What is Android Market? – Is a Website for distribution/sale Android Applications – Is a common way to distribute Android Applications This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.6. How to distribute Applications? • Distribute via Android Market site – Distribution process 1. Register Developer account on Android Market site – To register Developer account, you must have Google account 2. Create digital certificate for Applications※1 – Use Keytool (includes Java SDK) ※2 to create certificate 3. Applications must be digitally signed※1 – Use jarsigner (includes Java SDK) ※2 to make digital signature 4. Upload Applications onto Android Market ※1 Can create certificate, make sign by Eclipse functions ※2 Refer Tools manual to know the Tools usage – Requirement • Google Account This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.6. How to distribute Applications? • Distribute via Android Market site – Process of register Developer account on Android Market site 1. Register developer profile on Developer registration site – Registration Site » http://market.android.com/publish/signup – Input Profile items » » » » 2. 3. Name Mail address (need Google Account) Web site URL Phone number Payment for registration ($25) – Can payment with Credit card via Google checkout Agree with Distribution Agreement This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.6. How to distribute Applications? • Distribute without using Android Market site – Distribute Application Files directly • Example – Distribute Files via Homepage – Distribute Files by CD, USB memory, … – Distribute Files over network such as LAN environments • Distribution process 1. Create digital certificate for Applications 2. Digitally sign Applications 3. Distribute Application Files • Installation method 1. Using Android terminals to download and install files 2. Connect USB with PC and Android terminal, use install command to install This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.6. How to distribute Applications? • Distribute without using Android Market site – Installation Method ① Using Android terminals to download and install Application files • Process 1. Use browser of Android Terminal to download Application file 2. After the download is completed, click on the Application file on download history 3. Press “Install” on Install confirmation screen This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 1.6. How to distribute Applications? • Distribute without using Android Market site – Installation Method ① Click on file Click on Download file Click on Install button Step 1 Step 2 This material is licensed under the Creative Commons License BY-NC-SA 4.0. Step 3
    • 1.6. How to distribute Applications? • Distribute without using Android Market site – Installation Method ② Connect USB with PC and Android terminal, use Install command to install • Process 1. 2. Use USB cable to connect PC and Android terminal Start Command prompt, execute adb install command >adb install < Application file name(.apk)> ■Install Input Command Prompt( C:Simeji3.0.6.apk )by USB C:>adb install C:Simeji3.0.6.apk // ・・・ Step 1 596 KB/s (1413225 bytes in 2.312s) pkg: /data/local/tmp/Simeji3.0.6.apk Success // ・・・ Step 2 This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2. Android Application Architecture This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • Chapter 2: Overview • Main components • Life cycle • The security of Android This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.1. Main components • The components of Android are available to achieve key features of application Key features of a common Application User interface Inter-process communication Activity Intent External communication File Access Perform Background Actualize Content Provider Main components of Android This material is licensed under the Creative Commons License BY-NC-SA 4.0. Broadcast Receiver Service
    • 2.1. Main components • There are 5 basic components: 1. 2. 3. 4. 5. Activity Intent Broadcast Receiver Service Content Provider This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.1.1. Activity • What is Activity? – The Android application screen is created by placing many parts such as Button, Checkbox, … on Activity – One screen is composed of one activity – Activity has running and pausing status This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.1.1. Activity • The Android application screen is created by placing many parts such as Button, Checkbox, … on Activity ① ② The left figure is activity which view (button) placed on 1. Activity 2. View (button) The left figure screen is shown in the following configuration 2. View ( button) 1. Activity This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.1.1. Activity • Can handle to respond to user input screen – On screen, the information of operation (Ex: click on button) will be notified to Activity via View – Activity can handle to respond to the notification • Ex: after click on the button 3.Execute the handler when button clicked 1. Click on button Button 2.Notify Activity public void onClick(View view) { // View message on screen } This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.1.1. Activity • One screen is composed of one activity – For Application has multiple screens, the number of activities is equal to the number of screens. Application Application Activity Activity Activity Activity The Application is composed by 1 screen The Application is composed by 3 screens This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.1.1. Activity • Activity displayed mechanism – On screen, at one time, only one activity is displayed • In Android Application, even if there are many activities, only 1 activity can be displayed • For display new Activity, previous activity is hidden, new activity is displayed on above This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.1.1. Activity • There are several states of activity, the state is changed by operation • State of activity has defined in 3 types: 1. Foreground status • When activity is being displayed, the cursor is in working condition 2. Background status • Activity is in hidden condition by another activity 3. Visible status • When no activity screen is displayed (dialog…), the activity cursor is in losing condition (activity is being displayed over dialog) This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.1.1. Activity • The kind of activity is used depend on the type of screen – Example of Activity # Screen Using Activity class 1 Single Screen Activity class 2 List display Screen ListActivity class 3 Map Screen (Google Map) MapActivity class 1. Single Screen (use Activity) 2. List display Screen (use ListActivity) This material is licensed under the Creative Commons License BY-NC-SA 4.0. 3. Map Screen (use MapActivity)
    • 2.1.2. View • What is View? – Buttons, checkboxes, spinners, … – The View that contains other views is called ViewGroup • The relation of Activity, View and ViewGroup User Interface View View ViewGroup View ViewGroup Activity This material is licensed under the Creative Commons License BY-NC-SA 4.0. View
    • 2.1.2. View • Typical View View name Overview TextView Display text Button Basic press button EditText Input text CheckBox The button has ON/OFF status Spinner This button for choose one item from the list AnalogClock Analog Clock This material is licensed under the Creative Commons License BY-NC-SA 4.0. Image
    • 2.1.2. View • Typical ViewGroup Name Overview LinearLayout • The ViewGroup that view is placed in horizontal or vertical direction • Display scroll bar in case it’s not fit in screen TableLayout • The ViewGroup that view is placed in grid (similar HTML table tags) • TableRow is specified inside TableRow • The ViewGroup that view is placed in Table line ListView • The ViewGroup that view is placed in one list • Display scroll bar in case it’s not fit in screen TabHost • The ViewGroup that display is replaced by switching tabs This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.1.2. View • Can create the original view (Custom Views) – Custom Views can be created in 3 ways • Extending an existing View – Effective way if you want to add functionally into an existing View • Inherit from basic class (android.view.View) – If you want to create a completely new View – Possible for develop a View in most freedom – Require deep knowledge of basic class for implementation from scratch • Combine existing Views, ViewGroups to create a new View ■ For more detail, please refer Android Developers in Development Guide http://developer.android.com/intl/ja/guide/topics/ui/custom-components.html This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.1.3. Intent • What is intent? – The mechanism to request a process and to exchange message between components • The components can use intent are activity, broadcast receiver and service – Example: • Start activity B from activity A (screen transition) • Send request “call phone” from Address book application to Make call application This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.1.3. Intent • Call out component by intent: the intent containing the information of caller component give call out request for Android – The sample of calling out B component from A component This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.1.3. Intent • There are 2 ways in using intent: using explicit intents and using implicit intents – Using Explicit intents • This way is used in case of called component is designated its name directly. It is used to call out components in the same application (such as screen transition) – The sample of calling out designated component This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.1.3. Intent – Using Implicit intents • This way is used in case of the called component is not designated (named), only specify action (Sample in under figure: specify to “make a telephone call”). Often used to call out components in other Applications (such as other Application screen) • From configuration information of components, Android will determine which component be started. – The sample of calling out an specified action This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.1.3. Intent – Using Implicit intents (continuous) • When using Implicit intents, by cooperating with Android’s applications, the following tasks can be done – Phone Application » Open Dial window of specified number – Address book Application » Display address book list » Display information of 1 address in address book » Edit information of specified address – Web browser » Use Browser to display the URL address of specified Website This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.1.3. Intent – Using Implicit intents (continuous) • Implicit intents can send broadcast for whole system • Intents sent by sending broadcast (broadcasted intent) is used to report the change with condition, such as “Complete system startup”, “Change battery occurred”, … • Intents sent by sending broadcast (broadcasted intents) are intents that can receive only Broadcast receivers. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.1.4. Broadcast receiver • What is Broadcast receiver? – Broadcast receiver is a mechanism for responding with intents sent by sending broadcast – Can create peculiar Broadcast receiver by inheriting BroadcastReceiver class This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.1.4. Broadcast receiver • Example of using Broadcast receiver – Check mail when starting up Android terminal 1. 2. Receive broadcast intent when Android terminal startup complete by Broadcast receiver Broadcast receiver call out Mail check service ※ Although mail check process can be executed from Broadcast receiver directly, but it’s more efficient when using Service to deal with time-consuming ① Complete startup message Intent ② Execute Mail check service Broadcast receiver Intent Send broadcast intent Receive intent This material is licensed under the Creative Commons License BY-NC-SA 4.0. Mail check service
    • 2.1.4. Broadcast receiver • Can use Broadcast receiver to create a process started by system notification – Define the action of broadcast intent (some excerpts) # Constant name of Intent class Condition Setting value 1 ACTION_BOOT_COMPLETED Completed system startup android.intent.action. BOOT_COMPLETED 2 ACTION_TIME_CHANGED Time set up android.intent.action.TIME_SET 3 ACTION_TIMEZONE_CHANGED The time zone is changed android.intent.action.TIMEZONE_CHANGED 4 ACTION_PACKAGE_ADDED The Application is new installed android.intent.action. PACKAGE_ADDED 5 ACTION_PACKAGE_REMOVED The Application is uninstalled android.intent.action. PACKAGE_REMOVED 6 ACTION_BATTERY_CHANGED Whether charging, the battery is changed android.intent.action. BATTERY_CHANGED ■ Please refer more setting value at the following URL http://developer.android.com/intl/ja/reference/android/content/Intent.html This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.1.5. Service • What is service? – The mechanism to execute a process by independent thread – Service and foreground’s activity are executed in separate threads. Therefore, the process can continuous even if an interrupt occurred with other activities. Play music (not use Service) Play music (use Service) The difference in operating when Activity interrupt occurred by receiving mail in case of playing music directly from Activity or through service. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.1.5. Service • Introduction about application which used service – DroidTracker: The application for sending location information get by GPS through SMS, mail, twitter.  Processing location information acquisition by GPS function through Service.  Processing SMS, mail, twitter sending functions through Service. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.1.6. Content provider • What is Content provider? – The mechanism that allows the application that holding data can be accessed (search, insert, update, delete) from other applications – Example • Access phone call log from application • Insert Website into browser bookmarks from application ※ Data means the information that is persisted in files, database of application This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.1.6. Content provider • Why content provider must be used to access to other applications’ data? – The data kept in application can not be access directly from external applications Security wall Application Get information External Applications Storage Edit information This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.1.6. Content provider • Content provider has mechanism to realize/achieve the access from external applications Security wall Application Content provider Get information External Applications Storage Edit information This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.1.6. Content provider • In order to achieve a secure access, content provider will settle the interfaces – Main interfaces # Event name Content 1 Query Search data, then return searching results 2 Insert Insert data 3 Update Update data 4 Delete Content provider Delete data This material is licensed under the Creative Commons License BY-NC-SA 4.0. Interface is settled query insert update delete External Applications
    • 2.1.6. Content provider • In Android, depend on content provider, data has been published – Published data • • • • • • • Phone call log Contact list Browser Bookmarks Browser Search history Images in Media Pictures in Media Audio in Media This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.2. Life cycle • What is Life cycle? – In Activity and Service, Life cycle is defined as the life cycle of object from creation until destruction. Activity Life cycle Service Life cycle This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.2.1. Activity Life cycle • Activity Lifecycle – 3 types of status transition are determined in an activity (Foreground, background, visible) – When status change occurs, Event will be notified. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.2.1. Activity Life cycle • Life cycle includes notified events This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.2.1. Activity Life cycle • In an Activity, the following events are notified. The developers can create a process to respond to these notifications – Notified events # Events name Content 1 onCreate The event occurs in the initial startup 2 onStart The event occurs just before the activity is displayed 3 onResume The event occurs just before the activity is available (active status) 4 onPause The event occurs just before the activity becomes visible 5 onStop The event occurs in case of unused status for a while after onPause (The event may not occur when lack of memory) 6 onRestart The event occurs when stopped activity is showed again 7 onDestroy The event occurs just before the activity is annulled (The event may not occur when lack of memory) This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.2.1. Activity Life cycle • Notes about Activity notification event – Not ensure that onStop, onDestroy are always be notified • In case memory run out, Android will stop unused application processes forcedly • In this case, onStop, onDestroy notifications in Activity Life cycle can not be ensured • In case it’s a absolute necessary process, use onPause to processing The transition route in case of memory run out This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.2.2. Service Life cycle • The Service Life cycle is defined as the right figure • For an event is notified when changing state like in Activity, the process to respond to notifications can be created # Events name Content 1 onCreate The event occurs in the first startup 2 onStart The event occurs just before the Service is started 3 onDestroy The event occurs just before the Service is destroyed This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.3. Android Security • To use the functions of Hardware such as network, camera, … the permission to use information of each application must be set – The operations that can affect on OS, application, user data, … are restricted in Application This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.3. Android Security • The method of setting application use permission 1. Insert uses-permission tags in AndroidManifest.xml 2. Set the set value of Permission Function in android:name ※ In below sample: Permit an application to connect to the Internet ■ Setting sample <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=http://schemas.android.com/apk/res/android android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> ・・・ Setting the permission information </application> for Internet Connection <uses-sdk android:minSdkVersion="3" /> <uses-permission android:name=“android.permission.INTERNET“/> // ・・・ Step1,2 </manifest> This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.3. Android Security • In other to use the following functions, Android application set up the permission information into AndroidManifest.xml – The sample of Permission information set value Set value (android.permission) Explanation INTERNET Internet connection ACCESS_FINE_LOCATION Location information(GPS…)accession CALL_PHONE Call without using call out screen READ_CONTACTS Read in Address book WRITE_CONTACTS Write into Address book CAMERA Camera devices accession Please refer the other set value at following URL http://developer.android.com/intl/ja/reference/android/Manifest.permission.html This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 2.3. Android Security • When install an application that includes use permission information, the confirmation for permitted information is executed Confirmation of Internet Connection permission This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 3. Development Environment Preparation This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • Chapter 3: Overview • Development Tools Installation • Development Tools Installation method – Development Tool use the things stored in C:android_trainingdevtools • Emulator preparation – Android Virtual Device (AVD) creation – AVD verify operation This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 3.1. Development Tools Installation • In this training, the following Development Tools will be installed Software Version Integration Development environment Eclipse 3.5 (Galileo) Java SDK JDK 1.6.0_20 Android SDK 2.1 r2 Android Plug-in Android Development Tools (ADT) Ver.0.9.X This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 3.2. Development Tools Installation method • Android SDK Installation 1. Start explorer from Start Menu/[ All Program]-[Accessories]/[Explorer], open C:android_trainingdevtools 2. Right click on File android-sdk_r06-windows.zip, then choose [Open from program]-[Compresses (zip) Folders] 3. Move folder android-sdk-windows within zip into C:android_training tools 3 2 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 82
    • 3.2. Development Tools Installation method • Setting Environment variables 1. 2. Open Control panel from Start Menu/ [ Control Panel], double click on [System] System/ Properties Window, on [Detail setting] Tag, click on [variable Environment] Button 1 2 This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 3.2. Development Tools Installation method 3. 4. 5. 6. From a Environment variable window/[System Environment variables], select [Path], then click on [Edit] button Insert C:android_trainingtoolsandroid-sdk-windowstools; into the head of variable value in Edit window of System variable Click on [OK] button in Edit window of System variable Click on [OK] button in Environment variable window Insert 4 3 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 84
    • 3.2. Development Tools Installation method 7. Check the reflect of change in environment variable 1. Click on [Command prompt] in Start Menu/[ All program]-[ Accessories] to start Command prompt 2. Execute command set<sp>|<sp>find<sp> “android” from Command prompt, check to make sure that Path setting is displayed (Refer below chart for output result) ※ <sp> ・・・ means Space This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 3.2. Development Tools Installation method • Android Development Tools (ADT) Installation 1. 2. Start Eclipse On the Menu [Help] of Eclipse, select [Install New Software…] This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 86
    • 3.2. Development Tools Installation method 3. If Network environment is using Proxy, make the following settings i. ii. iii. On the Menu [Window] of Eclipse, select [Preferences] On Preferences window, select [General]-[Network Connections] From Network Connections screen, select 「Manual Proxy Configuration」, setting the items below: – – HTTP Proxy: Proxy Server address (For example: 192.168.1.1) Port :Port number of Proxy Server (For example with right chart 80) ii ii i i This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 87
    • 3.2. Development Tools Installation method 4. Input http://dl-ssl.google.com/android/eclipse/ into “Work with” of Available Software window, then press Enter. 4 http://dl-ssl.google.com/android/eclipse/ This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 88
    • 3.2. Development Tools Installation method • Alternative method in case failed to connect to server 4-1. Click on button [Add...] of Available Software window. 4-2. Click on button [Archive] at Add Site window. 4-3. Input ADT-0.9.X.zip into “File name” as file in folder C:android_trainingdevtoolsforOffline, click on [Open] button, then click on [OK] button. 4-1 4-2 C:android_trainingdevtoolsforOffline 4-3 This material is licensed under the Creative Commons License BY-NC-SA 4.0. 89
    • 3.2. Development Tools Installation method 5. 6. Check Android DDMS, Android Development Tools in Available Software window, click on [Next] button. Click on [Next] button in Install window. 5 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 6 90
    • 3.2. Development Tools Installation method 7. 8. On Install window, check [I accept the terms of the license agreements], then click on [Finish] button. Installation begins Installation complete, click on [Yes] when Software Updates message displayed, restart Eclipse 9 8 This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 3.2. Development Tools Installation method 9. After restarting Eclipse, check to make sure that Android Development Tools button has been inserted into Toolbar. 1 0 The Tool button of Android Development Tools is displayed This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 92
    • 3.2. Development Tools Installation method • Set up Eclipse – Recognize Android SDK in ADT 1. From Menu [Window] of Eclipse, select [Preferences] 1 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 93
    • 3.2. Development Tools Installation method 2. 3. 4. From Preferences window, select [Android]. After selected, Error Message will be displayed, click [OK] to close Message window. In SDK Location, input 「 C:android_trainingtoolsandroid-sdkwindows」 Check to make sure that Error message on the top of Preferences window is disappeared, then click [OK] button. 3 2 Check that Error message is disappeared C:android_trainingtoolsandroid-sdkwindows 4 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 94
    • 3.2. Development Tools Installation method 5. If Network environment is using Proxy, make the following settings: i. ii. From Preferences window, select [Android]-[Launch] From the Launch screen, insert Proxy setup into [Default Emulator Options] (Format: -http-proxy<sp><Proxy Server address>:<Port number> ) – If Proxy Server address is「 http://192.168.1.1 」, Port number is「80」, make setup as follow » -http-proxy http://192.168.1.1:80 i ii This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 95
    • 3.2. Development Tools Installation method 6. Select [Android SDK and AVD Manager] from menu [Window] of Eclipse. 6 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 96
    • 3.2. Development Tools Installation method 7. 8. Select Available Packages from left column of Android SDK and AVD Manager window. Click on [+] of URL on right column to drop down, check on [SDK Platform Android 2.1, API-7, revision 2] then click on [Install Selected] button. ※ In offline environment, copy platform folder of C:android_trainingdevtoolsforOffline to C:android_trainingtoolsandroid-sdk-windows in responding to instruction of Trainer. 7 8 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 97
    • 3.2. Development Tools Installation method 9. Select [Accept] in “Choose Packages to Install” window , then click on [Install] button. 10. After installation finished, click [Close] button to finish installation. 1 0 9 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 98
    • 3.3. Emulator preparation • Create Android Virtual Device (AVD) that is device image of Emulator 1. From menu [Window] of Eclipse, select [Android SDK and AVD Manager]. 1 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 99
    • 3.3. Emulator preparation 2. Click on [New] button of Android SDK Devices window. 2 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 100
    • 3.3. Emulator preparation 3. When “Create new Android Virtual Device” window is displayed, set up parameters as below. Item name Set up value Name Android 2.1-update1 Skin 4. default Target 3 WVGA800 After finished setting, click on [Create AVD] button. 4 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 101
    • 3.3. Emulator preparation • Start the Emulator, check that AVD has been created 1. 2. Start up “Android SDK and AVD Manager” in Eclipse, select Default for AVD Name, click on [Start] button. Check on [Scale display to real size] in “Launch Option” window, input “6” into [Screen Size (in)], then click on [Launch] button. 1 Input appropriate size according to environment. 2 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 102
    • 3.3. Emulator preparation 3. Check to make sure that Emulator screen as below will be displayed. This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 103
    • 4. Tutorials This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • Chapter 4: Overview • Learn how to operate Development Tools through creation of HelloWorld application – Android Development Tools (ADT) – Emulator, Android Virtual Device (AVD) – Dalvik Debug Monitor Services (DDMS) This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 4.1. Create an application by ADT • What is Android Development Tools (ADT)? – Plug-in to develop Android applications by Eclipse. • The functions that ADT provides – Create Android projects – Execute/ debug Android applications – Support to Resource files creation • Resource files will be described in this section This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p r a c t i c e 4.1.1. Display Hello World • The beginning of an Android project, create the below application – Complete image New project is creation an application that displays Hello World HelloWorld! like in the left chart This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p r a c t i c e 4.1.1. Display Hello World • Create Android project 1. 2. 3. Double click eclipse on desktop to start up Eclipse. Select [new]-[Other ] from menu [File] of Eclipse. Select [Android]-[Android Project] of New window , then click on [Next] button. 3 2 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 108
    • p r a c t i c e 4.1.1. Display Hello World 4. Input following information into Project Name, Build Target, Properties from window [New Android Project]. Input HelloWorld into Project Name Check Android 2.1-update1 on Build Target Input following information into the following items of Properties Application Name :HelloWorld Package Name :jp.oesf.tutorial Create Activity :HelloWorld MinSDK Version :7 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 109
    • p r a c t i c e 4.1.1. Display Hello World 5. 6. Android Project will be created, and be displayed as in left-hand picture. Select HelloWorld project from Package Explorer, right click then select [Run As]-[Android Application]. 4 5 5 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 110
    • p r a c t i c e 4.1.1. Display Hello World 7. Start Emulator on other window, confirm to be sure that the following screen is displayed. • • Application title is HelloWorld. “Hello World, HelloWorld!” is displayed in application. 6 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 111
    • 4.2. Create screen design • Create screen design on Resource file – What is resource file? • All external files besides source code. • Files stored below /res in Android Project. – Types of resource file (partial excluded) # Resource file Content 1 res/drawable(-Xdpi)/*.* Image file (.png, .jpeg, .gif) 2 res/layout/*.xml Screen design information (ex. main.xml) 3 res/values/colors.xml Color information 4 res/values/dimens.xml Size information (ex. 5px, 10dip, 20pt) 5 res/values/strings.xml Character string information ■Explanations related to resource file http://developer.android.com/intl/ja/guide/topics/resources/resources-i18n.html This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 112
    • 4.2. Create screen design • There is function supports creating resource file in ADT. – ADT supply 3 kinds of resource file editor • Layout editor – Support creating screen design resource file. • Resource editor – Support creating resource file besides screen design resource file. • Manifest editor – Support creating AndroidManifest.xml This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 113
    • p r a c t i c e 4.2. Create screen design • Use function Layout Editor, create screen that include button, check box, text input in HelloWorld. – Completed image • Use GUI function of Layout editor to add button and check box. • Edit directly screen design resource file (XML file) to add text input. • Setting as below for display character string of every View. # View Display string character 1 Button Button01 2 Check Box CB1 3 Text input EditText This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 114
    • p r a c t i c e 4.2.1. Create resource file • Open screen design resource file 1. 2. Double click res/layout/main.xml from Package Explorer. When res/layout/main.xml file is opened by Layout Editor, confirm to be sure that the screen on the right-hand picture is displayed. 2 1 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 115
    • p r a c t i c e 4.2.1. Create resource file • Insert button, check box into screen design by Layout tab of Layout Editor. 1. 2. 3. Select Button from [Views], drag & drop on the screen. The button will be displayed as the right-hand picture. Similarly, drag & drop CheckBox from [Views] on the screen and then confirm to be sure that it is displayed. 2 1 Drag & drop This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 116
    • p r a c t i c e 4.2.1. Create resource file 4. 5. Select CheckBox01 on the screen, change Text properties from CheckBox01 to CB1 from Properties Views. Similarly, change Text properties of @+id/Button01 into Button01. Select HelloWorld project from Package Explorer, right click then select [Run As]-[Android Application]. 5 5 5 4 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 117
    • p r a c t i c e 4.2.1. Create resource file 6. Start emulator, confirm to be sure that below screen is displayed . • CheckBox, Button is displayed on screen. • Confirm to make sure display character string of Button is Button01. • Confirm to make sure display character string of CheckBox is CB1. 6 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 118
    • p r a c t i c e 4.2.1. Create resource file • Add text input into screen design by tab <File name> of Layout editor 1. Click on tab main.xml of Layout editor, confirm to be sure that XML source is displayed in Layout editor. ■res/layout/main.xml(partial omitted) 1 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android ・・・・・・・・・・・ > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:text="Button01" android:id="@+id/Button01“ ・・・・・・・・・></Button> <CheckBox android:id="@+id/CheckBox01“ android:text="CB1“ ・・・・・・・・・ ></CheckBox> </LinearLayout> This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 119
    • p r a c t i c e 4.2.1. Create resource file 2. Add EditText tag that is Text Input View into main.xml. ■res/layout/main.xml(partial omitted) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android ・・・・・・・・・・・ > <TextView ・・・・・・・・・・・ /> <Button ・・・・・・・・・></Button> <CheckBox ・・・・・・・・・ ></CheckBox> <EditText android:id="@+id/EditText01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="EditText"></EditText> </LinearLayout> 2 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 120
    • p r a c t i c e 4.2.1. Create resource file 3. Switch display in Layout Tab, confirm to be sure that EditText is displayed as below. Switch display EditText is displayed in Layout Tab 3 EditText added This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 121
    • p r a c t i c e 4.2.1. Create resource file 4. Select HelloWorld project from Package Explorer, right click then select [Run As]-[Android Application]. When Emulator starts, confirm to be sure that the right-hand screen is displayed. 5. • • 4 Text input is displayed. Display character string of Text input is EditText. 5 5 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 122
    • 4.2.1. Create resource file • Attention points when using Layout Editor 1. Although we tried to paste View group in Layout tab (View included in Layout) to the screen by drop & drag, ViewGroup was not be displayed on the screen. • 2. Handle the following matter by edit directly XML. – Arrange View of buttons, etc. within ViewGroup. Unable to select CustomView from [Views] • Handle by edit directly XML. 1 Although we arranged LinearLayout but it is not disaplayed This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 123
    • 4.2.1. Create resource file • Mechanism of screen create usage resource file – Activity uses Class R to acquire content of screen design resource file. • Class R – Class generated automatically at the time of “build”. – The class necessary to acquire information of resource file from program. – Display screen design from resource file information acquired by Activity#setContentView. ■HelloWorld.java R.layout.main will display main.xml public class HelloWorld extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } ■setContentView Display screen design on Activity <?xml version="1.0" encoding="utf-8"?> <LinearLayout ・・・・・・・・・・・ > ・・・・・・・・・・・ <EditText android:id="@+id//EditText01“ ・・・・・・・・・・・ android:text="EditText"></EditText> </LinearLayout> This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 124
    • 4.2.1. Create resource file • Relationship between R class and resource file – Program and resource information will be connected by resource ID. ■HelloWorld.java setContentView(R.layout.main); Reference ■R.java public static final class layout { public static final int main=0x7f030000; } Resource ID This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved Resource file 125
    • p r a c t i c e 4.3. Use resource besides design • Change display character string “Button01” of button that was added into HelloWorld to “Hello Button”. • Set display character string into character string resource file. – Completed image This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 126
    • 4.3. Use resource besides design • Character string resource file is resource file defined character string using in application. (res/values/strings.xml) ■Character string resource file (res/values/strings.xml) <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, HelloWorld!</string> <string name="app_name">HelloWorld</string> </resources> Refer to character string ■TextView 1. Add character string that displays on the screen into character string resource file. on screen design resource file (res/layout/main.xml) <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> Execute 3. Character string set in strings.xml is displayed. 2. Refer to character string of character string resource file from Text Properties in TextView This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 127
    • p r a c t i c e 4.3. Use resource besides design • Insert character string set-up into character string resource file. 1. 2. Double click on res/values/strings.xml from Package Explorer. Open file res/values/strings.xml by Resource Editor, then confirm to be sure that the right-hand screen is displayed. 2 1 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 128
    • p r a c t i c e 4.3. Use resource besides design 3. 4. Click on [Add…] button of Resource Tab, display Option screen of additional element. Select String from Option screen, click [OK] button. 3 4 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 129
    • p r a c t i c e 4.3. Use resource besides design 5. 6. Confirm to be sure that String was inserted into Resources Elements of Resources Tab. Input value into Name, Value of “Attributes for String” of Resources Tab as below. Setting value of “Attributes for String” Item Name 5 Name button_label Value 6 Setting value Hello Button This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 130
    • p r a c t i c e 4.3. Use resource besides design 7. 8. 9. Double click on res/layout/main.xml from Package Explorer After starting up Layout editor, open Layout Tab, then select Button01 Change Text properties into @string/button_label from Properties View. 8 7 9 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 131
    • p r a c t i c e 4.3. Use resource besides design 10. Select HelloWorld project from Package Explorer, right click then select [Run As]-[Android Application]. 11. Start emulator, confirm to be sure that the right-hand screen is displayed. • Display character string of Button will become Hello Button. 11 10 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 132
    • 4.4. Modify Manifest file • Manifest editor – There are 5 functions to modify Manifest file. • Manifest Tab – Version information, extension information, etc… • Application Tab – usage component information, icon, application name, etc… • Permissions Tab – Access limit setting • Instrumentation Tab – Setting Class used for information gathering of profile, etc… (ex: TestRunner, etc…) • AndroidManifest.xml Tab – Edit directly XML file This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 133
    • 4.4. Modify Manifest file • What is manifest file? – AndroidManifest.xml (1 file exists in application) – Define the following information related to application. • • • • • Setting icon, title of application. Definition about usage component (Activity, Service, etc…) Definition related to conduct of component. Setting access limit of application. Setting usage of library. ■Explanation related to Manifest file. http://developer.android.com/intl/ja/guide/topics/manifest/manifest-intro.html This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 134
    • 4.4.1. Add Activities into application • Add Activities into application File name Additional Activities HelloWorld2.java Additional screen design resource file res/layout/ helloworld2.xml – Add Activities HelloWorld2 into application. – The screen design resource file of HelloWorld2 is helloworld2.xml. This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 135
    • 4.4.1. Add Activities into application • Setting by Application Tab so that it is possible to use Activities added to application. 1. 2. Double click on AndroidManifest.xml from Package Explorer Open file AndroidManifest.xml by Manifest editor. Select Application Tab in Manifest editor, then confirm to be sure that the right-hand screen is displayed. 2 1 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 136
    • 4.4.1. Add Activities into application 3. 4. Click on [Add…] button in Application Nodes, display Option screen of additional element. Select Activity from Option screen, then click [OK] button. 4 3 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 137
    • 4.4.1. Add Activities into application 5. 6. Confirm to be sure that Activity has been added into Application Nodes. Input value as below into Name of Attributes for Activity. Item name Name 5 Setting value HelloWorld2 6 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 138
    • 4.4.2. Set up Right of access to internet • Set up Right of access to internet by Permissions Tab 1. 2. Double click on AndroidManifest.xml from Package Explorer. Open AndroidManifest.xml file by Manifest editor. Select Permissons Tab in Manifest editor, confirm to be sure that the screen on the right-hand picture is displayed. 2 1 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 139
    • 4.4.2. Set up Right of access to internet 3. 4. Click on [Add…] button in Permissions, display Option screen of additional elements. Select Uses Permisson from Option screen, then click on [OK] button. 3 4 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 140
    • 4.4.2. Set up Right of access to internet 5. 6. 5 Confirm to be sure that Permission is added into Permissions. Select android.permission.INTERNET from pull-down menu of Name in Attributes for Uses Permission. 6 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 141
    • 4.5. Usage method of emulator • What is emulator? – Read Android Virtual Device (AVD), emulate hardware operation on computer. • Function / Setting that not be supported by emulator – – – – – – – Actual incoming call, outgoing call USB connection Camera/Videographer Headphone connection Communication usage Bluetooth Connection status setting Battery charging level, AC power source setting This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 142
    • 4.5. Usage method of emulator • What is Android Virtual Device (AVD)? – It contains Android terminal information. • Android operating environment information (Operating environment for every version of Android) • Skin information (outside exterior, monitor size) • SD Card information This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 143
    • 4.5. Usage method of emulator • Operating method of emulator # Device key Content 1 Volume UP 1 2 3 2 Volume DOWN 11 4 3 Power source Power On/Off 10 5 4 Select button Select button 5 End call 9 8 7 6 Home Move to Search window 7 Return Return to previous screen 8 Menu 12 6 Search Display menu on the screen 9 Home Display home 10 Start call 8 11 Cross key Move cursor 12 Keyboard Key input This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 144
    • 4.5. Usage method of emulator • Switching screen orientation (Ctrl + F11) – Screen display that fits orientation of device. Portrait display Landscape display Detail information related to Android emulator http://developer.android.com/intl/ja/guide/developing/tools/emulator.html This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 145
    • 4.6. Usage method of debug tool • ADT – We can use debug function of Eclipse in Android application development. • Breakpoint • Step execution • Dalvik Debug Monitor Services (DDMS) – Android SDK Debug tool pack – There are functions as below: • Monitoring • Emulator operating • Acquiring information in emulator/hardware This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 146
    • 4.6.1. Usage method of ADT • Usage method of Breakpoint/Step execution – Both of setting method of Breakpoint/Step execution are similar to usage method of Eclipse. Debug View and Step execution Interrupt processing by Breakpoint This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 147
    • 4.6.1. Usage method of ADT • Attention points when debugging on ADT – It is necessary to set up in AndroidManifest.xml to debug using hardware (Real Android terminal). ■AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="jp.oesf.mtgeduwg.training.rssreader" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"> ・・・ </application> Set up debug <uses-sdk android:minSdkVersion=“7" /> </manifest> This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 148
    • 4.6.2. Usage method of DDMS • 2 methods to start DDMS – Execute ddms command 1. 2. Select [Command prompt] in [All programs]-[Accessories] on Start Menu, then start Command prompt. Input ddms in Command prompt, then press [Enter] key. DDMS started from command This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 149
    • 4.6.2. Usage method of DDMS • 2 methods to start DDMS – Use DDMS from Eclipse 1. Select [Open Perspective]-[Other…]-[DDMS] from menu [Window] of Eclipse. ※There is some functions that are unavailable compared to DDMS that started command. DDMS perspective of Eclipse This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 150
    • 4.6.2. Usage method of DDMS • Debug function of DDMS – Monitoring 1. 2. – Transmitting data to emulator 1. 2. 3. – – Outgoing call SMS sending Transmit position information Acquiring screenshot Process management 1. 2. – Refer to log that application output Refer to usage status resource Execute garbage collection Interrupt process File operation This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 151
    • 4.6.3. Application log reference • Application log reference – We can refer to log that application outputs from LogCat view. LogCat Display log information that be output from application. Log Level Level E Error information W Warning information I Operation information D Debug information V Detail information This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 152
    • 4.6.3. Application log reference • Application log reference – Use log output method of android.util.Log class to output log. ■ Sample code import android.util.Log; Log.e(“HelloWorld”, “Error Message”); // Log that outputs errors Log.w(“HelloWorld”, “Warning Message”); // Log that outputs warnings Log.i(“HelloWorld”, “Infomation Message”); // Log that outputs information of application operation Log.d(“HelloWorld”, “Debug Message”); // Log that outputs debug information Log.v(“HelloWorld”, “Verbose Message”); // Log that outputs detail information Output Log This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 153
    • p r a c t i c e 4.6.4. Output log from Hello World • Output application log when starting HelloWorld 1. 2. Double click on HelloWorld.java from Package Explorer. Confirm to make sure that source code of HelloWorld.java is opened, screen on the right-hand picture is displayed. 2 1 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 154
    • p r a c t i c e 4.6.4. Output log from Hello World 3. Add 5 code that execute log output into HelloWorld#onCreate. ■HelloWorld.java package jp.oesf.tutorial; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class HelloWorld extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.e("HelloWorld", "Error Message"); Log.w("HelloWorld", "Warning Message"); Log.i("HelloWorld", "Infomation Message"); Log.d("HelloWorld", "Debug Message"); Log.v("HelloWorld", "Verbose Message"); 6 } } This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 155
    • p r a c t i c e 4.6.4. Output log from Hello World 4. 5. Select HelloWorld project from Package Explorer, right click then select [Run As]-[Android Application]. Confirm to be sure that emulator runs & right-hand screen is displayed. 5 4 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 156
    • p r a c t i c e 4.6.4. Output log from Hello World 6. 7. Modify perspective of Eclipse into DDMS, confirm to be sure that screen of DDMS is displayed. Confirm to be sure that 5 logs are output in LogCat view. 6 7 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 157
    • 4.6.5. Confirm resource usage status • Refer to resource usage status – Information available to refer and display screen • Info view (Only when executing command) – Display process information (ID, DalvikVM information) • Threads View – Display information of operating threads. • VM Heap View – Display usage status of Heap memory. • Allocation Tracker View – Display memory ensure status (ensure class, method, etc…). • Sysinfo View (only when executing command) – Display in graph format system status (CPU usage rate, memory status). This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 158
    • 4.6.5. Confirm resource usage status • Refer to VM Heap information Set Update into process that you want to display Heap information. Display in list the following items: Type of objects Number of objects Total amount of usage memory Minimum usage memory Maximum usage memory Average usage memory This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 159
    • p r a c t i c e 4.6.6. Transmit call to emulator • Transmit call (Transmit call to emulator) 1. 2. 3. Select Emulator Control View. Input phone number (09010000001) into Incoming number. Select Voice, then click [Call] button. Emulator Control Input incoming number ex. Incoming number : 09010000001 Select Voice Call : Transmit call Hang Up : Hang up the phone 1 Transmit call to Emulator, send SMS, position information. 2 3 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 160
    • p r a c t i c e 4.6.6. Transmit call to emulator 4. Confirm incoming call from 09010000001 to emulator. 4 Incoming call to emulator from 09010000001 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 161
    • 4.6.7. Send SMS to emulator • SMS sending (Send SMS to emulator) 1. 2. 3. Select Emulator Control View. Input phone number (09010000001) into Incoming number. Select SMS, input sending message (Hello Android) into Message, then click [Send] button. Input incoming number ・message ex. Incoming number : 09010000001 Select SMS Message : Hello Android 2 3 Send : Sending This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 162
    • 4.6.7. Transmit SMS to emulator 4. Confirm to be sure that message was transmitted from 09010000001 to emulator. Notify “SMS receiving” Message that sent into Messaging application, will be recorded. 4 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 163
    • 4.6.8. Send position information to emulator • Send position information 1. 2. 3. 4. Select Emulator Control view. Open Manual tab, select Decimal. Input Longitude/Latitude. Click on [Send] button. This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 164
    • 4.6.9. Other functions of emulator • Acquire screenshots 1. 2. When you click on [Screen Capture] button, screenshots of terminal will be displayed on Device Screen Capture window. Click [Save] button, then save file from Save window. 1 2 Preview display will be displayed on Device Screen Capture. There are some function including Save as below: Refresh : Re-acquire Save : Save Done : Finish This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 165
    • 4.6.9. Other functions of emulator • Execute garbage collection 1. 2. Select process that executed garbage collection from Devices View. Click on [Cause GC] button in Heap View 2 1 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 166
    • 4.6.9. Other functions of emulator • Stop process 1. 2. Select process that executed garbage collection from Devices View. Click on [Stop Process] button in Devices View. 2 1 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 167
    • 4.6.9. Other functions of emulator • Acquire/Forward/Delete file within Terminal device 1. 2. Select FileExplorer View. Click on buttons correspond to operation (Acquire/Forward/Delete file) 1 File Explorer Display file information within terminal device. Reference scope is limited because there is limit access for real device. 2 : Retrieve file from terminal : Forward file to terminal : Delete file This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 168
    • 5. Practice This material is licensed under the Creative Copyright 2009, Open Embedded Software Foundation, All rights reserved Commons License BY-NC-SA 4.0. 169
    • 5.1. Outline How to practice? • Learn how to develop Android application in 5 steps Step 2: The method to make screen transition Step 1: The method to make screen design - Make menu screen - Recognize “Button has been clicked“ - Add screen in List format - Use intent to make screen transition Step 4: Search database with specific criteria & display data in detail screen - Receive and send parameters between activities - Make narrow search from database - Set screen view properties from the program Step 3: SQLite database operations & display data in list format - Create database and table - Search data from database - Display searching result in list format Step 5: How to create option menu - Add option menu to application - Recognize “option menu clicked” - Display dialogue on screen This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 170
    • 5.1. Outline Preparation for practice • Take skeleton project for practice in Eclipse. – Storage place • C:android_trainingskeleton_project – Take 「RssReaderSkeleton1」in this folder. – How to take it in? • Start Eclipse, select 「Import」 from 「File」menu, display 「Import」 dialog. • Select 「Existing Projects into Workspace」 from 「General」 in 「Import」 dialogue, and press 「Next」 button, specify then take root directory in skeleton project in. – Make a check mark in 「Copy projects into workspace」. This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 171
    • 5.1. Outline Practice application outline • Create RSS Reader in practice – RSS reader created in practice will acquire RSS feed ( information in XML format) from internet and be saved in database. Search database, then display RSS feed on screen. Get RSS feed from internet Menu screen Register RSS feed into database. Internet Database Search RSS feed from database After registration into database finished, display dialogue. List screen Click “List display” button Select List data This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved Detail screen 172
    • 5.1. Outline Practice application outline • RSS reader screen and function outline – RSS reader created in practice, is configured by 3 screens. RSS reader Start ① Menu screen ・・・ Direction of screen transition ② List screen ③ Detail screen # Screen name Function outline 1 Menu screen • Start List screen • Display option menu • After clicking on option menu, get RSS feed from internet then register into database • After getting RSS feed, display Finish message on dialogue. 2 List screen • Display RSS feed that is registered in database in List format. 3 Detail screen • Display title, sending time, sender’s name, detail content of RSS feed that is selected in List screen. This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 173
    • 5.2. The method of making screen design • Learn how to make screen design in Android application. • Learn the following via practice. – Method of making screen design using Layout editor. – Method of creating string character resource file using Resource editor. This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 174
    • 5.2. The method of making screen design Create menu screen • Method of making screen design using Layout editor. – We can make screen design based on resource file in XML format in Android application. 1. Resource file Open (res/layout/main.xml), start layout editor 2 (Refer to「4.2.1. Create resource file」for Layout editor) 2. Display in Menu screen Arrange 「List display」button by Drag & Drop 1 Arrange 「Button」 from 「Views」 by Drag & Drop (Refer to「4.2.1. Create resource file」for method of arranging button) Use Layout editor to open res/layout/main.xml This material is licensedEmbedded Creative Commons License BY-NC-SA 4.0. Copyright 2009, Open under the Software Foundation, All rights reserved 175
    • 5.2. The method of making screen design Create menu screen 3. Use Properties View, set properties of arranged button. – Set id, layout_width, layout_height 3 3. Set id, height, width in properties. This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 176
    • 5.2. The method of making screen design Create menu screen • Setting value of properties – id • id that use when refers to View from program. • Format of setting value – @+id/<option id> • Set example (In case of putting id that called ”a_button” on button) – @+id/a_button – layout_width • Set the width of View • Format setting value – Numerical value +Unit ・・・ Specify by numerical value. Specify px, dip, etc … as for unit. – wrap_content ・・・ Fit the width of display character string, etc … in View. – fill_parent ・・・ Fit automatically numerical value in new View. – layout_height • Set height of View • Format of setting value is similar to layout_width This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 177
    • 5.2. The method of making screen design Create menu screen • Confirm to be sure that the screen created in emulator will be displayed. – Select project in Android application, right-click then select 「Android Application」 from 「Run As」. Specify 「RssReader」project, and right-click Implement application Select 「Android Application」 from 「Run As」 in menu that is displayed when right –click This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 178
    • 5.2. The method of making screen design Create menu screen • Use resource editor, set character string displayed on screen into character string resource file (res/values/strings.xml). – We can collect character string that displayed on the screen into resource file in Android application. ■Character string resource file (res/values/strings.xml) Add character string that display on the screen into character string resource file. <?xml version="1.0" encoding=“utf-8"?> <resources> <string name=“app_name”> RssReader </string> <string name=“go_to_list_page_button_label”> List Display</string> </resources> Refer to character string ■Resource file of screen design (res/layout/main.xml) Refer to character string of character <Button string resource file from button android:id="@+id/list_button" properties. android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=“@string/go_to_list_page_button_label“ /> This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 179
    • 5.2. The method of making screen design Create menu screen • Refer to character string that display in button be disposed in menu screen from character string resource file. – Use resource editor, open character string resource file (res/values/strings.xml). – Add setting of button display character string into character string resource file. (Refer “4.3. Use resource besides design” for additional procedure) – Use Layout Editor, open Screen design resource file (res/layout/main.xml), use character string of character string resource file by character string of button properties. – Execute application, confirm to be sure that character string is displayed. This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 180
    • p 実 5.2. The method of making screen r 習 Create menu screen a c t Practice 1 i c • Practice theme – Create screen design of Menu screen. e design • There is 1 button in Menu screen. • Display “List Display” in the button. This material is licensed under the Creative Copyright 2009, Open Embedded Software Foundation, All rights reserved Commons License BY-NC-SA 4.0. 181
    • p 実 5.2. The method of making screen design r 習 Create menu screen a c t Practice 1 i c • Practice procedure 1. Open resource file (res/layout/main.xml) by Layout Editor, arrange e buttons in Menu screen. 2. Set properties (id, height, width) for arranged buttons. 3. Add “List Display” to character string displayed in button into character string resource file (res/values/strings.xml). 4. Change setting of character string resource file that Text properties of the button is set into “Reference”. This material is licensed under the Creative Copyright 2009, Open Embedded Software Foundation, All rights reserved Commons License BY-NC-SA 4.0. 182
    • p 実 5.2. The method of making screen design r 習 Create menu screen a c t Practice 1 i – Setting information 1 c • Set the following value for View properties arranged in the screen. e # 2 View Properties 1 1 LinearLayout 1. android:layout_width=“fill_parent” 2. android:layout_height=“fill_parent” 2 Button 1. 2. 3. 4. android:id=“@+id/list_button” android:layout_width=“fill_parent” android:layout_height=“wrap_content” android:text=“@string/go_to_list_page_button_label” This material is licensed under the Creative Copyright 2009, Open Embedded Software Foundation, All rights reserved Commons License BY-NC-SA 4.0. 183
    • p 実 5.2. The method of making screen design r 習 Create menu screen a c t Practice 1 i – Setting information 2 c • Add the following value character string into character string resource file. e ※ The format added into character string resource file will be <string name=“Key”>Value</string> Key Value Content go_to_list_page_button_label List display Character string displayed in button 「List display」 This material is licensed under the Creative Copyright 2009, Open Embedded Software Foundation, All rights reserved Commons License BY-NC-SA 4.0. 184
    • p 実 5.2. The method of making r 習 Create menu screen a c t Practice 1 i – Setting information 3 c • Create the following resource file. e Screen Menu screen - Resource file screen design Description res/layout/main.xml (Resource file) • Arrange button “List display” in Menu screen res/values/strings.xml (Character string resource file) • Add character string “List display” displayed in the button “List display”. This material is licensed under the Creative Copyright 2009, Open Embedded Software Foundation, All rights reserved Commons License BY-NC-SA 4.0. 185
    • p 実 5.2. The method of making screen design r 習 Create menu screen a c t Practice 1 i c • Verification method – Execute the practice application, make sure that “List display button” is e displayed. This material is licensed under the Creative Copyright 2009, Open Embedded Software Foundation, All rights reserved Commons License BY-NC-SA 4.0. 186
    • p 実 5.2. The method of r 習 Create menu screen a c t Practice 1 i c • Practice answer – res/layout/main.xml e making screen design <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent“ > <Button android:id="@+id/list_button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/go_to_list_page_button_label" /> </LinearLayout> This material is licensed under the Creative Copyright 2009, Open Embedded Software Foundation, All rights reserved Commons License BY-NC-SA 4.0. 187
    • p 実 5.2. The method of r 習 Create menu screen a c t Practice 1 i c • Practice answer – res/values/strings.xml e making screen design <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">RssReader</string> <string name="go_to_list_page_button_label"> List display </string> </resources> This material is licensed under the Creative Copyright 2009, Open Embedded Software Foundation, All rights reserved Commons License BY-NC-SA 4.0. 188
    • 5.3. The method of screen transition • Learn how to make screen transition in Android application • Learn the following by practice – The method of recognizing that “Button has been clicked”. – The method of adding the screen in list format to application. – The method of implementing screen transition used Intent class. This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 189
    • 5.2. The method of making screen transition Recognize “Button has been clicked” • How to recognize “Button has been clicked” – Mechanism • When you click a button, “Event” will be created. Prepare “Event listener” to recognize this event by program. • Register “Event listener” that has been created beforehand to the button that you want to recognize event. • When event created by button, specified method will be retrieved. Event listener public void onClick(View view) { Click button // Process Event raised } Processing will be executed This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 190
    • 5.2. The method of making screen transition Recognize “Button has been clicked” • How to execute processing when after clicking the button on the screen? 1. 2. 3. Event listener implement interface 「OnClickListener」. Implement method onClick in “event listener”. Use method View#setOnClickListener to register “event listener” to button. • Sample code (Activity will be used as “event listener” in Sample code) import android.app.Activity; import android.view.View; import android.view.View.OnClickListener; public class SampleActivity extends Activity implements OnClickListener { ・・・1 public void onClick(View view) { ・・・・・・・・ ・・・2 findViewById } Method of class Activity that @Override public void onCreate(Bundle savedInstanceState) { acquire View object contain id super.onCreate(savedInstanceState); specified by argument. setContentView(R.layout.main); View listButton = findViewById(R.id.list_button); ・・・3 listButton.setOnClickListener(this); } } This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 191
    • 5.2. The method of making screen transition Recognize “Button has been clicked” • How to decide View clicked by method onClick? – Get “id” of View from argument “view” to decide. • Sample code (How to decide whether the 「list_button」 of the screen has been clicked?) public void onClick(View view) { if (view.getId() == R.id.list_button) { ・・・・・・・・ } } View#getId Method acquiring id from object View This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 192
    • p 実 5.2. The method of making screen transition r 習 Recognize “Button has been clicked” a c t Practice 2 i c • Practice theme – When you click on the button “List display” of menu screen, log will be e output. Click on the button “List display” This material is licensed under the Creative Copyright 2009, Open Embedded Software Foundation, All rights reserved Commons License BY-NC-SA 4.0. 193
    • p 実 5.2. The method of making screen transition r 習 Recognize “Button has been clicked” a c t Practice 2 i c • Practice procedure 1. Implement OnClickListener to make RssReaderActivity “event listener” e 2. Output log by method onClick of RssReaderActivity. • Tag:「RssReaderActivity 」 • Message:「Clicked.」 3. Register “event listener” to button “List display” by RssReaderActivity #onCreate. This material is licensed under the Creative Copyright 2009, Open Embedded Software Foundation, All rights reserved Commons License BY-NC-SA 4.0. 194
    • p 実 5.2. The method of making screen r 習 Recognize “Button has been clicked” a c t Practice 2 i – Setting information c • Implement the following class, method e Screen Class Method onCreate Menu screen RssReaderActivity onClick transition Description • Register “event listener” of the button “List display”. • Output log Log message 「Clicked.」 This material is licensed under the Creative Copyright 2009, Open Embedded Software Foundation, All rights reserved Commons License BY-NC-SA 4.0. 195
    • p 実 5.2. The method of making screen transition r 習 Recognize “Button has been clicked” a c t Practice 2 i c • Verification method – Verify output of Log message by LogCat after pressing “List display” e button of Menu screen. Click “List display” button Check output of Log message “Clicked.” This material is licensed under the Creative Copyright 2009, Open Embedded Software Foundation, All rights reserved Commons License BY-NC-SA 4.0. 196
    • p 実 5.2. The method of making screen r 習 Recognize “Button has been clicked” a c t Practice 2 i c • Practice answer – RssReaderActivity e transition package jp.oesf.mtgeduwg.training.rssreader; import android.app.Activity; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; public class RssReaderActivity extends Activity implements OnClickListener { public void onClick(View view) { Log.v("RssReaderActivity","Clicked"); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); View listButton = findViewById(R.id.list_button); listButton.setOnClickListener(this); } } This material is licensed under the Creative Copyright 2009, Open Embedded Software Foundation, All rights reserved Commons License BY-NC-SA 4.0. 197
    • 5.3. The method of making screen transition Add the screen in list format • How to add the screen in list format to Android application – Create new resource file screen design. – Create screen design in List display. – Create Activity class of additional screen. This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 198
    • 5.3. The method of making screen transition Add the screen in list format • Create new screen design resource file – Procedure 1. 2. 3. 4. Select directory res/layout Press additional button Android XML Input file name of created resource file Press Finish button 2 3 You can choose View group such as LinearLayout, TableLayout, ScrollView, … 1 4 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 199
    • 5.3. The method of making screen transition Add the screen in list format • How to create screen in list format – Mechanism • Create 2 screen design resource files in “screen design of line” and “screen design of frame” to create screen in List format. • Use ListView to create screen design in List format for “screen design of frame”. • Create connection between “screen design of frame” and “screen design of line” by program. Screen design Screen design of frame represents frame Screen design of line Screen design represents line ・・ ・ This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 200
    • 5.3. The method of making screen transition Add the screen in list format • How to create screen design of frame 1. Create new resource file of frame design in res/layout/. 2. Arrange ListView in screen design resource file created in step 1. 3. Set 「@android:id/list」 to properties 「id」 of ListView. • Make sure to set 「@android:id/list」 to 「id」, in order to list display usage ListView function. Screen design of frame Screen design of line ・ ・ ・ ■list.xml <?xml version="1.0" encoding=“utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 201
    • 5.3. The method of making screen transition Add the screen in list format 4. In case there is no data to be displayed, you can display View instead. In that case, arrange View used for alternative display in screen design & set 「@android:id/empty」 to 「id」. ■list.xml <?xml version="1.0" encoding=“utf-8"?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@android:id/empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/empty" /> </LinearLayout> This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 202
    • 5.3. The method of making screen transition Add the screen in list format • Method of creating screen design of line 1. 2. Create new resource file of line design in res/layout/. Open new screen design resource file, arrange View displayed in 1 record of the List. ■list_row.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:text="" android:id="@+id/feed_title" android:layout_width="wrap_content" android:layout_height="wrap_content"> </TextView> </LinearLayout> This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 203
    • 5.3. The method of making screen transition Add the screen in list format • Create new Activity class of Screen – Procedure 1. Choose [New]-[Class] from menu [File] of Eclipse. 1 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 204
    • 5.3. The method of making screen transition Add the screen in list format 2. 3. 4. Input Activity class name that was created in Name, package that creates class in Package. Set subclass of Activity class or class 「android.app.Activity」 in SuperClass. Select Activity class according to created screen. ※In case creating List screen, set 「android.app.ListActivity」. Press Finish button Set 「android.app.ListActivity」 to SuperClass to add Activity of List screen 2 3 4 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 205
    • 5.3. The method of making screen transition Add the screen in list format 5. Implement method Activity#onCreate in Activity class created in step 4. 6. Run method onCreate of new class in method Activity#onCreate. 7. After running method onCreate of new class, run method Activity#setContentView, then display screen design. Set argument of method Activity#setContentView that obtained id of additional screen design from R class. • Sample code package jp.oesf.mtgeduwg.training.rssreader; import android.app.ListActivity; import android.os.Bundle; public class RssListActivity extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list); } ・・・5 ・・・6 ・・・7 } This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 206
    • p 実 5.3. The method of making r 習 Add the screen in list format a c t Practice 3 i c • Practice theme – Create screen design of List screen e screen transition • In case there is data to be displayed, display title of RSS Feed. • In case there is no data to be displayed, display “No data”. Title of RSS Feed “Display data exist” case “Display data no exist” case This material is licensed under the Creative Copyright 2009, Open Embedded Software Foundation, All rights reserved Commons License BY-NC-SA 4.0. 207
    • p 実 5.3. The method of making screen transition r 習 Add the screen in list format a c t Practice 3 i c • Practice procedure 1. Create new Screen design resource files(res/layout/list.xml)for e Framework design of Screen list 2. Use Layout Editor to open the screen design resource files of Framework design, put ListView 3. In case of there isn’t data display in list, put a TextView to display a message 4. Set up the properties for ListView, TextView 5. Add the message displayed in case of there isn’t data display in list into string resource files(res/values/strings.xml) This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.3. The method of making screen transition r 習 Add the screen in list format a c t Practice 3 i 6. Refer the setting of string resource files added in 5. to modify the text c properties of Textview put in 3. e 7. Create new Screen design resource files (res/layout/list_row.xml) for Line design of Screen list 8. Use Layout editor to open Line design resource files, put TextView that be set up the RSS feed for title character 9. Modify TextView properties put in 8. 10. Create new RssListActivity • RssListActivity is the class that be created by inheriting ListActivity 11. Override the RssListActivity#onCreate method 12. Execute setContentView in/by RssListActivity#onCreate method, load res/layout/list.xml of Screen design resource files This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.3. The method of making r 習 Add the screen in list format a c t Practice 3 i – Setting information 1 c • Create the following resource files e Screen screen transition Resource files Content The whole • Put the ListView that displays the data in list form • Put the TextView that displays an alternative in case of there isn’t data to view res/layout/list_row.xml Screen list res/layout/list.xml • Put View that displays RSS feed title res/values/strings.xml • Add the setting for string displayed in alternative TextView – Setting information 2 • Add the following string into String resource files(res/values/strings.xml) ※ The form for addition into String resource files is <string name=“key”>value</string> Key Value empty There isn’t data This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.3. The method of making screen r 習 Add the screen in list format a c t Practice 3 i – Setting information 3 c • Implement the following class, method e Screen Class Method Screen list RssListActivity onCreate transition Overview • Display the res/layout/list.xml of Screen design by/in setContentView method This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.3. The method of making screen transition r 習 Add the screen in list format a c t Practice 3 i – Setting information 4 c • Set up the following information for View properties placed in framework of e Screen design 1 2 # View Property 1 ListView 1. android:id=“@android:id/list” 2. android:layout_width=“fill_parent” 3. android:layout_height=“wrap_content” 2 TextView 1. 2. 3. 4. This material is licensed under the Creative Commons License BY-NC-SA 4.0. android:id=“@android:id/empty” android:layout_width=“wrap_content” android:layout_height=“wrap_content” android:text=“@string/empty”
    • p 実 5.3. The method of making screen transition r 習 Add the screen in list format a c t Practice 3 i – Setting information 5 c • Set up the following information for View placed in line of Screen design e 1 # View Property 1 TextView 1. android:id=“@+id/feed_title” 2. android:layout_width=“fill_parent” 3. android:layout_height=“wrap_content” This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.3. The method of making screen transition r 習 Add the screen in list format a c t Practice 3 i c • Verify method – Compare the following resource files and Java source code with the e answers of practice, use source-based to verify • Resource files – res/layout/list.xml – res/layout/list_row.xml – res/layout/strings.xml • Java source code – RssListActivity.java This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.3. The method of making r 習 Add the screen in list format a c t Practice 3 i c • Practice Answers – res/layout/list.xml e screen transition <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@android:id/empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/empty" /> </LinearLayout> This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.3. The method of making r 習 Add the screen in list format a c t Practice 3 i c • Practice Answers – res/layout/list_row.xml e screen transition <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:text="" android:id="@+id/feed_title" android:layout_width="wrap_content" android:layout_height="wrap_content"> </TextView> </LinearLayout> This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 r 習 a c t i c e 5.3. The method of making screen transition Add the screen in list format Practice 3 • Practice Answers – res/values/strings.xml <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">RssReader</string> <string name="go_to_list_page_button_label"> display list </string> <string name="empty"> No data </string> </resources> This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.3. The method of making r 習 Add the screen in list format a c t Practice 3 i c • Practice Answers – RssListActivity e screen transition package jp.oesf.mtgeduwg.training.rssreader; import android.app.ListActivity; import android.view.View; import android.widget.ListView; public class RssListActivity extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list); } } This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.3. The method of making screen transition Use Intent to perform Screen transition • Perform Screen transition – Mechanism • • • Create an intent object from the information of Screen transition source Use the created intent object to execute the method that calls out Activity, the Screen transition target can be displayed When transiting screen to new created screen, the activities of Screen transition target must be registered in AndroidManifest.xml Create Intent object Screen A Intent Intent . “Key”, “Value” Android Screen B Intent The image of transition from Screen A to Screen B This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.3. The method of making screen transition Use Intent to perform Screen transition • The method of using intent to perform Screen transition 1. Create intent object • Give the object of activities of transition source (self screen) and the class information of activities of transition target into argument of intent, then create 2. Execute Activity#startActivity method. Give the Intent object that created in 1. into argument of method • Sample source (sample of transiting from SampleActivity to NextActivity) import android.app.Activity; import android.view.View; import android.view.View.OnClickListener; import android.content.Intent; public class SampleActivity extends Activity implements OnClickListener { public void onClick(View view) { Intent intent = new Intent(this,NextActivity.class); ・・・1 startActivity(intent); ・・・2 } } This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.3. The method of making screen transition Use Intent to perform Screen transition • Insert the setting of AndroidManifest.xml into added screen – To use the added screen from application, set up for AndroidManifest.xml • Refer set up method in 「 4.4.1. Insert the Activity into Application」 – If not perform the setting for AndroidManifest.xml, when execute Screen transition, the error will occur • Sample code(in case: insert NextActivity into AndroidManifest.xml) <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="jp.oesf.sample” android:versionCode="1” android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".SampleActivity” android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> Insert the setting of <category android:name="android.intent.category.LAUNCHER" /> NextActivity class </intent-filter> </activity> <activity android:name="NextActivity" android:label="@string/next_activity_title"></activity> </application> <uses-sdk android:minSdkVersion=“7" /> </manifest> This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.3. The method of making screen transition r 習 Use Intent to perform Screen transition a c t Practice 4 i c • Practice theme – When pressed on [ Display list] button on menu screen, the process of e transiting screen to screen list is created Click on Display list Transiting screen This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.3. The method of making screen transition r 習 Use Intent to perform Screen transition a c t Practice 4 i c • Practice procedure 1. Create the Intent object that is transited to Screen list when pressed on e 2. 3. [Display list] on RssReaderActivity class Use Activity#startActivity method to execute the process of transiting screen to Screen list Insert the setting of RssListActivity class of Screen list into AndroidManifest.xml This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.3. The method of making screen transition r 習 Use Intent to perform Screen transition a c t Practice 4 i – Setting information c • Implement the following Class, method e Screen Class Method Menu screen RssReaderActivity onClick Overview • Start Display list screen when Display list button is clicked This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.3. The method of making screen transition r 習 Use Intent to perform Screen transition a c t Practice 4 i c • Verify method – Verify the transiting screen to Screen list when pressed the [Display list] e button on menu screen – When there isn’t data display in list, the message [No data] is displayed Click on Display list Transiting screen This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.3. The method of making screen transition r 習 Use Intent to perform Screen transition a c t Practice 4 i c • Practice Answers – RssReaderActivity#onClick e package jp.oesf.mtgeduwg.training.rssreader; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; public class RssReaderActivity extends Activity implements OnClickListener { ・・・・・・・・ public void onClick(View view) { if (R.id.list_button == view.getId()) { Intent intent = new Intent(this, RssListActivity.class); startActivity(intent); } } } This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.3. The method of making screen transition r 習 Use Intent to perform Screen transition a c t Practice 4 i c • Practice Answers – AndroidManifest.xml e <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="jp.oesf.mtgeduwg.training.rssreader" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"> <activity android:name="RssReaderActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN"></action> <category android:name="android.intent.category.LAUNCHER"></category> </intent-filter> </activity> <activity android:label="@string/app_name" android:name="RssListActivity"></activity> </application> <uses-sdk android:minSdkVersion="3" /> <uses-permission android:name="android.permission.INTERNET"></uses-permission> </manifest> This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.4. Search Database in all cases and Display list • Learn the method of researching data by operate database (SQLite) from application • Learn the following things by practicing • The method of operating SQLite from Android • The method of researching data from database • The method of displaying research results in Screen list This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.4. Search Database in all cases and Display list • What is SQLite? – Lightweight Database • • • • Use simple single file database to store data Can use SQL for Database operations Can manage transactions Specifications, source code are being published, drivers have been developed in many languages – Reference • http://www.sqlite.org/ • http://ja.wikipedia.org/wiki/SQLite This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.4. Search Database in all cases and Display list • Operating database(SQLite)is – Use the following classes that provided by Android • SQLiteOpenHelper class – Create Database – Create Table – Perform Database connection ( Open)/ disconnection (Close) • SQLiteDatabase class – Perform Table search/insert/ renew/ delete – Execute the specified SQL This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.4. Search Database in all cases and Display list • Overview of the processing from database to search data This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.4. Search Database in all cases and Display list Create database, table and search in database • Use SQLiteOpenHelper to create Database – Mechanism • In order to create Database, the inherited subclass of SQLiteOpenHelper class must be created • SQLite Database file is created by the execution of SQLiteOpenHelper class constructor that executed by subclass constructor • When Database file is already existing, even if constructor is executed in 2nd time or more, Files can not be created. Database file only be created when files are inexistent This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.4. Search Database in all cases and Display list Create database, table and search in database • Use SQLiteOpenHelper to create Database – Mechanism • When Database is created by SQLiteOpenHelper, onCreate method is executed. By executing the SQL that tables be created by onCreate method, the Tables can be created. • Need SQLiteDatabase objects to execute SQL. Because of SQLiteDatabase objects can be given by SQLiteOpenHelper#onCreate, use SQLiteDatabase objects to execute SQL. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.4. Search Database in all cases and Display list Create database, table and search in database • The method of Creation Database, Table by using SQLiteOpenHelper 1. 2. Create subclass of SQLiteOpenHelper class Execute the constructor of SQLiteOpenHelper class by constructor of the class created in 1., create Database 3. Implement 2 methods that are decided by SQLiteOpenHelper • onCreate : the method that implements the Table creation process • onUpgrade : the table that implements the Table definition renew process ※ In SQLiteOpenHelper, implementation of the methods above is an obligation, if not implement, it will be compile error 4. Execute the SQL statement that creates Table (CREATE TABLE statement)at SQLiteOpenHelper#onCreate • SQL statements execution method 1. Create the string for the SQL statement that creates Table 2. onCreate method SQLiteDatabase database, in the 1st argument of execSQL method, provide the string for SQL statement, then execute This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.4. Search Database in all cases and Display list Create database, table and search in database • Sample code import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; In sample code, the subclass name is 「SampleSQLiteOpenHelper」 public class SampleSQLiteOpenHelper extends SQLiteOpenHelper { ・・・1 public SampleSQLiteOpenHelper(Context context) { // Create Database (Database name is 「 SAMPLE_DATABASE 」) super(context, “SAMPLE_DATABASE”, null, 1); } ・・・2 // The method that implements the Table creation process @Override public void onCreate(SQLiteDatabase database) { // Execute CREATE TABLE statement database.execSQL(“CREATE TABLE SAMPLE_TABLE(_id INTEGER,name TEXT);”); } ・・・3 ・・・4 // The method that implements the Table definition renew process @Override public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) { } } This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.4. Search Database in all cases and Display list Create database, table and search in database • Search data from database – Mechanism • Need SQLiteDatabase objects to search data from database • By executing SQLiteDatabase#query method, the results can be gotten by Cursor class • Get SQLiteDatabase objects by SQLiteOpenHelper method. Depend on uses, there are 2 methods of getting SQLiteDatabase – getReadableDatabase ・・・ get SQLiteDatabase objects that have specialized reading » Only use in case of execute the searching – getWritableDatabase ・・・ get SQLiteDatabase objects that have written ability » Use in case of execute the process of writing/entry into Table ※ In case of execute the searching only, use getReadableDatabase method This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.4. Search Database in all cases and Display list Create database, table and search in database • Beside the query method for Search execution, SQLiteDatabase class also provides the following methods. By using these methods, the information of Database can be gotten or renewed Methods Overview query() Search Data insert() Register Data update() Renew Data delete() Delete Data execSQL() Execute the optional SQL statement to operate data In table: The typical methods of SQLiteDatabase and overview ■ Refer the detail of SQLiteDatabase in Reference of Android Developers http://developer.android.com/intl/ja/reference/android/database/sqlite/SQLiteDatabase.html This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.4. Search Database in all cases and Display list Create database, table and search in database • The method of Search data by using SQLiteDatabase#query method 1. 2. 3. 4. Create the objects of SQLiteOpenHelper subclass Use SQLiteOpenHelper#getReadableDatabase method to get SQLiteDatabase objects Execute SQLiteDatabase#query method to get the Cursor objects of Search results If using Cursor at Activity, execute Activity#startManagingCursor to transmit Cursor management to Activity ※ By transmitted Cursor management to Activity, the management of Cursor object ( such as destruction…) is performed automatically 5. Execute SQLiteDatabase#close method to close database This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.4. Search Database in all cases and Display list Create database, table and search in database • Sample code SampleSQLiteOpenHelper databaseOpenHelper = new SampleSQLiteOpenHelper(this); SQLiteDatabase database = null; Cursor cursor = null; try { // Get the SQLiteDatabase objects that have specialized reading database = databaseOpenHelper.getReadableDatabase(); // Execute the Searching, get the Cursor object of searching results cursor = database.query(“SAMPLE_TABLE", null, null, null, null, null, null); // Transfer the Cursor object management to Activity startManagingCursor(cursor); } finally { if (database != null) { // Disconnect/ cut off with Database database.close(); } } This material is licensed under the Creative Commons License BY-NC-SA 4.0. ・・・2 ・・・3 ・・・4 ・・・5 ・・・1
    • 5.4. Search Database in all cases and Display list Create database, table and search in database • The method of executing the specified SQL by using SQLiteDatabase#execSQL method – Sample code( Execution of CREATE TABLE statement) import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class SampleSQLiteOpenHelper extends SQLiteOpenHelper { ・・・・・・・・ // The method that implements the Table creation process @Override public void onCreate(SQLiteDatabase database) { // Execute the CREATE TABLE statement database.execSQL(“CREATE TABLE SAMPLE_TABLE(_id INTEGER,name TEXT);”); } ・・・・・・・・ } This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 r 習 a Create database, table and search in database c t Practice 5 i c • Practice theme – Create Database and Table when starting Screen list e – Send the Database files that used for search test to emulator. After that, execute Table search and get Cursor object 5.4. Search Database in all cases and Display list ※The subclass of SQLiteOpenHelper is provided at Skeleton project as DatabaseOpenHelper class ※The Database files for search test are also provided as the same ※Use the Table called RSS_FEED to perform searching. The SQL statement that creates RSS_FEED table is provided as an invariable/ constant at DatabaseOpenHelper This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 r 習 a Create database, table and search in database c t Practice 5 i c • Practice procedure 1. Execute the SQL statement that creates RSS_FEED table by e DatabaseOpenHelper#onCreate method 5.4. Search Database in all cases and Display list • SQL statements are provided at Skeleton project 2. Use RssListActivity#onResume method to execute the follows process 1. Execute DatabaseOpenHelper#getReadableDatabase method, get SQLiteDatabase object 2. After getting SQLiteDatabase object, in Log message that outputs open log, output 「Succeeded in open the database.」 3. After outputing open log, execute SQLiteDatabase#query method, then execute the RSS_FEED table Search 4. After executing SQLiteDatabase#query method, if the returned Cursor object of query method is not null value, output the Search log. Output 「 Got cursor.」in Log message This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 r 習 a Create database, table and search in database c t Practice 5 i 5. After outputting the Search log, close Database c 6. After closing Database, output Close log e In Log message, output 「Succeeded in close the database. 」 5.4. Search Database in all cases and Display list 7. Use DDMS to transmit Database files used for Search test (files of SQLite) to Emulator ※ Sending files method is written in next page This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 r 習 a Create database, table and search in database c t Practice 5 i ※ The procedure of sending Database files used for Search test to c Emulator by using DDMS e 5.4. Search Database in all cases and Display list 1. Select Eclipse menu [Window]-[Open Perspective]-[Other…] to open 「Open Perspective 」window 2. Select 「DDMS」from Open Perspective window, open DDMS perspective 2 1 Select Other… This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 r 習 a Create database, table and search in database c t Practice 5 i 3. Open File Explorer view of DDMS perspective, select c 「 data/data/jp.oesf.mtgeduwg.training.rssreader/databases 」, then press e 「Push a file onto the device」button 5.4. Search Database in all cases and Display list 4. On File selection dialog, select data stored in 「C:android_trainingtestdata」 4 3 This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 r 習 a Create database, table and search in c t Practice 5 i – Setting information 1 c • Implement the following classes, methods e 5.4. Search Database in all cases and Display list Screen Class Method - DatabaseOpenHelper Constructor database Overview • Create Database • Database name is 「data」 onCreate Screen list RssListActivity • Create Table in Database • RSS_FEED table onResume • Connect/ open to Database • Search all RSS_FEED Tables, get Search results • Disconnect/ close to Database This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 r 習 a Create database, table and c t Practice 5 i – Setting information 2 c • Create the following Tables e 5.4. Search Database in all cases and Display list search in database – RSS_FEED ※ The SQL statements that used for Table creation are provided at DatabaseOpenHelper class of Practice Application Column name Type Overview _id INTEGER GUID TEXT • The identifier assigned into RSS feed TITLE TEXT • Title PUBLISH_DATE TEXT • Delivery/send date DESCRIPTION TEXT • Detail content LINK TEXT • Link SENDER_NAME TEXT • The name of deliverer/ sender • Primary key • Auto increment This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 r 習 a Create database, table and search in database c t Practice 5 i c • Verification method – When started the Screen list, verify the following output log by LogCat e 5.4. Search Database in all cases and Display list • Open log • Search log • Close log 「Succeeded in open the database.」 「Got cursor. 」 「Succeeded in close the database. 」 This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 r 習 a Create database, table c t Practice 5 i c • Practice Answers – DatabaseOpenHelper e 5.4. Search Database in all cases and Display list and search in database package jp.oesf.mtgeduwg.training.rssreader.helper; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class DatabaseOpenHelper extends SQLiteOpenHelper { /** Database File name */ private static final String DATABASE_NAME = "data"; /** The SQL that creates RSS Feed table */ private static final String CREATE_RSS_FEED_TABLE = "CREATE TABLE RSS_FEED(" + "_id INTEGER PRIMARY KEY AUTOINCREMENT," + "SENDER_NAME TEXT, URL TEXT, TITLE TEXT, PUBLISH_DATE TEXT, DESCRIPTION TEXT, LINK TEXT," + "GUID TEXT);“; This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 r 習 a Create database, table and search c t Practice 5 i c • Practice Answers – DatabaseOpenHelper (Continue) e 5.4. Search Database in all cases and Display list in database public DatabaseOpenHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase database) { database.execSQL(CREATE_RSS_FEED_TABLE); } @Override public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) { } } This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 r 習 a Create database, table and c t Practice 5 i c • Practice Answers – RssListActivity#onResume e 5.4. Search Database in all cases and Display list search in database protected void onResume() { super.onResume(); DatabaseOpenHelper databaseOpenHelper = new DatabaseOpenHelper(this); SQLiteDatabase database = null; Cursor cursor = null; try { database = databaseOpenHelper.getReadableDatabase(); Log.v("RssListActivity", "Succeeded in open the database."); cursor = database.query("RSS_FEED", null, null, null, null, null, null); startManagingCursor(cursor); if(cursor != null){ Log.v("RssListActivity", "Got cursor."); } } finally { if (database != null) { database.close(); Log.v("RssListActivity", "Succeeded in close the database."); } } } } This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.4. Search Database in all cases and Display list Display Data in list form • Display on Screen the data searched from Database in list form – Mechanism • Need ListView and ListActivity class to display in list the data gotten from Cursor on screen • Android provides the mechanism called Adapter in order to display in list the Cursor information • By using Cursor object, ListActivity and Adapter in association, the Cursor information is displayed in ListView of the Screen Cursor Object This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.4. Search Database in all cases and Display list Display Data in list form • The method of displaying Cursor object content on list screen 1. 2. Create SimpleCursorAdapter object from Cursor object Use ListActivity#setListAdapter method to register SimpleCursorAdapter object in ListActivity • Sample code public class RssListActivity extends ListActivity { Use the following arguments to create ・・・・・・・・ SimpleCursorAdapter object @Override • Activity protected void onResume() { • Line design id super.onResume(); DatabaseOpenHelper databaseOpenHelper = • Cursor new DatabaseOpenHelper(this); • The key of Data gotten from Cursor SQLiteDatabase database = null; • The id of View placed on Line design for Cursor cursor = null; display the gotten Data try { database = databaseOpenHelper.getReadableDatabase(); cursor = database.query("RSS_FEED", null, null, null, null, null, null); startManagingCursor(cursor); SimpleCursorAdapter rssFeedCursorAdapter = new SimpleCursorAdapter(this, R.layout.list_row, cursor, new String[] {"TITLE"}, ・・・1 new int[] {R.id.feed_title}); setListAdapter(rssFeedCursorAdapter); ・・・2 ・・・・・・・・ This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 r 習 a Display Data in list form c t Practice 6 i c • Practice theme – Execute Search the RSS Feed data stored in Database, display in list RSS e Feed titles on list screen 5.4. Search Database in all cases and Display list Screen transition RSS Feed title This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 r 習 a Display Data in list form c t Practice 6 i c • Practice procedure 1. Create SimpleCursorAdapter object from Cursor object gotten in e Practice 5 2. After creating SimpleCursorAdapter object, execute RssListActivity#setListAdapter method, register SimpleCursorAdapter object in RssListActivity 5.4. Search Database in all cases and Display list This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 r 習 a Display Data in list form c t Practice 6 i – Setting information c • Implement the following class, method e 5.4. Search Database in all cases and Display list Screen Class Method List screen RssListActivity onResume Overview • Search RSS Feed from Database then display in list form on list screen This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 r 習 a Display Data in list form c t Practice 6 i • Verification method c – After transited screen from menu screen to list screen, verify the display e in list of RSS Feed titles on list screen 5.4. Search Database in all cases and Display list In menu screen, click on List display button Screen transition RSS Feed title This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 r 習 a Display Data in list form c t Practice 6 i c • Practice Answers – RssListActivity#onResume e 5.4. Search Database in all cases and Display list public class RssListActivity extends ListActivity { ・・・・・・・・ @Override protected void onResume() { super.onResume(); DatabaseOpenHelper databaseOpenHelper = new DatabaseOpenHelper(this); SQLiteDatabase database = null; Cursor cursor = null; try { database = databaseOpenHelper.getReadableDatabase(); cursor = database.query("RSS_FEED", null, null, null, null, null, null); startManagingCursor(cursor); SimpleCursorAdapter rssFeedCursorAdapter = new SimpleCursorAdapter(this, R.layout.list_row, cursor, new String[] {"TITLE"}, new int[] {R.id.feed_title}); setListAdapter(rssFeedCursorAdapter); } finally { if (database != null) { database.close(); } } } } This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.5. Search Database for 1 item and Display detail • Learn the method of narrowing search by specifying criteria from database. • Learn the following by practice: • The method of recognizing that 1 item has been selected from the data displayed in list. • The method of receiving and sending parameters between Activities. • The method of narrowing search from database. • The method of retrieving a value from searching data. • The method of setting screen View properties from the program. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.5. Search Database for 1 item and Display detail Receive and send parameters between Screens • How to recognize that 1 item has been selected from the data displayed in list? – Mechanism • When you click and select 1 item from the data displayed in ListView, the specified method of Activity will be called out. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.5. Search Database for 1 item and Display detail Receive and send parameters between Screens • The method of recognizing that 1 item has been selected from the data displayed in list. – Implement ListActivity#onListItemClick method on Activity displayed in List • Sample code public class RssListActivity extends ListActivity { ・・・・・・・・ @Override protected void onListItemClick(ListView listView, View view, int position, long id) { super.onListItemClick(listView, view, position, id); // Describe solution after selected 1 item } } This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.5. Search Database for 1 item and Display detail Receive and send parameters between Screens • How to send and receive parameters between Activities? – Mechanism • By setting the parameter that you want to receive & send to “Intent” object generated during screen transition to send & receive parameter between Activities. • Parameter will be set by combination of key & value. • The transited screen will use Method of Activity to acquire sent “Intent” object, the origin screen will use the key used when setting parameter to get the value from “Intent” object. Activity A Fill “Intent” with parameter (Key & value) Intent . “Key”, “Value” Activity B Intent Intent Android • Acquire parameter from “Intent” • Specify the Key of parameter then output the value This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.5. Search Database for 1 item and Display detail Receive and send parameters between Screens • The method of receiving and sending parameter using “Intent” – The source Activity 1. 2. Generate “Intent” object in the source Activity. Use method Intent#putExtra in “Intent” object to add parameter. – Set parameter by a set of “Key & value”. 3. • Call out the destination Activity using method Activity#startActivity. Sample code (the source Activity) public class SampleListActivity extends ListActivity { ・・・・・・・・ @Override protected void onListItemClick(ListView listView, View view, int position, long id) { Intent intent = new Intent(this, NextActivity.class); intent.putExtra(“id”, id); startActivity(intent); } } ・・・1 ・・・2 ・・・3 This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.5. Search Database for 1 item and Display detail Receive and send parameters between Screens – The started up Activity 1. The started up Activity will use method Activity#getIntent to acquire “Intent” object. 2. Acquire parameter that is being put away in “Intent” object by using method Intent#getExtras 3. Use method “getter” corresponded to data format, specify the Key from parameter, then acquire the value. • Sample code (Example of acquiring parameter by “onCreate”) public class NextActivity extends Activity { ・・・・・・・・ @Override public void onCreate(Bundle savedInstanceState) { Intent intent = getIntent(); Bundle extras = intent.getExtras(); long id = extras.getLong(“id”); } } ・・・1 ・・・2 ・・・3 This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.5. Search Database for 1 item and Display detail r 習 a Receive and send parameters between Screens c t Practice 7 i c • Practice theme – After 1 RSS Feed is selected from the list screen, the screen will be e transited to detail screen. At that time, “id” of the RSS Feed selected from the list screen will be received and sent to detail screen, and the “id” received when starting up detail screen will be output to Log. Click 1 item from the List screen Screen transitio n Receive and send “id” to parameter This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.5. Search Database for 1 item and Display detail r 習 a Receive and send parameters between Screens c t Practice 7 i c • Practice procedure 1. Create Screen design resource file (res/layout/detail.xml) of detail e screen. 2. Open Screen design resource file of detail screen by Layout Editor, then arrange TextView by the following order. Title Date Sender’s name Content This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.5. Search Database for 1 item and Display detail r 習 a Receive and send parameters between Screens c t Practice 7 i 3. Set properties of TextView arranged in detail screen. c 4. Implement method RssListActivity#onListItemClick. e 5. Generate “Intent” object transited to DetailActivity by method RssListActivity#onListItemClick. 6. Set argument “id” of method RssListActivity#onListItemClick in generated “Intent” object. 7. Transit to DetailActivity by using method Activity#startActivity. 8. Acquire “Intent” object by method DetailActivity#onResume. 9. Get “id” value from acquired “Intent” object, then output to Log. • Log output format – – tag message : DetailActivity : id = <Acquired id> This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.5. Search Database for 1 item and Display r 習 a Receive and send parameters between Screens c t Practice 7 i – Setting information 1 c • Create the following resource file. e Screen Detail screen Resource file detail Content res/layout/detail.xml • Arrange TextView displaying title of RSS Feed, sending date, sender’s name, content. – Setting information 2 • Create the following resource file. Screen Class Method Description List screen RssListActivity onListItemClick • Transfer “id” of RSS Feed selected in List screen to detail screen, open detail screen. Detail screen DetailActivity onCreate • Display screen design of res/layout/detail.xml by method setContentView. onResume • Output “id” of RSS Feed transferred from List screen to Log. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.5. Search Database for 1 item and Display r 習 a Receive and send parameters between Screens c t Practice 7 i – Setting information 3 c • Set the following information to View properties. e # 1 2 3 4 View Properties 1 TextView 1. android:id=“@+id/title” 2. android:layout_width=“wrap_content” 3. android:layout_height=“wrap_content” 2 TextView 1. android:id=“@+id/publish_date” 2. android:layout_width=“wrap_content” 3. android:layout_height=“wrap_content” 3 TextView 1. android:id=“@+id/sender_name” 2. android:layout_width=“wrap_content” 3. android:layout_height=“wrap_content” 4 TextView 1. android:id=“@+id/description” 2. android:layout_width=“wrap_content” 3. android:layout_height=“wrap_content” This material is licensed under the Creative Commons License BY-NC-SA 4.0. detail
    • p 実 5.5. Search Database for 1 item and Display r 習 a Receive and send parameters between Screens c t Practice 7 i – Setting information 4 c • Implement the following class, method. e detail Screen Class Method Description List screen RssListActivity onListItemClick • Transfer “id” of RSS Feed selected in List screen to detail screen, open detail screen. Detail screen DetailActivity onResume • Output “id” of RSS Feed transferred from List screen to Log. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.5. Search Database for 1 item and Display detail r 習 a Receive and send parameters between Screens c t Practice 7 i c • Verification method – After selected 1 item from RSS Feed displayed on List screen, verify e screen transition to detail screen. – Verify output of selected RSS Feed “id” to Log by LogCat. Click 1 item from the List screen Screen transitio n Receive and send “id” to parameter Output “id” of RSS Feed transferred to Log This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.5. Search Database for 1 item and Display r 習 a Receive and send parameters between Screens c t Practice 7 i c • Practice answers – res/layout/detail.xml e <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:id="@+id/LinearLayout“ android:layout_width="fill_parent“ android:layout_height="fill_parent“ android:orientation="vertical“> <TextView android:id="@+id/title“ android:layout_width="wrap_content“ android:layout_height="wrap_content“></TextView> <TextView android:id="@+id/publish_date“ android:layout_width="wrap_content“ android:layout_height="wrap_content"></TextView> <TextView android:id="@+id/sender_name“ android:layout_width="wrap_content“ android:layout_height="wrap_content"></TextView> <TextView android:id="@+id/description“ android:layout_width="wrap_content“ android:layout_height="wrap_content"></TextView> </LinearLayout> This material is licensed under the Creative Commons License BY-NC-SA 4.0. detail
    • p 実 5.5. Search Database for 1 item and Display r 習 a Receive and send parameters between Screens c t Practice 7 i c • Practice answers – RssListActivity#onListItemClick e public class RssListActivity extends ListActivity { ・・・・・・・・ @Override protected void onListItemClick(ListView listView, View view, int position, long id) { super.onListItemClick(listView, view, position, id); Intent intent = new Intent(this, DetailActivity.class); intent.putExtra("id", id); startActivity(intent); } } – DetailActivity#onResume public class DetailActivity extends Activity { ・・・・・・・・ @Override protected void onResume() { super.onResume(); Intent intent = getIntent(); Bundle extras = intent.getExtras(); long id = extras.getLong("id"); Log.v("DetailActivity", "id = " + id); } } This material is licensed under the Creative Commons License BY-NC-SA 4.0. detail
    • 5.5. Search Database for 1 item and Display detail data from a narrow search • How to specify criteria and search data from database? – Mechanism • In database searching process, we can acquire only the data corresponded to given criteria. • Given criteria will be specified in format of keyword “Where” of SQL from program. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.5. Search Database for 1 item and Display detail data from a narrow search • The method of executing criteria specified search 1. Specify narrow criteria by argument of method SQLiteDatabase#query. • Describe narrow criteria in format of keyword “Where” of SQL. – Use “_id=<criteria value>” to specify the criteria of Key 「_id」. • Sample code SampleSQLiteOpenHelper databaseOpenHelper = new SampleSQLiteOpenHelper(this); SQLiteDatabase database = null; Cursor cursor = null; try { int id = 1; database = databaseOpenHelper.getReadableDatabase(); cursor = database.query(“SAMPLE_TABLE", null, "_id=" + id, null, null, null, null); ・・・1 } finally { if (database != null) { database.close(); } } This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.5. Search Database for 1 item and Display detail data from a narrow search • The method of retrieving value from searching data 1. Search data from database, acquire Cursor object that has data corresponded to searching criteria. Execute method Cursor#moveToFirst by acquired Cursor, then acquire data. 2. • If data can not be retrieved, the return value of every method will be “false”. • Sample code public class SampleActivity extends Activity { ・・・・・・・・ @Override public void onResume() { super.onResume(); SampleSQLiteOpenHelper databaseOpenHelper = new SampleSQLiteOpenHelper(this); SQLiteDatabase database = databaseOpenHelper.getReadableDatabase(); Cursor cursor = database.query(“SAMPLE_TABLE", null, “_id = ” + id , null, null, null, null); cursor.moveToFirst(); } } ・・・1 ・・・2 If acquired data is limited to 1 item, execute moveToFirst(). If acquire various data, execute moveToNext(). ※Refer “Android Developers’ Reference” for detail. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.5. Search Database for 1 item and Display detail data from a narrow search 3. After retrieving data from Cursor object, use method corresponded to type of column data to get value. • Use method Cursor#getColumnIndex with argument is column name to get index of the column • Use method getter corresponded to type of the value with acquired index of the column as argument to get the value. • Sample code Pattern of value Method getter String getString(int columnIndex) int getInt(int columnIndex) short getShort(int columnIndex) long getLong(int columnIndex) double getDouble(int columnIndex) float getFloat(int columnIndex) public class SampleActivity extends Activity { ・・・・・・・・ @Override protected void onResume() { super.onResume(); SampleSQLiteOpenHelper databaseOpenHelper = new SampleSQLiteOpenHelper(this); SQLiteDatabase database = databaseOpenHelper.getReadableDatabase(); Cursor cursor = database.query(“SAMPLE_TABLE", null, “_id = ” + id , null, null, null, null); cursor.moveToFirst(); // Retrieve the first item of data Table. Pattern of data string & method getter (extract) int columnIndex=cursor.getColumnIndex("TITLE"); String title=cursor.getString(columnIndex); } } This material is licensed under the Creative Commons License BY-NC-SA 4.0. ・・・3
    • 5.5. Search Database for 1 item and Display detail data from a narrow search • The method of setting View properties of screen from program. 1. 2. 3. Acquire object View by using method Activity#findViewByld. Because return value of Activity#findViewByld is returned by abstract class View, it will be casted to View class being in use. Execute method setter of properties of object View acquired, set the value. • Example of setting - Change text properties of TextView of screen from program. >> Procedure ・Use method Activity#findViewByld, acquire object TextView. ・Use method setText of object TextView, change text properties. >> Sample code View view = findViewById(R.id.title); TextView textView = (TextView) view; textView.setText("text"); ・・・1 ・・・2 ・・・3 This material is licensedEmbedded SoftwareCommons License BY-NC-SA 4.0. Copyright 2009, Open under the Creative Foundation, All rights reserved 278
    • p 実 5.5. Search Database for 1 item and Display detail r 習 a data from a narrow search c t Practice 8 i c • Practice theme – Search for the information of RSS Feed from database with criteria is e “id” of RSS Feed received from detail screen, then display detail screen. • Practice procedure 1. Generate object DatabaseOpenHelper by method onResume of DetailActivity. 2. Acquire object SQLiteDatabase (Readonly) by method DatabaseOpenHelper#getReadableDatabase. 3. Execute method SQLiteDetabase#query with argument is narrow criteria of table name & “id”, acquire object Cursor. • • Table name : RSS_FEED Narrow criteria : _id = <“id” received from detail screen※> ※ Value of RSS Feed received from detail screen was acquired in Practice 7. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.5. Search Database for 1 item and Display detail r 習 a data from a narrow search c t Practice 8 i 4. Execute method Cursor#moveToFirst, read 1 data item from object c Cursor. e 5. Execute method Cursor#getColumnIndex, Cursor#getString, specify the Key from 1 data item , then acquire the value. # TITLE title 2 PUBLISH_DATE publish_date 3 SENDER_NAME sender_name 4 7. View “id” of detail screen to display the value 1 6. Key retrieve value from object Cursor DESCRIPTION description Execute method TextView#setText, set acquired value to View of detail screen. Execute method SQLiteDetabase#close, close object database. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.5. Search Database for 1 item r 習 a data from a narrow search c t Practice 8 i – Setting information c • Implement the following class & method e Screen Class Method Detail screen DetailActivity onResume and Display detail Description • Read the content of RSS Feed from database, then display on detail screen. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.5. Search Database for 1 item and Display detail r 習 a data from a narrow search c t Practice 8 i c • Verification method – After RSS Feed is selected from the list screen, verify screen transition e to detail screen from the list screen & display of the following items on detail screen. • Verify items – – – – Title Date Sender’s name Content This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.5. Search Database for r 習 a data from a narrow search c t Practice 8 i c • Practice answers – DetailActivity#onResume e 1 item and Display detail protected void onResume() { super.onResume(); DatabaseOpenHelper databaseOpenHelper = new DatabaseOpenHelper(this); SQLiteDatabase database = null; Cursor cursor = null; try { Bundle extras = getIntent().getExtras(); long id = extras.getLong("id"); database = databaseOpenHelper.getReadableDatabase(); cursor = database.query("RSS_FEED", null, "_id=" + id, null, null, null, null); startManagingCursor(cursor); if (!cursor.moveToFirst()) { return; } ((TextView) findViewById(R.id.title)).setText(cursor.getString(cursor.getColumnIndex("TITLE"))); ((TextView) findViewById(R.id.publish_date)).setText(cursor.getString(cursor.getColumnIndex("PUBLISH_DATE"))); ((TextView) findViewById(R.id.sender_name)).setText(cursor.getString(cursor.getColumnIndex("SENDER_NAME"))); ((TextView) findViewById(R.id.description)).setText(cursor.getString(cursor.getColumnIndex("DESCRIPTION"))); } finally { if (database != null) { database.close(); } } } } This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 r 習 a c t • Learn how to create Option menu i c • Learn the following by practicing. – The method to add Option menu to Application e – The method to recognize “Option menu clicked” – The method to display dialogue on screen 5.6. The method of creating Option menu Option menu Menu displayed by pressing “MENU” button This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.6. The method of creating Option menu r 習 a Add Option menu c t • How to add Option menu i – Mechanism c • Option menu is created by resource file used for menu. e • There is method to create Option menu in Activity. Therefore, menu can be displayed on the screen by reading resource file used for menu within that method. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.6. The method of creating Option menu r 習 a Add Option menu c t • The method of displaying Option menu after clicked “Menu” i button on emulator . c – Create resource file used for Menu in “res/menu/” directory e • Sample code <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:title="@string/option_menu_add_label" android:id="@+id/main_menu_add"></item> </menu> – Read resource file used for Menu by method onCreateOptionMenu of Activity, then display Option menu. • Sample code @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuInflater menuInfalter = getMenuInflater(); menuInfalter.inflate(R.menu.main_menu, menu); return true; } This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.6. The method of creating Option menu r 習 a Add Option menu c t Practice 9 i c • Practice theme – After clicked button “menu” of emulator on screen Menu, display e Option menu. Option menu created by practice will be the simple one that display only character. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.6. The method of creating Option menu r 習 a Add Option menu c t Practice 9 i c • Practice procedure 1. Create resource file (res/menu/main_menu.xml) used for Menu. e 2. Add “Item” to resource file used for Menu, set Id, Title. 3. Override method onCreateOptionMenu in RssReaderActivity, read resource file used for Menu, then display Option menu. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.6. The method of creating r 習 a Add Option menu c t Practice 9 i – Setting information c • Create the following resource file. e Screen Resource file - res/menu/main_menu.xml Option menu Properties • android:id="@+id/main_menu_add" • android:title="@string/option_menu_add_label“ • Implement the following class & method. Screen Class Method Menu screen RssReaderActivity onCreateOptionMenu This material is licensed under the Creative Commons License BY-NC-SA 4.0. Description • Display Option menu
    • p 実 5.6. The method of creating Option menu r 習 a Add Option menu c t Practice 9 i c • Verification method – After displayed menu screen, click “menu” button of emulator to verify e the display of Option menu. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.6. The method of creating r 習 a Add Option menu c t Practice 9 i c • Practice answers – res/menu/main_menu.xml e Option menu <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:title="@string/option_menu_add_label" android:id="@+id/main_menu_add"></item> </menu> – RssReaderActivity public class RssReaderActivity extends Activity implements OnClickListener { ・・・・・・・・ @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuInflater menuInfalter = getMenuInflater(); menuInfalter.inflate(R.menu.main_menu, menu); return true; } } This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.6. The method of creating Option menu Recognize “Option menu clicked” • How to recognize “Option menu clicked” – Mechanism • After clicked Option menu, “event” will be raised in Activity, and specified method of Activity will be invoked. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 5.6. The method of creating Option menu Recognize “Option menu clicked” • The method of displaying Dialogue after clicked Option menu 1. 2. Display dialogue by method onOptionsItemSelected of Activity. To display dialogue, create object of AlertDialog.Builder, then call out method AlertDialog.Builder#show. ※ Use method AlertDialog.Builder#setTitle to set title to dialogue. • Sample code @Override public boolean onOptionsItemSelected(MenuItem item) { new AlertDialog.Builder(this) .setTitle(R.string.ok_dialog_label) .show(); return true; } ・・・1 ・・・2 This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.6. The method of creating Option menu r 習 a Recognize “Option menu clicked” c t Practice 10 i c • Practice theme – After clicked Option menu, execute acquiring process of RSS Feed from e internet, then display finish message by dialogue. Click Option menu This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.6. The method of creating Option menu r 習 a Recognize “Option menu clicked” c t Practice 10 i ※ About process of acquiring RSS Feed from internet and registering to c database. e • Practice provides skeleton project as Java class, including the full process to acquire RSS Feed from internet, then insert RSS Feed into database. • From the screen, the full process can be executed by execution of method RssFeedRegister#registration made URL argument. Class provided by skeleton project Description RssFeedRegister Class execute the full process to make URL argument, acquire RSS Feed from internet then register to database. HttpHelper Class make URL argument, then acquire RSS Feed from internet. XmlHelper Class make RSS Feed argument, pass data structure in XML format, then convert data of RSS Feed to Java object. HttpHelperException Exceptional class to generate after acquired RSS Feed from internet. RssFeedEntity Class convert data of RSS Feed from format of XML, then be filled with data. Table. Description of class provided by skeleton project This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.6. The method of creating Option menu r 習 a Recognize “Option menu clicked” c t Practice 10 i c • Practice procedure – Override method RssReaderActivity#onOptionsItemSelected, execute e method RssFeedRegister#registration, acquire RSS Feed from internet, then register to database. Use AlertDialog.Builder to notify of process completion, then display dialogue. – Setting information • Implement the following class & method Screen Class Method Menu screen RssReaderActivity onOptionsItemSelected This material is licensed under the Creative Commons License BY-NC-SA 4.0. Description • Display dialogue
    • p 実 5.6. The method of creating Option menu r 習 a Recognize “Option menu clicked” c t Practice 10 i c • Verification method – After displayed & clicked Option menu on Menu screen, verify display of e dialogue. Click Option menu This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • p 実 5.6. The method of creating Option r 習 a Recognize “Option menu clicked” c t Practice 10 i c • Practice answers – RssReaderActivity#onOptionsItemSelected e menu public class RssReaderActivity extends Activity implements OnClickListener { ・・・・・・・・ @Override public boolean onOptionsItemSelected(MenuItem item) { RssFeedRegister rssFeedRegister = new RssFeedRegister(this); rssFeedRegister.registration("http://www.oesf.jp/modules/news/index.php?page=rss"); new AlertDialog.Builder(this) .setTitle(R.string.ok_dialog_label) .show(); return true; } } This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 6. Training summary This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 6.1. Training evaluation • The first day Outline Chapter Learn the basic of Android platform Chapter 1 Learn architecture of Android application •Life cycle •Main components •Security Chapter 2 Learn construction method of development environment Chapter 3 Learn procedure from create project to application implementation Chapter 4 Learn function of development tool, using method •Eclipse + ADT, emulator (AVD), DDMS Chapter 4 This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 6.1. Training evaluation • The second day Outline Chapter Create User interface •Create screen design (Single screen, list screen) •Implement screen event •Implement screen transition •Implement dialogue Chapter 5 Use Internet •Acquire RSS Feed from internet Chapter 5 XML analysis Chapter 5 Use Database •Database management using SQLiteOpenHelper •Insert record to table, delete record •Use transaction Chapter 5 Make unit test •Use Android test framework to make unit test Chapter 5 This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 6.2. Information source related to Android • Internet – Technical materials/ manual • Android Developers – http://developer.android.com/intl/ja/index.html – Community • Japan Android Association – http://android.siprop.org/ • Android-SDK-Japan – http://groups.google.co.jp/group/android-sdk-japan This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 6.2. Information source related to Android • Internet – wiki • Summary of information related to Google Android – http://www29.atwiki.jp/android/ • Android Wiki – http://wikiwiki.jp/android/ – Samples • Samples are opened to the public by android developers – http://developer.android.com/intl/ja/guide/samples/index.html • apps-for-android – http://code.google.com/p/apps-for-android/ This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 6.2. Information source related to Android • Books Title Author Publisher Issued date An introduction to Google Android Yoshikazu Shima Gijutsu-Hyohron Co., Ltd. 2008/5/25 Android for beginner Ed Burnette O'Reilly Japan, Inc. 2009/5/15 A guide to develop Google Android application Hideo Kinami Nikkei Business Publications, Inc. 2009/6/8 A guide to programming Google Android Mamezou Co., Ltd. Takashi Egawa, … ASCII Media Works Inc. 2009/7/2 Android 2.1 programming bible Hidekazu Furukawa Socym Co., Ltd. 2010/5/18 This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 6.3. Tips • Introduce development Tips in every system development phase – Design phase • Define column _id by SQLite – Implement phase • Use Java library created by the third party – Test phase • Unit test of Android application This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 6.3. Tips Define column _id by SQLite • Define column that will be Primary Key of SQLite with the name “_id” – Reason • Because of the spec that use the value of string “_id” the unique ID when using database through Content Provider. – http://developer.android.com/intl/ja/guide/topics/providers/c ontent-providers.html#basics • Because there is the implementation acquires information from the string “_id” by Class such as CursorAdapter、ArrayListCursor, … – “_id” is hard-coded in source. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 6.3. Tips Define column _id by SQLite ■CursorAdapter.java protected void init(Context context, Cursor c, boolean autoRequery) { boolean cursorPresent = c != null; mAutoRequery = autoRequery; mCursor = c; ・・・・・・・ mRowIDColumn = cursorPresent ? c.getColumnIndexOrThrow("_id") : -1; mChangeObserver = new ChangeObserver(); ■ArrayListCursor.java public ArrayListCursor(String[] columnNames, ArrayList<ArrayList> rows) { ・・・・・・・ for (int i = 0; i < colCount; ++i) { if (columnNames[i].compareToIgnoreCase("_id") == 0) { mColumnNames = columnNames; ・・・・・・・ } This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 6.3. Tips Use Java library created by the third party • Because Android sticks to Java SE, you can use Java library created by the third party. – Library created by the third party loading Standard Android • JUnit • Apache Commons • bluez.org • JSON.org This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 6.3. Tips Use Java library created by the third party • In some cases, we can not use just as it is. 1. Use Java standard class that Android does not support 2. Use too much memory 3. Use file .properties • We can not use resource file, external file because of strict restriction. Especially, related to problem 2. & 3., build errors do not occur, but exception will occur when executing. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 6.3. Tips Use Java library created by the third party • Solution – Edit the library (In case we can acquire the source) • Example – Deal with the part using Class that Android does not support by supporting class or implement by yourself. – In case .properties are being used, hard code the content. • The risk of solution – It is necessary to perform maintenance after changed by yourself. – Obligation to open source to the public maybe occur, depends on the kind of license of library changed. This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 6.3. Tips Unit test of Android application • Unit test of Android application will be created by using Android test framework. – What is Android test framework? • Framework on the base of JUnit3 • Implementation procedure of Unit test is the same with JUnit 1. Create TestCase 2. Create TestSuite (Register TestCase) 3. Execute TestCase or TestSuite by TestRunner ※Execute by Eclipse is ok This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 6.3. Tips Unit test of Android application • Create Unit test inherited TestCase class in every target class. Target class TestCase class of Android POJO AndroidTestCase Activity ActivityInstrumentationTestCase2 Comments Test event with screen operation such as Key input event Ex. Push button ActivityUnitTestCase Service ServiceTestCase Content Provider Test event Activity ProviderTestCase2 ■Refer the following link for more details http://developer.android.com/intl/ja/reference/android/test/package-summary.html This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 6.3. Tips Unit test of Android application • Example of POJO test class creation package jp.oesf.mtgeduwg.training.test; import jp.oesf.mtgeduwg.training.rssreader.SamplePojo; import android.test.AndroidTestCase; public class SamplePojoTest extends AndroidTestCase { public void testGetMessage() throws Exception { SamplePojo samplePojo = new SamplePojo(); assertEquals(samplePojo.getMessage(), "Message from SamplePojo"); } } This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 6.3. Tips Unit test of Android application • Example of Activity test class creation (Key input) package jp.oesf.mtgeduwg.training.test; public class AddActivityTest extends ActivityInstrumentationTestCase2<AddActivity> { public AddActivityTest() { super("jp.oesf.mtgeduwg.training.rssreader", AddActivity.class); } public void testUrlFieldKeyInput() { // Key transmission (Input "H","T","T","P") sendKeys(KeyEvent.KEYCODE_H, KeyEvent.KEYCODE_T, KeyEvent.KEYCODE_T, KeyEvent.KEYCODE_P); assertEquals(urlField.getText().toString(), "http"); } } This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 6.3. Tips Unit test of Android application • Example of Activity test class creation (Push button) package jp.oesf.mtgeduwg.training.test; public class AddActivityTest extends ActivityInstrumentationTestCase2<AddActivity> { public AddActivityTest() { super("jp.oesf.mtgeduwg.training.rssreader", AddActivity.class); } public void testAddButtonClick() { getActivity().runOnUiThread(new Runnable() { public void run() { urlField.setText("http://www.oesf.jp/modules/news/index.php?page=rss"); addButton.performClick(); }}); Click the button getInstrumentation().waitForIdleSync(); assertTrue(existsUrlAddressById(10)); This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 6.3. Tips Unit test of Android application • Example of Activity test class creation (Generate onResume) package jp.oesf.mtgeduwg.training.test; public class AddActivityUnitTest extends ActivityUnitTestCase<AddActivity> { public AddActivityUnitTest() { super(AddActivity.class); } @Override protected void setUp() throws Exception { super.setUp(); } public void testSendResumeEvent() throws Exception { Generate event onResume in Activity Intent intent = new Intent(Intent.ACTION_MAIN); startActivity(intent, null, null); getInstrumentation().callActivityOnResume(getActivity()); } This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 6.3. Tips Unit test of Android application • Generate TestSuite – In Android, TestSuiteBuilder will be prepared as a class generated by TestSuite. package jp.oesf.mtgeduwg.training.test; public class AllTests extends TestCase { public static Test suite() throws Exception { TestSuiteBuilder testSuiteBuilder = new TestSuiteBuilder(AllTests.class); return testSuiteBuilder.includePackages ("jp.oesf.mtgeduwg.training.rssreader") .build(); } Execute all Test case below specified } packages ※It is possible to use create method of TestSuite of JUnit This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • 6.3. Tips Unit test of Android application • Setting in AndroidManifest.xml is required to execute Test. 1. Add setting of library required for execution of Test 2. Add setting of TestRunner <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=http://schemas.android.com/apk/res/android> <application android:icon="@drawable/icon“> <uses-library android:name=“android.test.runner” /> ・・・ ① </application> <instrumentation android:name="android.test.InstrumentationTestRunner" android:label="Tests for RssReader" android:targetPackage=“jp.oesf.mtgeduwg.training.rssreader”/> ・・・ ② </manifest> This material is licensed under the Creative Commons License BY-NC-SA 4.0.
    • Credit This document is created by Ulsystems Co., Ltd. http://www.ulsystems.co.jp This document is edited by Leading Edge Co., Ltd. to respond to Android 2.1 and online environment. http://www.leadinge.co.jp/ This document is translated from Japanese to English by ISB Vietnam Co., Ltd. http://www.isb-vietnam.com.vn/ This material is licensedEmbedded Creative Commons License BY-NC-SA 4.0. Copyright 2009, Open under the Software Foundation, All rights reserved 319