3. About me – Lars Vogel
Independent Eclipse and Android consultant,
trainer and book author
Eclipse committer
Maintains http://www.vogella.de Java, Eclipse
and Android related Tutorials with more then a
million visitors per month.
7. Android from 10 000 feet
- Open Source
- Full stack based on Linux
- Java programming interface
- Project is lead by Google
- Tooling available for Eclipse (and other IDE's)
8.
9. On Android you develop in Java
(There is also the NDK which allows development in C / C++...)
10. Overview of the API Capabilities
Rich UI components
Threads and Background Processing
Full network stack (Http, JSON)
Database available
Images
Access to the hardware (GPS, Camera,
Phone)
and much more............
14. Android Programming
You use the Java
programming language
but Android does not run
Java Bytecode
15. Dalvik
Run Dalvik Executable Code (.dex)
Tool dx converts Java Bytecode into .dex
• Register based VM vrs. stack based as the JVM
• .dex Files contain more then one class
• Approx. 50 % of the size of a class file
• op codes are two bytes instead of one in the JVM
• As of Android 2.2 Dalvik JIT compiler
• Primary engineer: Dan Bornstein
17. Android Development Tools (ADT)
for Eclipse
Eclipse based tooling
• Provide the emulator
• Wizard for creating new project
• Additional Tooling, e.g views
18. Emulator
QEMU-based ARM emulator runs
same image as a device
Use same toolchain to work with
device or emulator
Inital startup is
slooooowwwwww.....
25. Context
Global information about an application environment.
Allows access to application-specific resources and classes
Support application-level operations such as launching
activities, broadcasting and receiving intents, etc.
Basically all Android classes need a context
Activity extends Context
29. Layouts
android.view.ViewGroup
- Layout
- extends View
• Layouts are typically defined via XML
files (resources)
• You can assign properties to the
layout elements to influence their
behavior.
32. Life Cyle of an Activity
void onCreate(Bundle savedInstanceState)
void onStart()
void onRestart()
void onResume()
void onPause()
void onStop()
void onDestroy()
33. States of an Activity
• Active/ Running – Visible and interacts with user
• Paused – still visible but partically obscured (other
transparant activity, dialog or the phone screen),
instance is running but might be killed by the
system
• Stopped – completely obscured, e.g. User presses
the home screen button, instance is running but
might be killed by the system
• Killed – if stopped or paused, system might calling
finish or by killing it
38. Defining Activities
• Create a class which extends
„Activitiy“
• Create an entry in
„AndroidManifest.xml“ for the activity.
• <activity
android:name=“MyNewActivity“></a
ctivity>
43. Implicit Intents
• new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.vogella.de"));
• new Intent(Intent.ACTION_CALL, Uri.parse("tel:
(+49)12345789"));
• new Intent(Intent.ACTION_VIEW,
Uri.parse("geo:50.123,7.1434?z=19"));
• new
Intent("android.media.action.IMAGE_CAPTURE");
44. Gettings results
Intent intent = new
Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);
public void onActivityResult(int requestCode, int
resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode
== 0) {
String result = data.toURI();
Toast.makeText(this, result, Toast.LENGTH_LONG);
}
}
46. XML Drawables
• XML Resources
• Can be used to define transitions,
shapes or state
A drawable resource is something
that can be drawn to the screen
49. Services
Service runs in the background without interacting with the user.
Android provides a multitude of system services
• NotificationService
• VibratorService
• LocationService
• AlarmService
• ....
Access via context.getSystemService(Context.CONST);
You can define your own service for things that should run in the
background
51. Services
Service runs in the background without interacting with the user.
Android provides a multitude of system services
• NotificationService
• VibratorService
• LocationService
• AlarmService
• ....
Access via context.getSystemService(Context.CONST);
You can define your own service for things that should run in the
background
52. Broadcast Receiver – Listen to events
Example:
BATTERY_LOW,
ACTION_BOOT_COMPLETED
android.intent.action.PHONE_STATE
Defined statically in manifest or temporary
via code
Broadcast Receiver only works within the
onReceive() method
C2DM
53. Own Service
• Extend Service or IntentService
• start with startService(Intent) or
bindService(Intent).
• onStartCommand is called in the service
• bindService gets a ServiceConnection as
parameter which receives a Binder object
which can be used to communicate with the
service
54. How to start a service
Broadcast Receiver –
Event: BOOT_COMPLETED
Or
ACTION_EXTERNAL_APPLICATIONS_A
VAILABLE
startService in the onReceiveMethod()
Timer Service
Start BroadcastReceiver which starts the
service
61. Use Threads (not the UI one)
• Long running stuff should run in the
background
• Thread cannot modify the UI
• Synch with the UI thread
runOnUiThread(new Runnable)
62. Handler and AsyncTask
• Handler runs in the UI thread
• Allows to send Message object and or
to send Runnable to it
handler.sendMessage(msg)
handler.post(runnable)
• AsyncTask for more complex work
63. AsyncTask
• To use it subclass it
AsyncTask <TypeOfVarArgParams , ProgressValue ,
ResultValue>
doInBackground() – Do the work
onPostExecute() – Update the UI
65. Lifecycle - Thread
• If Activity is destroyed the Threads should be
preserved.
• Close down Dialogs in the onDestroy() method
• One object can be saved via
onRetainConConfigurationInstance()
• Access via getLastNonConfigurationInstance()
• Only use static inner classes in your Threads
otherwise you create a memory leakage.
67. Option Menu & Action Bar
• Option Menu displays if „Menu“
button is pressed
• You can decided if the entries are
displayed in the Action Bar or in a
menu
• Use „ifRoom“ flag if possible.
68. Option Menu & Action Bar
• Option Menu displays if „Menu“
button is pressed
• You can decided if the entries are
displayed in the Action Bar or in a
menu
• Use „ifRoom“ flag if possible.
70. Read and Write Preferences
• Key – Value pairs
• PreferenceManager.getDefaultShared
Preferences(this);
– To read: getString(key), get...
– To change use edit(), putType(key, value),
commit()
• To maintain use PreferenceActivity
71. PreferenceActivity
• Create resource file (preference)
• New Activity which extends
PreferenceActivity
• To add preferences:
– addPreferencesFromResource(R.xml.prefer
ences);
• Saving and edits will be handled
automatically by the
PreferenceActivity
74. Adapter
Data for ListView
defined by Adapter
View per row defined
by Adapter
75. ListActivity
ListActivity has already a predefined ListView with the id @android:id/list
which will be used automatically.
Provides nice method hooks for typical operations on lists
If you define your own layout this layout must contain a ListView with the
ID:
@+android:id/list
78. Reuse existing rows
If convertView is not null -> reuse it
Saves memory and CPU power (approx. 150 % faster according to
Google IO)
Holder Pattern saves 25%
83. I can talk and
hear
Internet (java.net, Apache HttpClient, JSON...)
Bluetooth
Email
SMS
VoIP (SIP (Session Initiation Protocol))
84. Other Capabilities
Push to device
Interactive Widgets on the homescreen
Live Wallpapers (as background)
Animations and Styling
Simple List handling
(Multi-) Touch
NFC
Canvas / OpenGL ES (Game programming....)
Native Rendering
89. Android: Where to go
from here:
Android Introduction Tutorial
http://www.vogella.de/articles/Android/article.html
Or Google for „Android Development Tutorial“
Android SQLite and ContentProvider Book
http://www.amazon.com/dp/B006YUWEFE
More on Android
http://www.vogella.de/android.html
90. Thank you
For further questions:
Lars.Vogel@gmail.com
http://www.vogella.de
Twitter http://www.twitter.com/vogella
Google+ http://gplus.to/vogella
91. License &
Acknowledgements
• This work is licensed under the Creative Commons
Attribution-Noncommercial-No Derivative Works 3.0
Germany License
– See http://creativecommons.org/licenses/by-nc-nd/3.0/de/deed.en_US