Vous êtes sur la page 1sur 30

Overview

Lets Start with Helloworld? The Directory Structure of an Android Project. Running on Eclipse. Understanding the Building Blocks of Android

Creating An Android Project

Creating An Android Project


Need the items circled Then click Finish

Directory View of an Android Project

Project Components
src your source code gen auto-generated code (usually just R.java) Included libraries Resources
Drawables (like .png images) Layouts Values (like strings)

Manifest file

XML
Used to define some of the resources
Layouts (UI) Strings

Manifest file Shouldnt usually have to edit it directly, Eclipse can do that for you Preferred way of creating UIs
Separates the description of the layout from any actual code that controls it Can easily take a UI from one platform to another

R Class
Auto-generated: you shouldnt edit it Contains IDs of the project resources Enforces good software engineering Use findViewById and Resources object to get access to the resources
Ex. Button b = (Button)findViewById(R.id.button1) Ex. getResources().getString(R.string.hello));

Layouts (1)
Eclipse has a great UI creator
Generates the XML for you

Composed of View objects Can be specified for portrait and landscape mode
Use same file name, so can make completely different UIs for the orientations without modifying any code

Layouts (2)

Layouts (3)
Click Create to make layout modifications When in portrait mode can select Portrait to make a res sub folder for portrait layouts
Likewise for Landscape layouts while in landscape mode Will create folders titled layout-port and layout-land

Note: these port and land folders are examples of alternate layouts, see here for more info
http://developer.android.com/guide/topics/resources/providing-resources.html

Avoid errors by making sure components have the same id in both orientations, and that youve tested each orientation thoroughly

Layouts (4)

Strings
In res/values
strings.xml

Application wide available strings Promotes good software engineering UI components made in the UI editor should have text defined in strings.xml Strings are just one kind of Value there are many others

Manifest File (1)


Contains characteristics about your application When have more than one Activity in app, NEED to specify it in manifest file
Go to graphical view of the manifest file Add an Activity in the bottom right Browse for the name of the activity

Need to specify Services and other components too Also important to define permissions and external libraries, like Google Maps API

Manifest File (2) Adding an Activity

Running in Eclipse (1)


Similar to launching a regular Java app, use the launch configurations Specify an Android Application and create a new one Specify activity to be run Can select a manual option, so each time program is run, you are asked whether you want to use the actual phone or the emulator
Otherwise, it should be smart and use whichever one is available

Running in Eclipse (2)

Running in Eclipse (3)

Running in Eclipse (4)

USB Debugging
Should be enabled on phone to use developer features In the main apps screen select Settings -> Applications -> Development -> USB debugging.

Understanding the APK File


An APK file is a file format used to distribute and install software (usually games or applications) on the Android operating system. This file format is closely related to the standard Java .jar format. It contains the Manifest file, Resources and classes , which are required to run your application on the device.

Android Application Components


Android is designed with the maximum amount of modularity in mind. There are four main types of components that can be (but do not need to be) used within an Android application:
Activities handle the UI to the Smartphone screen. Services handle background processing. Broadcast receivers handle communication in your apps. Content providers handle data and database management issues.

Android Activities: Defining the UI


In Android, an activity is represent a single screen. An Activity is an application component that provides a screen with which users can interact in order to do something. The controls placed in the window allows the user to perform certain actions and are called Views or Widgets. Each Activity in android will be subclass of Activity class defined in Android SDK.

An Activity in Android can exist in four states. 1. Active/Running state 2. Paused state 3. Stopped state 4. Destroyed/Dead state

Active/Running state state when an activity is in the front and has focus in it. Paused state activity is partially visible to the user but not active and lost focus. A paused activity is completely alive and maintains its state Stopped state This is when the Activity is no longer visible in the screen. Destroyed/Dead state An Activity is said to be dead or destroyed when it no longer exists in the memory

Android Services: Processing in the Background


Faceless components that run in the background
E.g. music player, network download etc

Run in the background


Can continue even if Activity that started it dies Should be used if something needs to be done while the user is not interacting with application. Otherwise, a thread is probably more applicable. Should create a new thread in the service to do work in, since the service runs in the main thread

Can be bound to an application


In which case will terminate when all applications bound to it unbind Allows multiple applications to communicate with it via a common interface.

Needs to be declared in manifest file Like Activities, has a structured life cycle

Broadcast Receivers
Broadcast receivers are communication components that receive messages that are sent between the Android OS and other application components, or between different Android application components. Like Android services, broadcast receivers operate in the background. Android NotificationManager Class is used to notify.

Content Providers
Used mainly for Data Management.
Store and retrieve data and make it accessible to all applications Example: Android ships with a number of content providers for common data types (e.g., audio, video, images, personal contact information, etc.)

Provides uniform APIs for: querying delete, update and insert.

Content is represented by URI and MIME type

Intents
Think of Intents as a verb and object; a description of what you want done.
E.g. VIEW, CALL, PLAY etc..
Home

Contacts

System matches Intent with Activity that can best provide the service Activities and Intent Receivers describe what Intents they can service

GMail

Chat

Blogger Blogger

Any queries after class kr.gaurav0209@gmail.com


kumar.gaurav@synerzip.com (+91) 9022 839 292

Vous aimerez peut-être aussi