180 likes | 309 Views
Android Tutorial. naderjawad@gmail.com. Android. Written in Java Utilizes Dalvik VM Just in time (JIT) compilation since Android 2.2. Java Tips. Similar syntax to C/C++ All function/procedure arguments passed in by value Class objects are references that are passed by value
E N D
Android Tutorial naderjawad@gmail.com
Android • Written in Java • Utilizes Dalvik VM • Just in time (JIT) compilation since Android 2.2
Java Tips • Similar syntax to C/C++ • All function/procedure arguments passed in by value • Class objects are references that are passed by value • No support for multiple inheritance • Does support multiple interfaces • Garbage collected (Hooray!) • Beware! Can still create memory leaks!
Java Tips • All methods are virtual by default • Java naming convention
Java Memory Leaks • Every class object is a reference to the object • Java VM keeps track of reference counts to every object • Once nothing refers to an object anymore it gets garbage collected • Once finished with an object, set it to null to release the memory
Android Components • Activity • Code that has a user interface associated with it • Implemented as a stack • Service • Code that runs in the background without a user interface • Broadcast Receiver • Code that runs in response to Android OS or Application intents(messages)
Android Components (cont.) • Content Providers • Database that is exposed to all applications to consume • Intents • Messages that are passed throughout the Android OS/ other applications
Building Android Applications • Create UI using XML • New UI dev tools make building UIs awesome • Once an xml file is saved, the Eclipse ADT tools create an R.java • DO NOT MODIFY R.java!! • Never a good idea, ever like forealz
Building Android Applications • Reference UI elements by utilizing findViewById(int) member method on View object • Ex. • Button b = (Button)findViewById(R.id.myButton); • findViewById is expensive!
Starting New Activities • Intent i = new Intent(); • i.setClass(callingActivity.this, calleeActivity.this); • startActivity(i);
Passing objects to Activities • Easiest way, just create a static data structure • Accessible to all activities within an application • Pass objects through bundles in an intent • Intent i = new Intent(); • i.putExtra(key, stringValue); • Retreive object • Bundle b = getIntent().getExtras(); • String o = b.getString(key);
Passing values to Activities • Can pass primitive types through bundles • char, int, float, double, String • Can pass custom objects by implementing Parcelable interface • http://developer.android.com/reference/android/os/Parcelable.html
Be careful of ANRs! • ANR = Application Not Responding • Occurs when application is not responsive to touch events for ~5 seconds • All major computation should be done on a separate thread • Use threads • Need a runnable object to update the UI thread • AsyncTask • http://developer.android.com/reference/android/os/AsyncTask.html
Running Android Apps • Can use Android emulator • Obnoxiously slow • Better emulator coming soon, not quite sure when • Debugging on device is king!
Building Apps for Varying Devices • Java code same (mostly) • Add additional xml layout files for each device • Save in corresponding directories • layout-land • Folder for landscape view • layout-mdpi • Folder for layouts for devices with medium density displays
Building Apps for Varying Devices • Save copies of same image in different resolutions in drawable folders • drawable-hdpi • drawable-mdpi • drawable-ldpi
Android Extra Fun Stuff • Renderscript • New library of APIs that allows for easy animation creation • Also useful for complex computation • ADK • Accessory Development Kit • Allows for development of accessories that interface with devices through USB • Based off of Arduino • NDK (native development kit) • JNI (Java Native Interface) • Allows for glue between Java and C code for extra performance • Note just writing your application in C++ using the NDK doesn’t make it faster than normal Java implementation
Hints Tips Tricks • Android.com • Google IO Sessions • http://www.google.com/events/io/2011/sessions.html • Android developer blog • http://android-developers.blogspot.com • Stackoverflow • google