Android Development Study Notes
After buying an Android phone and spending a day playing with it, I started learning Android programming. After all, I’ll be teaching the college’s “Mobile Device Development” course next semester. A 30-year-old老程序员 has to start learning programming again.
Recording some study notes, continuously updated.
================================================================
Part 1 Basics
Android layers: Hardware -> Linux Kernel -> Dalvik VM -> Android Libraries -> App
Dalvik is not standard Java. It has its own instruction set, a register-based VM. Much of its code comes from Apache Harmony (harmonious indeed).
- Single executable: .class -> .dex file.
- Packaged file: .jar -> .apk (actually just zip, making Chinese localization trivial — no PE file parser needed, just unzip + notepad)
Android Libraries all start with the android namespace, including view, app, util, widget, webkit, etc.
Android supports multitasking. The system can run 6 tasks simultaneously. Beyond that, tasks get killed. Long-press the home button to see the 6 running tasks.
Each Android task runs in its own Linux process, with its own Dalvik instance and user ID. (That’s intense, even more so than .NET.) However, there are ways to run multiple tasks in the same instance, sharing a single VM instance.
Android devices can’t be like the iPhone with just one button. You need at least home, back, and context menu buttons.
Profit model: Android Market cut + Google Services
Part 2 Development Environment Setup
Requirements: JDK + Eclipse + Android SDK + ADT (Google’s Eclipse plugin)
Languages: SDK supports Java; NDK supports C/C++; also Simple language, BASIC-like (Bill Gates weeps….)
Part 3 Getting Started
An Android project generated by IDE includes:
| .classpath // xml path file
| .project // xml project file
| AndroidManifest.xml // android program configuration
| default.properties // android build system
+---.settings // settings
+---assets // currently empty
+---bin // build output
| | classes.dex // dalvik vm executable
| | HelloAndroid.apk // android executable
| | resources.ap_ // resource file
| \---com
| \---hezongjian // java compiled classes
| MyActivity.class
| R.class
+---gen // R.java, like resource.h
| \---com
| \---hezongjian
| R.java // IDE-managed constants
+---res // resources
| +---drawable-hdpi // various resolution resources
| +---drawable-ldpi
| +---drawable-mdpi
| +---layout // UI layouts
| | main.xml // main UI layout
| \---values
| strings.xml // string resources
\---src // source code
` -–com
-–hezongjian
MyActivity.java // main program`
Android UI uses XML layout files like main.xml, where you define widgets.
Main Java file:
public class MyActivity extends Activity {
` public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}`
For a messagebox, use android.widget.Toast. It auto-dismisses after a while.
public boolean onTouchEvent(MotionEvent event) {
` Toast.makeText(this, “Hello”, Toast.LENGTH_SHORT).show();
return super.onTouchEvent(event);
}`
Or use AlertDialog from android.app:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Hello World");
alert.show();
Android apps consist of these parts (none mandatory):
- Activity: like a window, but not quite
- Broadcast Receiver: receives system broadcasts
- Content Provider: provides data to other apps (e.g., an antivirus providing security level)
- Service: a background service
An Android task is a stack of Activities. One task can call another app’s Activity and push it onto its own stack, making it appear as part of itself. This design is more innovative than the tree-structured Windows approach.
These parts are bound together by Intent. Intent is a very abstract concept. Many Chinese explanations are confusing, but the English documentation on developer.android.com (when you can access it) is perfectly clear. Another classic case of Chinese technical translation making simple concepts incomprehensible.