tutorial android

42

Upload: admarques

Post on 15-Dec-2015

12 views

Category:

Documents


2 download

DESCRIPTION

Tutorial para criar programas para android

TRANSCRIPT

Page 1: Tutorial Android
Page 2: Tutorial Android

Recursos

- Application framework proporciona a reutilização e substituição de

componentes

- Dalvik virtual machine otimizada para dispositivos móveis

- Browser Integrado baseado no webkit engine

- Gráficos Otimizados possui uma biblioteca 2D; e 3D baseada na

especificação OpenGL ES 1.0 (aceleração de hardware é opcional)

- SQLite para guardar dados estruturados

- Suporte multimídia para áudio, vídeo e formatos de imagem (MPEG4,

H.264, MP3, AAC, AMR, JPG, PNG, GIF)

- Telefonia GSM (dependente de hardware)

- Bluetooth, EDGE, 3G, e WiFi (dependente de hardware)

- Câmera, GPS, compasso, e acelerômetro (dependente de hardware)

- Rico ambiente de desenvolvimento , incluindo um emulador de

dispositivo,

ferramentas de depuração, memória, performance e um plugin para o Eclipse

(ADT)

Page 3: Tutorial Android

Execução

Emulador é pesado

Leva de 1 a 10 minutos para carregá-lo

Pelo menos 1GB de RAM, para não

ficar travando

Page 4: Tutorial Android

Execução

Page 5: Tutorial Android

Execução

Page 6: Tutorial Android

Organização

Page 7: Tutorial Android

Organização

XML + JAVA

\src : fonte java das aplicações

\res\drawable: imagens estáticas do programas. (ícones)

\res\layout: telas do programa (XML)

AndroidManifest.xml: permissões e definições

\gen\R.java: faz a ligação entre o JAVA e o XML. (Não pode ser modificado)

Page 8: Tutorial Android

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="br.com.android"

android:versionCode="1"

android:versionName="1.0">

<uses-permission android:name="android.permission.BLUETOOTH"/>

<application android:icon="@drawable/icon"

android:label="@string/app_name">

<activity android:name=".AppHello“

android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category

android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

Page 9: Tutorial Android

Permissões

ACCESS_CHECKIN_PROPERTIES :Allows read/write access to the "properties" table in the checkin database, to change values that get uploaded.

ACCESS_COARSE_LOCATION: Allows an application to access coarse (e.g., Cell-ID, WiFi) location.

ACCESS_FINE_LOCATION: Allows an application to access fine (e.g., GPS) location.

Etc. (~100)

Disponíveis em: http://developer.android.com/reference/android/Manifest.permission.html

Page 10: Tutorial Android

XML - Layout

Page 11: Tutorial Android

XML - Layout

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >

<TextView android:id="@+id/text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello, I am a TextView" />

<Button android:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello, I am a Button" />

</LinearLayout>

Page 12: Tutorial Android

Tipos Layouts - FrameLayout

FrameLayout is the simplest type of layout object. It's basically a blank space on your screen that you can later fill with a single object — for example, a picture that you'll swap in and out. All child elements of the FrameLayout are pinned to the top left corner of the screen; you cannot specify a different location for a child view. Subsequent child views will simply be drawn over previous ones, partially or totally obscuring them (unless the newer object is transparent).

Page 13: Tutorial Android

Tipos Layouts – LinearLayout

LinearLayout aligns all children in a single direction — vertically or horizontally, depending on how you define the orientation attribute. All children are stacked one after the other, so a vertical list will only have one child per row, no matter how wide they are, and a horizontal list will only be one row high (the height of the tallest child, plus padding). A LinearLayoutrespects margins between children and the gravity (right, center, or left alignment) of each child.

Page 14: Tutorial Android

Tipos Layouts – LinearLayout

Page 15: Tutorial Android

Tipos Layouts – TableLayout

TableLayout positions its children into

rows and columns. TableLayout

containers do not display border lines for

their rows, columns, or cells. The table

will have as many columns as the row

with the most cells. A table can leave

cells empty, but cells cannot span

columns, as they can in HTML.

Page 16: Tutorial Android

Tipos Layouts – TableLayout

<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"android:layout_height="fill_parent"android:stretchColumns="1"><TableRow>

<TextViewandroid:text="@string/table_layout_4_open"android:padding="3dip" />

<TextViewandroid:text="@string/table_layout_4_open_shortcut"android:gravity="right"android:padding="3dip" />

</TableRow>

<TableRow><TextView

android:text="@string/table_layout_4_save"android:padding="3dip" />

<TextViewandroid:text="@string/table_layout_4_save_shortcut"android:gravity="right"android:padding="3dip" />

</TableRow></TableLayout>

Page 17: Tutorial Android

Tipos Layouts – TableLayout

Page 18: Tutorial Android

Tipos Layouts – RelativeLayout

RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on. Elements are rendered in the order given, so if the first element is centered in the screen, other elements aligning themselves to that element will be aligned relative to screen center. Also, because of this ordering, if using XML to specify this layout, the element that you will reference (in order to position other view objects) must be listed in the XML file before you refer to it from the other views via its reference ID.

Page 19: Tutorial Android

Tipos Layouts – RelativeLayout

Page 20: Tutorial Android

Tipos Layouts – Outros

Gallery A horizontal scrolling display of images, from a bound list.

GridView Displays a scrolling grid of m columns and n rows.

ListView Displays a scrolling single column list.

ScrollView A vertically scrolling column of elements.

Spinner Displays a single item at a time from a bound list, inside a one-row textbox. Rather like a one-row listbox that can scroll either horizontally or vertically.

SurfaceView Provides direct access to a dedicated drawing surface. It can hold child views layered on top of the surface, but is intended for applications that need to draw pixels, rather than using widgets.

TabHost Provides a tab selection list that monitors clicks and enables the application to change the screen whenever a tab is clicked

ViewFlipper A list that displays one item at a time, inside a one-row textbox. It can be set to swap items at timed intervals, like a slide show.

ViewSwitcher Same as ViewFlipper.

Page 21: Tutorial Android

Views - Widgets

View

Button

CheckBox

AnalogClocks

DigitalClock

ImageButton

ProgressBar

TextView

Etc.

http://developer.android.com/reference/android/view/View.html - Funções e atributos XML e JAVA para as Views

Page 22: Tutorial Android

Views

android:autoLink setAutoLinkMask(int

android:autoText setKeyListener(KeyListener)

android:bufferTypesetText(CharSequence,TextView.BufferType)

android:capitalize setKeyListener(KeyListener)

android:cursorVisible setCursorVisible(boolean)

android:digits setKeyListener(KeyListener)

android:drawableBottomsetCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable)

android:drawableLeftsetCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable)

Nem a metade dos atributos do TextView.

Page 23: Tutorial Android

XML - Amigável

http://www.droiddraw

.org

Page 24: Tutorial Android

Java

Toda classe deve ser derivada da Activity.

Método principal: onCreate(BundlesavedInstanceState)

Para mostrar a página principal: setContentView(R.layout.main);

Override nada mais é do que a substituição de métodos, variáveis. (Eclipse já sugere onde deve se colocar esse tipo de comando)

Page 25: Tutorial Android

Java

package br.com.android;

import android.app.Activity;

import android.os.Bundle;

public class AppHello extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(BundlesavedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

}

Page 26: Tutorial Android

Dalvik Debug Monitor Server

(DDMS)

Page 27: Tutorial Android

DDMS

Threads

VM Heap

Emulator Control ( Location,

Telefone)

File Explorer (Inserir e remover

arquivos do SD card)

Etc.

Page 28: Tutorial Android

LogCat View

Page 29: Tutorial Android

Emulator Console via telnet

Nem sempre DDMS funciona.

# connects to device: telnet localhost 5554

# set the power level: power status full ou

power status charging

# make a call to the device: gsm call

012041293123

# send a sms to the device: sms send

12345 Will be home soon

# set the geo location: geo fix <lon> <lat>

Page 30: Tutorial Android

Exemplo - Mapa

Page 31: Tutorial Android

Exemplo - Chave

Obter certificado para utilizar o mapa:

keytool.exe -list -alias androiddebugkey

-keystore "C:\android\debug.keystore" -

storepass android -keypass android

Obter chave através do certificado:

http://code.google.com/android/maps-

api-signup.html

Page 32: Tutorial Android

Exemplo – AndroidManifest.xml

Page 33: Tutorial Android

Exemplo – main.xml

Page 34: Tutorial Android

Exemplo - Java

Page 35: Tutorial Android

Exemplo – Resultado 1

Page 36: Tutorial Android

Exemplo - Zoom

Page 37: Tutorial Android

Exemplo - Java

Page 38: Tutorial Android

Exemplo – Resultado 2

Page 39: Tutorial Android

Exemplo – Mudando perspectiva

mapView.setSatellite(true);

mapView.setStreetView(true);

Page 40: Tutorial Android

Exemplo – Resultado 3

Page 41: Tutorial Android

Exemplo – Local padrão

Page 42: Tutorial Android

Referências

http://www.portalandroid.org/comunidad

e/

Apostila de Android: Programando

Passo a Passo 3ª Edição De :

Luciano Alves da Silva

http://developer.android.com/guide

Professional Android Application

Development - Reto Meier