Einf hrung in Android Einführung in die...
Transcript of Einf hrung in Android Einführung in die...
Einführung in Android
Sebastian Süß
Einführung in die Anwendungsentwicklung
FH Gießen Friedberg
27. Oktober 2010
Freitag, 29. Oktober 2010
Sebastian Süß
Inhalt
• Was bisher geschah / Prognosen
• Grundlagen
• Entwicklungsumgebung
• Beispiel: Notensammler
• Intents und Filter
• Beispiel: Erweiterungsmechanismus
• Fragen
2Freitag, 29. Oktober 2010
Sebastian Süß
Was bisher geschah
2005 • Google kauft Android Inc.
2007 • Open Handset Alliance mit 35 (jetzt 78) Unternehmen gegründet. Branchen: Software, Hardware, Netzbetreiber, Marketing
2008• T-Mobile G1 / Android Market Start in USA und GB
• Android wird Open Source (Apache License V. 2.0)
• SDK 1.0 (API Level 1) veröffentlicht
2009 • Versionen: 1.1 , 1.5 „Cupcake“, 1.6 „Donut“, 2.0 „Eclair“
2010 • 2.1 „Eclair“, 2.2 (AL 8) „Froyo“ Mai, 3.0 „Gingerbread“ Ende 2010
3
Android Market 20.10.2010
Freitag, 29. Oktober 2010
Sebastian Süß
Prognosen - Aufteilung weltweit 2010 - 2014
4
Source (2010): http://www.gartner.com/it/page.jsp?id=1434613
OS Unit Sales WW (M) 2014 2010 Difference
Symbian 264,4 30,2 % 40,1 % -9,9 %
Android 259,3 29,6 % 17,7 % 11,9 %iOS 130,4 14,9 % 15,4 % -0,5 %
RIM OS 102,6 11,7 % 17,5 % -5,8 %Win Phone 34,5 3,9 % 4,7 % -0,8 %
Other 84,5 9,6 % 4,7 % 4,9 %Total 875,7
Freitag, 29. Oktober 2010
Sebastian Süß
Grundlagen
5Freitag, 29. Oktober 2010
Sebastian Süß
Grundlagen - Android Platform
6
Libraries (C/C++)
SQLite
WebKit
FreeType
OpenGL ES
MediaFramework
...
Android Runtime
Core Lib
Dalvik VM
Application Framework
LocationManager
NotificationManager
ResourceManager
...
Applications (Apps)
Home / Launcher Contacts Browser MyApp
http://developer.android.com/guide/basics/what-is-android.html
Linux Kernel
DisplayDriver
CameraDriver
ProcessManagement
...
Freitag, 29. Oktober 2010
Sebastian Süß
Grundlagen - Build einer Anwendung
7
.java
.java
.java
.xml
.png
.xml
javac
.class
.class
.class dx
classes.dexaapt
.xml
.png
.xml
classes.dex
.apk signieren
Projektsrc
res
Freitag, 29. Oktober 2010
Sebastian Süß
Grundlagen - Anwendung & Prozesse
Jede Anwendung...
• besitzt eine eindeutige Benutzer ID (app_<nummer>)
• läuft isoliert in einem Linux-Prozess mit eigener DVM
http://developer.android.com/guide/topics/fundamentals.html
8
USER PID ! PPID!! NAMEroot 33 1 ! zygoteapp_17 127 33 ! com.android.launcherapp_21 241 33 ! com.android.emailapp_34 314 33 ! de.mlh.vtrainerapp_32 337 33 ! ! de.tu.ap.notensammleru2
Freitag, 29. Oktober 2010
Sebastian Süß
Grundlagen - Anwendung & Prozesse
Jede Anwendung...
• besitzt einen eigenen „Sandkasten“ im Dateisystem(/data/data/<app_ID>)
• bekommt nur die Berechtigungen, die vom Entwickler nach außen bekannt gemacht werden(<uses-permission>)
http://developer.android.com/guide/topics/fundamentals.html
9
/data/data/
Freitag, 29. Oktober 2010
Sebastian Süß
Grundlagen - Anwendungs-Komponenten
10
Anwendungs-Komponenten
Activity Service
BroadcastReceiver
ContentProvider
Freitag, 29. Oktober 2010
Sebastian Süß
Grundlagen - Anwendungs-Komponenten - Activity
11
• Komponente für eine bestimmte Benutzeraktivität
• Verwaltet Benutzerschnittstelle / Menüs / Dialoge
• Eine Anwendung besitzt i.d.R. mehrere Activities
Activity 1: ListContacts Activity 2: EditContact
...
Anwendung: Kontakte
Freitag, 29. Oktober 2010
Sebastian Süß
Grundlagen - Anwendungs-Komponenten - Service
12
• Komponente für Hintergrundaktivitäten
• Keine Benutzerschnittstelle
• Stellt Dienste (remote/local) bereit
Service: MusicPlayer Activity: MusicController
Anwendung: Music player
local
Activity: MusicController
Anwendung: Music player 2
remote
Freitag, 29. Oktober 2010
Sebastian Süß
Grundlagen - Anwendungs-Komponenten - Broadcast receiver
13
• Empfängt und verarbeitet Nachrichten
• Bsp. „battery low“ oder „picture taken“
Anwendung: Music player
local
Service: MusicPlayer
BR: CallReceiver
incoming call
Freitag, 29. Oktober 2010
Sebastian Süß
Grundlagen - Anwendungs-Komponenten - Content provider
14
• Regelt Datenzugriff zwischen Anwendungen
• Unterstützt Records und Datenströme
URI (enthält Content Provider Id + Datentyp)
+ Projektion, Selektion und Sortierung
Anwendung: Kontakte
DB
Activity: Backup
Anwendung: Backup
Data Cursor (read only): contact recordsContentResolver ContactCP
URI Beispiel:
content://com.example.contactcp/contacts
Präfix Authority (CP Id) Datentyp
Freitag, 29. Oktober 2010
Sebastian Süß
Entwicklungsumgebung
15Freitag, 29. Oktober 2010
Sebastian Süß
Entwicklungsumgebung - Android SDK
Android SDK http://developer.android.com/sdk
Debug Monitor, Tracer, Emulator,...
Installierte Android Plattformen: System images, Dev libs,...
Installierte add-on APIs: z.B. Google API level 7 (Android 2.1)
16
Android SDK and Device Manager
.
.
.
Freitag, 29. Oktober 2010
Sebastian Süß
Entwicklungsumgebung - Android AVD Manager
17Freitag, 29. Oktober 2010
Sebastian Süß
Entwicklungsumgebung - Android AVD Manager
18Freitag, 29. Oktober 2010
Sebastian Süß
Entwicklungsumgebung - Android Emulator
19
Home App
Standard Knöpfe
Launcher
System-Statusleiste
Freitag, 29. Oktober 2010
Sebastian Süß
Entwicklungsumgebung - Android Developer Tools (ADT) Eclipse
Installation über: https://dl-ssl.google.com/android/eclipse/20
Freitag, 29. Oktober 2010
Sebastian Süß
Standardbeispiel - Projekt erstellen
21
ID der Anwendungz.B. com.android.phonecom.google.maps
Freitag, 29. Oktober 2010
Sebastian Süß
Standardbeispiel - Projektstruktur
22
public class MyFirstAppActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Freitag, 29. Oktober 2010
Sebastian Süß
Standardbeispiel - Projektstruktur
Ressourcen-Ordner: Verschiedene Ressourcen-Typen (Bilder, Texte,...)
23
<resources> <string name="hello">Hello World, MyFirstAppActivity!</string> <string name="app_name">My first Android application</string></resources>
Freitag, 29. Oktober 2010
Sebastian Süß
Standardbeispiel - Projektstruktur
24
/* AUTO-GENERATED FILE. DO NOT MODIFY. * * ... */package de.suess.firstapp;
public final class R {
public static final class drawable { public static final int icon=0x7f020000; }
public static final class layout { public static final int main=0x7f030000; }
public static final class string { public static final int app_name=0x7f050000; public static final int hello=0x7f040000; }}
Freitag, 29. Oktober 2010
Sebastian Süß
Standardbeispiel - Projektstruktur
25
Enthält weiterhin nur eine ID für Standard- und Alternativ-Ressource
Alternativen für Landscape
Alternativen für Sprache „deutsch“
App
Freitag, 29. Oktober 2010
Sebastian Süß
Standardbeispiel - Projektstruktur
26
<manifest xmlns:android="..." package="de.suess.firstapp" android:versionCode="1" android:versionName="1.0_pre-alpha-midnight-build">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MyFirstAppActivity" ... > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity>
</application>
<uses-sdk android:minSdkVersion="4" /> <uses-permission android:name="android.permission.CAMERA" />
</manifest>
Freitag, 29. Oktober 2010
Sebastian Süß
Standardbeispiel - Layout
27
<LinearLayout xmlns:android="..." android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>
Activity-TitelleisteTextView
LinearLayout
ViewGroup
View
Freitag, 29. Oktober 2010
Sebastian Süß
Anwendung: Notensammler
Erweitertes Beispiel: Notensammler
28
speichern
Activity 2: NoteErfassenActivity 1: NoteListe
anlegen
Freitag, 29. Oktober 2010
Sebastian Süß
Erweitertes Beispiel - Layout NoteErfassen
TextView...
RadioGroup
Spinner
EditText (number)
EditText (number)
Button
LinearLayout (vertical)
AutoCompleteTextView
29Freitag, 29. Oktober 2010
Sebastian Süß
Erweitertes Beispiel - Layout NoteErfassen
30
• Neue Ressource hinzufügen: @+id/<name>
• Schwellwert Vorschläge:completionThreshold="<Anzahl Zeichen>"
<LinearLayout ...>
<TextView android:text="@string/info_insert_mark".../> <TextView android:text="@string/module".../>
<AutoCompleteTextView android:id="@+id/autotxt_modul" android:layout_width="fill_parent" android:layout_height="wrap_content" android:completionThreshold="3" />
<RadioGroup android:id="@+id/rgrp_term"...>
<RadioButton android:id="@+id/rbtn_st" android:text="@string/st" android:checked="true" .../> <RadioButton android:id="@+id/rbtn_wt" android:text="@string/wt".../>
</RadioGroup>
Freitag, 29. Oktober 2010
Sebastian Süß
Erweitertes Beispiel - Layout NoteErfassen
31
<Spinner android:id="@+id/spin_year" .../>
<TextView android:text="@string/crp" .../>
<EditText android:id="@+id/etxt_crp" android:inputType="number" .../>
<TextView android:text="@string/mark_percent".../>
<EditText android:id="@+id/etxt_mark_percent" android:inputType="number" .../>
<Button android:id="@+id/btn_save" android:text="@string/save" android:onClick="onButtonClick" .../>
</LinearLayout>
• Eingabewerte bestimmen mit: inputType="<type>"
• Call-back-Methode für ButtonClick: onClick="onButtonClick"
Freitag, 29. Oktober 2010
Sebastian Süß
Erweitertes Beispiel - Ressourcen erweitern
<resources> <string-array name="modules"> <item>CS1001 Grundlagen der Informatik</item> <item>CS1003 Rechnerarchitektur</item> <item>CS1004 Algorithmen & Datenstrukturen</item> <item>CS1005 Objektorientierte Programmierung</item> ... </string-array></resources>
32
Konstante: R.array.modules
Freitag, 29. Oktober 2010
Sebastian Süß
Erweitertes Beispiel - Activity NoteErfassen anpassen
public class NoteErfassen extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.note_erfassen); // configure auto text view AutoCompleteTextView autotxtModul = (AutoCompleteTextView) findViewById(R.id.autotxt_modul);
String[] suggest = getResources().getStringArray(R.array.modules);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, suggest);
autotxtModul.setAdapter(adapter);
// configure year spinner Spinner spinModul = (Spinner) findViewById(R.id.spin_year);
ArrayAdapter<Integer> adapter2 = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, getYears());
spinModul.setAdapter(adapter2);...
33Freitag, 29. Oktober 2010
Sebastian Süß
Erweitertes Beispiel - Activity NoteErfassen anpassen
public class NoteErfassen extends Activity {... public void onButtonClick(View target) {
switch (target.getId()) { case R.id.btn_save: persistNote(); // beende Activity finish(); break; } }...
• Note wird gespeichert
• Activity beendet und zurück zu „NoteListe“
34Freitag, 29. Oktober 2010
Sebastian Süß
Erweitertes Beispiel - Activity NoteListe erstellen
35
ListViewListItem (android.R.layout.simple_list_item_1)
.
.
.
Freitag, 29. Oktober 2010
Sebastian Süß
Erweitertes Beispiel - Activity NoteListe erstellen
public class NoteListe extends ListActivity {... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(Bundle savedInstanceState);
NoteVO[] noten = getNoten();
ArrayAdapter<NoteVO> adapter = new ArrayAdapter<NoteVO>(this, android.R.layout.simple_list_item_1, noten);
setListAdapter(adapter); }...
• ListActivity enthält Standard-Layout (ListView)
• ListItem-Layout: simple_list_item_1
36Freitag, 29. Oktober 2010
Sebastian Süß
Erweitertes Beispiel - NoteListe Optionsmenü
37Freitag, 29. Oktober 2010
Sebastian Süß
Erweitertes Beispiel - NoteListe Optionsmenü
38
<menu ...>
<item android:id="@+id/item_add_mark"android:title="@string/add_mark"android:icon="@android:drawable/ic_menu_add" />
<item android:id="@+id/item_show_catalog" android:title="@string/show_catalog" android:icon="@android:drawable/ic_menu_view" />
</menu>
http://androiddrawableexplorer.appspot.com/
Freitag, 29. Oktober 2010
Sebastian Süß
Erweitertes Beispiel - NoteListe Optionsmenü
39
public class NoteListe extends ListActivity {...@Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.note_liste_menue, menu);
return true; }
Freitag, 29. Oktober 2010
Sebastian Süß
Erweitertes Beispiel - NoteListe Optionsmenü
40
public class NoteListe extends ListActivity {...@Override public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item_add_mark: Intent i = new Intent(this, NoteErfassen.class); startActivity(i); break;
; ; ; }
return super.onOptionsItemSelected(item); }
Freitag, 29. Oktober 2010
Sebastian Süß
Erweitertes Beispiel - NoteListe Optionsmenü
41
public class NoteListe extends ListActivity {...@Override public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item_add_mark: Intent i = new Intent(this, NoteErfassen.class); startActivity(i); break;
case R.id.item_show_catalog: Intent i2 = new Intent(Intent.ACTION_VIEW); i2.setData(Uri.parse("http://www.mni.fh-giessen.de")); startActivity(i2); break; }
return super.onOptionsItemSelected(item); }
Expliziter IntentKomponente zur Programmierzeit bekannt!
Impliziter IntentKomponente wird zur Laufzeit ermittelt
Freitag, 29. Oktober 2010
Sebastian Süß
Intent - implizit
42
startActivity
Activity aus App Browser
AndroidPlatform
startActivity
search matching Activity
Freitag, 29. Oktober 2010
Sebastian Süß
Intent - implizit
43
Activity aus App Contact Activity aus App Phone
Intent i = new Intent(Intent.ACTION_DIAL);i.setData(Uri.parse("tel:+49 641 309 1234"));startActivity(i);
startActivity
AndroidPlatform
startActivity
search matching Activity
Freitag, 29. Oktober 2010
Sebastian Süß
Intent - implizit
44
Mehrere Activities für einen Intent
Keine Activity gefunden
Was kann beim Senden von impliziten Intents passieren?
• Genau eine Activity wurde gefunden und gestartet
• Mehrere Activities wurden gefunden
• Keine Activity wurde gefunden
Freitag, 29. Oktober 2010
Sebastian Süß
Intent - Filter
45
IntentKomp. A
Komp. C
Komp. B
?
Wie findet das System eine passende Komponente zu einem impliziten Intent?
Filter A
Filter B
Filter C
Ein Filter beschreibt, auf welche Intents eine Komponente (Activity, Service, BR) reagiert
Freitag, 29. Oktober 2010
Sebastian Süß
Intent - Filter
46
<activity android:name=".MyVideoViewer"> <intent-filter> <action android:name="android.intent.action.VIEW" />
<data android:mimeType="video/quicktime" /> <data android:mimeType="video/mpeg" />
<category android:name="android.intent.category.DEFAULT" /> </intent-filter></activity>
Freitag, 29. Oktober 2010
Sebastian Süß
Intent - Filter
47
<activity android:name=".MyBrowser"> <intent-filter> <action android:name="android.intent.action.VIEW" />
<data android:scheme="http" /> <data android:scheme="https" />
<category android:name="android.intent.category.DEFAULT" /> </intent-filter></activity>
Freitag, 29. Oktober 2010
Sebastian Süß
Intent - Filter
48
<receiver android:name=".StartUpBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter></receiver>
Freitag, 29. Oktober 2010
Sebastian Süß
Intent - Filtertest
49
Action test
Positiv, wenn die Action mit einem Action-Filter-Element übereinstimmt
android.intent.action.VIEWandroid.intent.action.EDIT
android.intent.action.VIEWCATEGORY_DEFAULThttp://xyz.de/test.mpg
http://developer.android.com/guide/topics/intents/intents-filters.html#ires
Intent filter
Freitag, 29. Oktober 2010
Sebastian Süß
Intent - Filtertest
50
• Positiv, wenn alle Kategorien im Intent auch im Filter vorkommen
• Positiv, wenn Kategorie im Intent leer
• Intents, die über startActivity oder startActivityForResult versendet werden, besitzen immer die Kategorie „Default“
Category testpositiv
android.intent.category.DEFAULTandroid.intent.category.BROWSABLEandroid.intent.category.ALTERNATIVE
http://developer.android.com/guide/topics/intents/intents-filters.html#ires
Intent filter
Action test
android.intent.action.VIEWCATEGORY_DEFAULThttp://xyz.de/test.mpg
Freitag, 29. Oktober 2010
Sebastian Süß
Intent - Filtertest
51
Positiv, wenn die Daten im Intent zu einem Data-Filter-Element passen
<data android:mimeType="video/mpeg" android:scheme="http"/><data android:mimeType="audio/mpeg" android:scheme="http"/>
Data test
http://developer.android.com/guide/topics/intents/intents-filters.html#ires
Category testAction test
android.intent.action.VIEWCATEGORY_DEFAULThttp://xyz.de/test.mpg
positiv
Intent filter
Freitag, 29. Oktober 2010
Sebastian Süß
Beispiel: Erweiterungsmechanismus
Anforderungen:• Basisanwendung mit Testlektion• Weitere Lektionen können nachgekauft werden
52Freitag, 29. Oktober 2010
Sebastian Süß
Beispiel: Erweiterungsmechanismus
Plugin: A
DB
CP RequestBR
Anwendung: Small-Talk
DB
ResponseBR
Plugin: B
DB
CP RequestBR
1. Start
2. sendBroadcastID: de.mlh.st.PLUGIN_REQUEST
53Freitag, 29. Oktober 2010
Sebastian Süß
Beispiel: Erweiterungsmechanismus
Plugin: A
DB
CP RequestBR
Anwendung: Small-Talk
DB
ResponseBR
Plugin: B
DB
CP RequestBR
4. Register
3. sendBroadcastID: de.mlh.st.PLUGIN_RESPONSE
ContentProvider Authority ID
54Freitag, 29. Oktober 2010
Sebastian Süß
Beispiel: Erweiterungsmechanismus
Plugin: A
DB
CP RequestBR
Anwendung: Small-Talk
DB
ResponseBR
Plugin: B
DB
CP RequestBR
CResolver
6. Query:
content://de.mlh.st.pluginA/lection
5.
55Freitag, 29. Oktober 2010
Sebastian Süß
Beispiel: Erweiterungsmechanismus
Plugin: A
DB
CP RequestBR
Anwendung: Small-Talk
DB
ResponseBR
Plugin: B
DB
CP RequestBR
CResolver
7. Query:
content://de.mlh.st.pluginB/lection
5.
56Freitag, 29. Oktober 2010
Sebastian Süß
Stresstest
57
http://developer.android.com/guide/developing/tools/monkey.html
Freitag, 29. Oktober 2010
Sebastian Süß
Fragen
58
?
Freitag, 29. Oktober 2010
Sebastian Süß
Quellen
• Google Inc. Webseite, 19.6.2010. http://developer.android.com
• Arno Becker, Markus Pant: Android, Grundlagen der Programmierung, dpunkt.verlag 2009
• Sayed Hashimi et al.: Pro Android 2, Apress 2010
59Freitag, 29. Oktober 2010