Android - Benutzer-Homepage-Server der TH Mittelhessen

Transcrição

Android - Benutzer-Homepage-Server der TH Mittelhessen
Einführung in die
Einführung in Android
Anwendungsentwicklung
Sebastian Süß
FH Gießen Friedberg
27. Oktober 2010
Freitag, 29. Oktober 2010
Inhalt
• Was bisher geschah / Prognosen
• Grundlagen
• Entwicklungsumgebung
• Beispiel: Notensammler
• Intents und Filter
• Beispiel: Erweiterungsmechanismus
• Fragen
Sebastian Süß
Freitag, 29. Oktober 2010
2
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
Android Market 20.10.2010
Sebastian Süß
Freitag, 29. Oktober 2010
3
Prognosen - Aufteilung weltweit 2010 - 2014
OS
Symbian
Android
iOS
RIM OS
Win Phone
Other
Total
Unit Sales WW (M) 2014
264,4
259,3
130,4
102,6
34,5
84,5
875,7
30,2 %
29,6 %
14,9 %
11,7 %
3,9 %
9,6 %
2010 Difference
40,1 %
17,7 %
15,4 %
17,5 %
4,7 %
4,7 %
-9,9 %
11,9 %
-0,5 %
-5,8 %
-0,8 %
4,9 %
Source (2010): http://www.gartner.com/it/page.jsp?id=1434613
Sebastian Süß
Freitag, 29. Oktober 2010
4
Grundlagen
Sebastian Süß
Freitag, 29. Oktober 2010
5
Grundlagen - Android Platform
Applications (Apps)
Home / Launcher
Contacts
Browser
MyApp
Application Framework
Location
Manager
Notification
Manager
Resource
Manager
Libraries (C/C++)
...
Android Runtime
SQLite
FreeType
Media
Framework
Core Lib
WebKit
OpenGL ES
...
Dalvik VM
Process
Management
...
Linux Kernel
Display
Driver
Camera
Driver
http://developer.android.com/guide/basics/what-is-android.html
Sebastian Süß
Freitag, 29. Oktober 2010
6
Grundlagen - Build einer Anwendung
Projekt
src
.java
.java
.class
javac
.class
.java
dx
.class
res
.xml
.xml
aapt
classes.dex
.png
.xml
classes.dex
.xml
.png
Sebastian Süß
Freitag, 29. Oktober 2010
.apk
signieren
7
Grundlagen - Anwendung & Prozesse
Jede Anwendung...
• besitzt eine eindeutige Benutzer ID (app_<nummer>)
• läuft isoliert in einem Linux-Prozess mit eigener DVM
USER
root
app_17
app_21
app_34
app_32
PID ! PPID! !
33
1
!
127
33
!
241
33
!
314
33
!
337
33 ! !
NAME
zygote
com.android.launcher
com.android.email
de.mlh.vtrainer
de.tu.ap.notensammleru2
http://developer.android.com/guide/topics/fundamentals.html
Sebastian Süß
Freitag, 29. Oktober 2010
8
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>)
/data/data/
http://developer.android.com/guide/topics/fundamentals.html
Sebastian Süß
Freitag, 29. Oktober 2010
9
Grundlagen - Anwendungs-Komponenten
Anwendungs-Komponenten
Sebastian Süß
Freitag, 29. Oktober 2010
Activity
Service
Broadcast
Receiver
Content
Provider
10
Grundlagen - Anwendungs-Komponenten - Activity
• Komponente für eine bestimmte Benutzeraktivität
• Verwaltet Benutzerschnittstelle / Menüs / Dialoge
• Eine Anwendung besitzt i.d.R. mehrere Activities
Anwendung: Kontakte
Activity 1: ListContacts
Activity 2: EditContact
...
Sebastian Süß
Freitag, 29. Oktober 2010
11
Grundlagen - Anwendungs-Komponenten - Service
• Komponente für Hintergrundaktivitäten
• Keine Benutzerschnittstelle
• Stellt Dienste (remote/local) bereit
Anwendung: Music player 2
Anwendung: Music player
Service: MusicPlayer
Activity: MusicController
remote
Sebastian Süß
Freitag, 29. Oktober 2010
Activity: MusicController
local
12
Grundlagen - Anwendungs-Komponenten - Broadcast receiver
• Empfängt und verarbeitet Nachrichten
• Bsp. „battery low“ oder „picture taken“
Anwendung: Music player
Service: MusicPlayer
incoming
call
Sebastian Süß
Freitag, 29. Oktober 2010
BR:
CallReceiver
local
13
Grundlagen - Anwendungs-Komponenten - Content provider
• Regelt Datenzugriff zwischen Anwendungen
• Unterstützt Records und Datenströme
URI Beispiel:
content://com.example.contactcp/contacts
Präfix
Authority (CP Id)
Datentyp
Anwendung: Backup
Anwendung: Kontakte
Activity: Backup
DB
URI (enthält Content Provider Id + Datentyp)
+ Projektion, Selektion und Sortierung
ContentResolver
Sebastian Süß
Freitag, 29. Oktober 2010
Data Cursor (read only): contact records
ContactCP
14
Entwicklungsumgebung
Sebastian Süß
Freitag, 29. Oktober 2010
15
Entwicklungsumgebung - Android SDK
Android SDK http://developer.android.com/sdk
Installierte add-on APIs: z.B. Google API level 7 (Android 2.1)
Installierte Android Plattformen: System images, Dev libs,...
Debug Monitor, Tracer, Emulator,...
Android SDK and Device Manager
.
.
.
Sebastian Süß
Freitag, 29. Oktober 2010
16
Entwicklungsumgebung - Android AVD Manager
Sebastian Süß
Freitag, 29. Oktober 2010
17
Entwicklungsumgebung - Android AVD Manager
Sebastian Süß
Freitag, 29. Oktober 2010
18
Entwicklungsumgebung - Android Emulator
System-Statusleiste
Standard Knöpfe
Home App
Launcher
Sebastian Süß
Freitag, 29. Oktober 2010
19
Entwicklungsumgebung - Android Developer Tools (ADT) Eclipse
Installation über: https://dl-ssl.google.com/android/eclipse/
Sebastian Süß
Freitag, 29. Oktober 2010
20
Standardbeispiel - Projekt erstellen
ID der Anwendung
z.B.
com.android.phone
com.google.maps
Sebastian Süß
Freitag, 29. Oktober 2010
21
Standardbeispiel - Projektstruktur
public class MyFirstAppActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
setContentView(R.layout.main);
}
Sebastian Süß
Freitag, 29. Oktober 2010
22
Standardbeispiel - Projektstruktur
Ressourcen-Ordner: Verschiedene Ressourcen-Typen (Bilder, Texte,...)
<resources>
<string name="hello">Hello World, MyFirstAppActivity!</string>
<string name="app_name">My first Android application</string>
</resources>
Sebastian Süß
Freitag, 29. Oktober 2010
23
Standardbeispiel - Projektstruktur
/* 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;
}
}
Sebastian Süß
Freitag, 29. Oktober 2010
24
Standardbeispiel - Projektstruktur
App
Enthält weiterhin nur eine ID für Standard- und Alternativ-Ressource
Alternativen für Landscape
Alternativen für Sprache „deutsch“
Sebastian Süß
Freitag, 29. Oktober 2010
25
Standardbeispiel - Projektstruktur
<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>
Sebastian Süß
Freitag, 29. Oktober 2010
26
Standardbeispiel - Layout
ViewGroup
<LinearLayout xmlns:android="..."
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
View
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
Activity-Titelleiste
TextView
LinearLayout
Sebastian Süß
Freitag, 29. Oktober 2010
27
Erweitertes Beispiel: Notensammler
Anwendung: Notensammler
Activity 1: NoteListe
Activity 2: NoteErfassen
anlegen
speichern
Sebastian Süß
Freitag, 29. Oktober 2010
28
Erweitertes Beispiel - Layout NoteErfassen
TextView
.
.
.
LinearLayout (vertical)
AutoCompleteTextView
RadioGroup
Spinner
EditText (number)
EditText (number)
Button
Sebastian Süß
Freitag, 29. Oktober 2010
29
Erweitertes Beispiel - Layout NoteErfassen
<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>
•
•
Neue Ressource hinzufügen: @+id/<name>
Schwellwert Vorschläge:
completionThreshold="<Anzahl Zeichen>"
Sebastian Süß
Freitag, 29. Oktober 2010
30
Erweitertes Beispiel - Layout NoteErfassen
<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"
Sebastian Süß
Freitag, 29. Oktober 2010
31
Erweitertes Beispiel - Ressourcen erweitern
Konstante: R.array.modules
<resources>
<string-array name="modules">
<item>CS1001 Grundlagen der Informatik</item>
<item>CS1003 Rechnerarchitektur</item>
<item>CS1004 Algorithmen &amp; Datenstrukturen</item>
<item>CS1005 Objektorientierte Programmierung</item>
...
</string-array>
</resources>
Sebastian Süß
Freitag, 29. Oktober 2010
32
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);
...
Sebastian Süß
Freitag, 29. Oktober 2010
33
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“
Sebastian Süß
Freitag, 29. Oktober 2010
34
Erweitertes Beispiel - Activity NoteListe erstellen
ListView
ListItem (android.R.layout.simple_list_item_1)
.
.
.
Sebastian Süß
Freitag, 29. Oktober 2010
35
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:
Sebastian Süß
Freitag, 29. Oktober 2010
simple_list_item_1
36
Erweitertes Beispiel - NoteListe Optionsmenü
Sebastian Süß
Freitag, 29. Oktober 2010
37
Erweitertes Beispiel - NoteListe Optionsmenü
<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/
Sebastian Süß
Freitag, 29. Oktober 2010
38
Erweitertes Beispiel - NoteListe Optionsmenü
public class NoteListe extends ListActivity {
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.note_liste_menue, menu);
return true;
}
Sebastian Süß
Freitag, 29. Oktober 2010
39
Erweitertes Beispiel - NoteListe Optionsmenü
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);
}
Sebastian Süß
Freitag, 29. Oktober 2010
40
Erweitertes Beispiel - NoteListe Optionsmenü
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;
Expliziter Intent
Komponente zur Programmierzeit bekannt!
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;
Impliziter Intent
Komponente wird zur Laufzeit ermittelt
}
return super.onOptionsItemSelected(item);
}
Sebastian Süß
Freitag, 29. Oktober 2010
41
Intent - implizit
Activity aus App Browser
search matching Activity
startActivity
startActivity
Android
Platform
Sebastian Süß
Freitag, 29. Oktober 2010
42
Intent - implizit
Activity aus App Contact
Activity aus App Phone
search matching Activity
startActivity
startActivity
Android
Platform
Intent i = new Intent(Intent.ACTION_DIAL);
i.setData(Uri.parse("tel:+49 641 309 1234"));
startActivity(i);
Sebastian Süß
Freitag, 29. Oktober 2010
43
Intent - implizit
Was kann beim Senden von impliziten Intents passieren?
• Genau eine Activity wurde gefunden und gestartet
• Mehrere Activities wurden gefunden
• Keine Activity wurde gefunden
Mehrere Activities für einen Intent
Keine Activity gefunden
Sebastian Süß
Freitag, 29. Oktober 2010
44
Intent - Filter
Wie findet das System eine passende Komponente zu
einem impliziten Intent?
?
Filter A
Komp. A
Filter B
Komp. B
Filter C
Komp. C
Intent
Ein Filter beschreibt, auf welche Intents eine Komponente
(Activity, Service, BR) reagiert
Sebastian Süß
Freitag, 29. Oktober 2010
45
Intent - Filter
<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>
Sebastian Süß
Freitag, 29. Oktober 2010
46
Intent - Filter
<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>
Sebastian Süß
Freitag, 29. Oktober 2010
47
Intent - Filter
<receiver android:name=".StartUpBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Sebastian Süß
Freitag, 29. Oktober 2010
48
Intent - Filtertest
Intent filter
android.intent.action.VIEW
android.intent.action.EDIT
Action test
android.intent.action.VIEW
CATEGORY_DEFAULT
http://xyz.de/test.mpg
Positiv, wenn die Action mit einem Action-Filter-Element übereinstimmt
http://developer.android.com/guide/topics/intents/intents-filters.html#ires
Sebastian Süß
Freitag, 29. Oktober 2010
49
Intent - Filtertest
Intent filter
android.intent.category.DEFAULT
android.intent.category.BROWSABLE
android.intent.category.ALTERNATIVE
Action test
positiv
Category test
android.intent.action.VIEW
CATEGORY_DEFAULT
http://xyz.de/test.mpg
•
•
•
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“
http://developer.android.com/guide/topics/intents/intents-filters.html#ires
Sebastian Süß
Freitag, 29. Oktober 2010
50
Intent - Filtertest
Intent filter
<data android:mimeType="video/mpeg" android:scheme="http"/>
<data android:mimeType="audio/mpeg" android:scheme="http"/>
Action test
Category test
positiv
Data test
android.intent.action.VIEW
CATEGORY_DEFAULT
http://xyz.de/test.mpg
Positiv, wenn die Daten im Intent zu einem Data-Filter-Element passen
http://developer.android.com/guide/topics/intents/intents-filters.html#ires
Sebastian Süß
Freitag, 29. Oktober 2010
51
Beispiel: Erweiterungsmechanismus
Anforderungen:
•
•
Basisanwendung mit Testlektion
Weitere Lektionen können nachgekauft werden
Sebastian Süß
Freitag, 29. Oktober 2010
52
Beispiel: Erweiterungsmechanismus
Anwendung: Small-Talk
DB
1. Start
ResponseBR
2. sendBroadcast
ID: de.mlh.st.PLUGIN_REQUEST
CP
Sebastian Süß
Freitag, 29. Oktober 2010
RequestBR
CP
RequestBR
DB
DB
Plugin: A
Plugin: B
53
Beispiel: Erweiterungsmechanismus
Anwendung: Small-Talk
DB
4. Register
ResponseBR
3. sendBroadcast
ID: de.mlh.st.PLUGIN_RESPONSE
ContentProvider Authority ID
CP
Sebastian Süß
Freitag, 29. Oktober 2010
RequestBR
CP
RequestBR
DB
DB
Plugin: A
Plugin: B
54
Beispiel: Erweiterungsmechanismus
Anwendung: Small-Talk
5.
CResolver
DB
ResponseBR
6. Query:
content://de.mlh.st.pluginA/lection
CP
Sebastian Süß
Freitag, 29. Oktober 2010
RequestBR
CP
RequestBR
DB
DB
Plugin: A
Plugin: B
55
Beispiel: Erweiterungsmechanismus
Anwendung: Small-Talk
5.
CResolver
DB
ResponseBR
7. Query:
content://de.mlh.st.pluginB/lection
CP
Sebastian Süß
Freitag, 29. Oktober 2010
RequestBR
CP
RequestBR
DB
DB
Plugin: A
Plugin: B
56
Stresstest
http://developer.android.com/guide/developing/tools/monkey.html
Sebastian Süß
Freitag, 29. Oktober 2010
57
Fragen
?
Sebastian Süß
Freitag, 29. Oktober 2010
58
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
Sebastian Süß
Freitag, 29. Oktober 2010
59

Documentos relacionados