Android Chapter2
-
Upload
dontmatter9x2553 -
Category
Documents
-
view
219 -
download
0
Transcript of Android Chapter2
![Page 1: Android Chapter2](https://reader031.fdocuments.in/reader031/viewer/2022021122/577cd8d81a28ab9e78a22092/html5/thumbnails/1.jpg)
7/29/2019 Android Chapter2
http://slidepdf.com/reader/full/android-chapter2 1/23
Chương 2. Cấ u trúc mộtchương trình Android
Hanoi University of Science and
Technology
Nguyen Hong Quang
19/8/2012
![Page 2: Android Chapter2](https://reader031.fdocuments.in/reader031/viewer/2022021122/577cd8d81a28ab9e78a22092/html5/thumbnails/2.jpg)
7/29/2019 Android Chapter2
http://slidepdf.com/reader/full/android-chapter2 2/23
Content 2.1. Chương trình Hello World
Phân tích Hello World
Giới thiệu ADT và chứ c năng xem Log
2.1. Cấ u trúc một chương trình
Phân tích AndroidManifest.xml
R.java và định ngh ĩ a Resource
Các Permissions
![Page 3: Android Chapter2](https://reader031.fdocuments.in/reader031/viewer/2022021122/577cd8d81a28ab9e78a22092/html5/thumbnails/3.jpg)
7/29/2019 Android Chapter2
http://slidepdf.com/reader/full/android-chapter2 3/23
Creating “Hello World”
Program Project name: HelloAndroid
Build Target: Android 4.1
Application name: Hello, Android
Package name: org.example.hello
Create Activity: Hello
Min SDK Version: 8 (Android 2.2 –Froyo)
![Page 4: Android Chapter2](https://reader031.fdocuments.in/reader031/viewer/2022021122/577cd8d81a28ab9e78a22092/html5/thumbnails/4.jpg)
7/29/2019 Android Chapter2
http://slidepdf.com/reader/full/android-chapter2 4/23
Running on AVD (Android
Virtual Devices) Emulator
![Page 5: Android Chapter2](https://reader031.fdocuments.in/reader031/viewer/2022021122/577cd8d81a28ab9e78a22092/html5/thumbnails/5.jpg)
7/29/2019 Android Chapter2
http://slidepdf.com/reader/full/android-chapter2 5/23
Giới thiệu ADT Configure an Android Virtual Device
(AVD) for the emulator
Create a debug configuration for yourproject
Build the Android project and launch
the debug configuration
![Page 6: Android Chapter2](https://reader031.fdocuments.in/reader031/viewer/2022021122/577cd8d81a28ab9e78a22092/html5/thumbnails/6.jpg)
7/29/2019 Android Chapter2
http://slidepdf.com/reader/full/android-chapter2 6/23
Debugging Android
Applications Using DDMS
![Page 7: Android Chapter2](https://reader031.fdocuments.in/reader031/viewer/2022021122/577cd8d81a28ab9e78a22092/html5/thumbnails/7.jpg)
7/29/2019 Android Chapter2
http://slidepdf.com/reader/full/android-chapter2 7/23
Debugging Android
Applications Using DDMS Task management
select individual instances and inspect processes and threads
File management browse files and directories on the emulator or a device
Emulator interaction send a number of events, such as simulated calls, SMS
messages, and location coordinates, to specific emulatorinstances
Screen captures take screenshots of the current screen
Logging Like “System.out.println”, but more comfortable
![Page 8: Android Chapter2](https://reader031.fdocuments.in/reader031/viewer/2022021122/577cd8d81a28ab9e78a22092/html5/thumbnails/8.jpg)
7/29/2019 Android Chapter2
http://slidepdf.com/reader/full/android-chapter2 8/23
Cấ u trúc chương trình
HelloWorld AndroidManifest.xml: where global
settings are made.
Directory: res : resources are held drawable: contains actual image files
that application can use and reference.
layout : holds an XML file, main.xml, thatis referenced by your application when
building its interface assets: contains audio files for streaming
and animation assets
src : contains all the source files
![Page 9: Android Chapter2](https://reader031.fdocuments.in/reader031/viewer/2022021122/577cd8d81a28ab9e78a22092/html5/thumbnails/9.jpg)
7/29/2019 Android Chapter2
http://slidepdf.com/reader/full/android-chapter2 9/23
Phân tích AndroidManifest.xml
Global settings
![Page 10: Android Chapter2](https://reader031.fdocuments.in/reader031/viewer/2022021122/577cd8d81a28ab9e78a22092/html5/thumbnails/10.jpg)
7/29/2019 Android Chapter2
http://slidepdf.com/reader/full/android-chapter2 10/23
Application manifest file The manifest defines the structure and
metadata of Android application, itscomponents, and its requirements. uses-sdk: minimum and maximum SDK version
uses-configuration: specify each combination of input mechanisms are supported by application.
uses-feature: specify which hardware featuresyour application requires (Audio, Bluetooth,Camera, Location, Microphone, NFC, Sensors,Telephony, Touchscreen, USB, Wi-Fi).
![Page 11: Android Chapter2](https://reader031.fdocuments.in/reader031/viewer/2022021122/577cd8d81a28ab9e78a22092/html5/thumbnails/11.jpg)
7/29/2019 Android Chapter2
http://slidepdf.com/reader/full/android-chapter2 11/23
Application manifest file supports-screens:
specify the screen sizes of application
uses-permission: declare the user permissions your application requires.
application: specify the metadata for your application (including its
title, icon, and theme)
activity: An activity tag is required for every Activity within Android application.
Must include the main launch Activity and any other Activity that may be displayed.
![Page 12: Android Chapter2](https://reader031.fdocuments.in/reader031/viewer/2022021122/577cd8d81a28ab9e78a22092/html5/thumbnails/12.jpg)
7/29/2019 Android Chapter2
http://slidepdf.com/reader/full/android-chapter2 12/23
Các PermissionsManifest.permission
<uses-permissionandroid:name=”android.permission.ACCESS_FINE
_LOCATION” />
<uses-permissionandroid:name=”android.permission.BLUETOOTH” />
<uses-permissionandroid:name=”android.permission.READ_CONTACTS” />
![Page 13: Android Chapter2](https://reader031.fdocuments.in/reader031/viewer/2022021122/577cd8d81a28ab9e78a22092/html5/thumbnails/13.jpg)
7/29/2019 Android Chapter2
http://slidepdf.com/reader/full/android-chapter2 13/23
EXTERNALIZING RESOURCES
![Page 14: Android Chapter2](https://reader031.fdocuments.in/reader031/viewer/2022021122/577cd8d81a28ab9e78a22092/html5/thumbnails/14.jpg)
7/29/2019 Android Chapter2
http://slidepdf.com/reader/full/android-chapter2 14/23
Externalization of resources Ideas: keep non-code resources, such as images and string
constants, external to code.
ranging from simple values such as strings and colors tomore complex resources such as images (Drawables),animations, themes, and menus, layouts
Advantages: easier to maintain, update, and manage.
easily define alternative resource values forinternationalization and to include different resources tosupport variations in hardware — particularly, screen
size and resolution
![Page 15: Android Chapter2](https://reader031.fdocuments.in/reader031/viewer/2022021122/577cd8d81a28ab9e78a22092/html5/thumbnails/15.jpg)
7/29/2019 Android Chapter2
http://slidepdf.com/reader/full/android-chapter2 15/23
strings.xml
![Page 16: Android Chapter2](https://reader031.fdocuments.in/reader031/viewer/2022021122/577cd8d81a28ab9e78a22092/html5/thumbnails/16.jpg)
7/29/2019 Android Chapter2
http://slidepdf.com/reader/full/android-chapter2 16/23
Phân tích main.xml
![Page 17: Android Chapter2](https://reader031.fdocuments.in/reader031/viewer/2022021122/577cd8d81a28ab9e78a22092/html5/thumbnails/17.jpg)
7/29/2019 Android Chapter2
http://slidepdf.com/reader/full/android-chapter2 17/23
HelloWorld.java
![Page 18: Android Chapter2](https://reader031.fdocuments.in/reader031/viewer/2022021122/577cd8d81a28ab9e78a22092/html5/thumbnails/18.jpg)
7/29/2019 Android Chapter2
http://slidepdf.com/reader/full/android-chapter2 18/23
Resources
Application resources arestored under the res
folder in your projecthierarchy.
Each of the availableresource types is stored
in subfolders, groupedby resource type.
Examples: colors, styles,
menus, raw,…
![Page 19: Android Chapter2](https://reader031.fdocuments.in/reader031/viewer/2022021122/577cd8d81a28ab9e78a22092/html5/thumbnails/19.jpg)
7/29/2019 Android Chapter2
http://slidepdf.com/reader/full/android-chapter2 19/23
R.java and Resource definition
R class file that contains references toeach of the resources which include in
project. This enables to reference the resources
in code, with the advantage of design-
time syntax checking.
![Page 20: Android Chapter2](https://reader031.fdocuments.in/reader031/viewer/2022021122/577cd8d81a28ab9e78a22092/html5/thumbnails/20.jpg)
7/29/2019 Android Chapter2
http://slidepdf.com/reader/full/android-chapter2 20/23
Using Resources in Code
Using the static R class
The R class contains static subclasses for each of theresource types for which defined at least oneresource. For example: the default new project includes the R.string
and R.drawable subclasses.
Each of the subclasses within R exposes its
associated resources as variables, with the variablenames matching the resource identifiers For example: R.string.app_name or R.drawable.icon.
![Page 21: Android Chapter2](https://reader031.fdocuments.in/reader031/viewer/2022021122/577cd8d81a28ab9e78a22092/html5/thumbnails/21.jpg)
7/29/2019 Android Chapter2
http://slidepdf.com/reader/full/android-chapter2 21/23
Resource types
Simple Values strings, colors, dimensions, styles, and string or
integer arrays strings.xml, colors.xml,…
Styles and Themes
Drawables
bitmaps and NinePatches (stretchable PNGimages)
![Page 22: Android Chapter2](https://reader031.fdocuments.in/reader031/viewer/2022021122/577cd8d81a28ab9e78a22092/html5/thumbnails/22.jpg)
7/29/2019 Android Chapter2
http://slidepdf.com/reader/full/android-chapter2 22/23
Resource types
Layouts enable you to decouple your presentation
layer from business logic by designing UIlayouts in XML rather than constructingthem in code
Animations
Menus Design menu layouts in XML
![Page 23: Android Chapter2](https://reader031.fdocuments.in/reader031/viewer/2022021122/577cd8d81a28ab9e78a22092/html5/thumbnails/23.jpg)
7/29/2019 Android Chapter2
http://slidepdf.com/reader/full/android-chapter2 23/23