Android Chapter2

Post on 14-Apr-2018

221 views 0 download

transcript

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

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

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)

7/29/2019 Android Chapter2

http://slidepdf.com/reader/full/android-chapter2 4/23

Running on AVD (Android

 Virtual Devices) Emulator

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

7/29/2019 Android Chapter2

http://slidepdf.com/reader/full/android-chapter2 6/23

Debugging Android

 Applications Using DDMS

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

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

7/29/2019 Android Chapter2

http://slidepdf.com/reader/full/android-chapter2 9/23

Phân tích AndroidManifest.xml

Global settings

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).

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.

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” />

7/29/2019 Android Chapter2

http://slidepdf.com/reader/full/android-chapter2 13/23

EXTERNALIZING RESOURCES 

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

7/29/2019 Android Chapter2

http://slidepdf.com/reader/full/android-chapter2 15/23

strings.xml

7/29/2019 Android Chapter2

http://slidepdf.com/reader/full/android-chapter2 16/23

Phân tích main.xml

7/29/2019 Android Chapter2

http://slidepdf.com/reader/full/android-chapter2 17/23

HelloWorld.java

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,…

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.

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.

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)

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

7/29/2019 Android Chapter2

http://slidepdf.com/reader/full/android-chapter2 23/23