Android Learning Notes
Part II – Android API Framework & Programming Skills
-- Last revised on Nov 30, 2008
1. AndroidManifest.xml
1) Reference page:
/android-sdk-windows-1.0_r1/docs/devel/bblocks-manifest.html
2) Sample:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.my_domain.app.helloactivity">
<application android:label="@string/app_name">
<activity android:name=".HelloActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> <type android:value="vnd.android.cursor.item/vnd.google.note" /> </intent-filter> </activity>
</application>
</manifest>
|
l xmlns:android -- 这是XML命名空间(name space)的声明,它是告诉Android的工具, 你将要涉及到公共的属性?? 已被定义在XML命名空间。在每一个Android的布局文件(包括manifest, layout等) 的最外边的标签必须有这个属性。
l <action android:name="android.intent.action.MAIN" />
-- Primary attributes
action -- The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.
data -- The data to operate on, such as a person record in the contacts database, expressed as a Uri.
l <category android:name="android.intent.category.LAUNCHER"/>
<type android:value="vnd.android.cursor.item/vnd.google.note" />
-- Secondary attributes
category --
1. CATEGORY_LAUNCHER means it should appear in the Launcher as a top-level application.
2. CATEGORY_ALTERNATIVE means it should be included in a list of alternative actions the user can perform on a piece of data.
type – ???
component – ???
extras -- This is a Bundle of any additional information.
l More information about intent please refer to Android’s document at: /android-sdk-windows-1.0_r1/docs/reference/android/content/Intent.html
Note: Every Activity must have an <activity> tag in the manifest whether it is exposed to the world or intended for use only within its own package. If an Activity has no matching tag in the manifest, you won't be able to launch it.
When startActivity(Intent) is called, Android compares the information in an Intent object with the Intent-Filter exposed by every application and finds the activity most appropriate to handle the data or action specified by the caller. Then system picks the Activity whose Intent-Filter best matches intent and launch it.
2. Some classes and API in Android
1) Views
• TextView, EditText, Button, ImageView, Checkbox, Lists, etc
2) Layouts: can be nested
• FrameLayout : Each Child a Layer
• LinearLayout : Single Row / Column
• RelativeLayout : Relative to Parent / Other Views
• TableLayout : Rows and Columns - HTML like
• AbsoluteLayout : <x,y> Coords – Discouraged
3) int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
4)
3. Porting J2ME application to Android platform
1) Replacement classes in Android API for classes in J2ME
J2ME Class |
Android Class | |
javax.microedition.lcdui.* |
Canvas |
android.view.View |
|
Canvas.repaint() |
View.invalidate() |
|
Graphics |
android.graphics.Canvas |
|
Graphics.fillRect(startX, startY, width, height) |
Canvas.drawRect(startX, startY, endX, endY) |
|
Graphics.drawRect(startX, startY, width, height) |
Canvas.drawLine(startX, startY, startX, endY) Canvas.drawLine(startX, startY, endX, startY) Canvas.drawLine(startX, endY, endX, endY) Canvas.drawLine(endX, startY, endX, endY) |
|
Image |
Bitmap, BitmapFactory |
|
Display.setCurrent(Displayable) |
Activity. setContentView(View) |
|
Canvas.keyPressed / keyReleased |
View.onKeyDown() |
|
Left/Right Softkey |
View.onTouchEvent() or Button to simulate |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
|
| |
|
| |
HttpConnection |
HttpURLConnection | |
|
| |
Thread |
Handler.post(Runnable()) | |
|
Typeface mFace = Typeface.createFromAsset(getContext().getAssets(),"fonts/samplefont.ttf") |
4. Programming skills and Tips
1) Do as little work as possible in the constructor of your class that is extended from Activity class, but do most of initialization work in the onCreate() method.
2) The construct of main Activity MUST be modified by public. ???
3) Call View.setFocusable(true) to enable View to have focus and receive user key events.
4) J2ME draws text from left to right and from up downwards to bottom; Android draws text from left to right but from bottom upwards to top, as the following diagram shows.
For image, shape and etc, Android draws in the same way as J2ME.
5) Use the android.util.Log class to log messages in Android.
l Log.v() - Send a VERBOSE log message.
l Log.d() - Send a DEBUG log message.
l Log.i() - Send an INFO log message.
l Log.w() - Send a WARN log message.
l Log.e() - Send an ERROR log message.
6) You should NOT call Activity.getCurrentFocus() in Activity.onStart() / onResume() / onCreate() method, which will return null instance.
7) Resource files' name should not be initialized with digital. Because resource files’ name will be compiled into R.java as an int variable, which should not be initialized with digital. – Further, resource file name must contain ONLY [a-z0-9_.].
8) Asdfasdfd
9) Asdfasdfd
10)
5. Use permissions in AndroidManifest.xml
Permission Type |
Use Cases |
android.permission.INTERNET |
Access Internet using Http or other protocols |
android.permission.READ_CONTACTS android.permission.WRITE_CONTACTS |
Access Contacts (Phone Book) in the handset |
|
|
|
|
6. Thread usage in Android platform
1) Comparing with thread in J2ME
7. Some code snippets useful in programming
1) Draw text in the center of vertical canvas:
y = Canvas.getHeight();
pa = new Paint();
pa.setTextAlign(Paint.Align.CENTER);
Paint.FontMetrics fm = pa.getFontMetrics();
Canvas.drawText("Alpha", x/2, (y-fm.ascent)/2, p);
2)
Compute the string’s width |
Paint.measureText(String str, int start, int end) |
Compute the height of a character |
Paint.ascent() and Paint.descent() |
3) Save a layout view and set back to this view:
View layoutView = Activity.findViewById(R.id.layoutId);
Activity.setContentView(layoutView);
4) Show a floating notice dialog:
Toast.makeText(Activity.this, info.subSequence(0, info.length()),
Toast.LENGTH_SHORT).show();
8. XMPP
Dalvik VM Insight
9. Dex File Anatomy
10. Four Kinds of Memory
clean vs. dirty
• clean: mmap()ed and unwritten
• dirty: malloc()ed
shared vs. private
• shared: used by many processes
• private: used by only one process
• clean (shared or private)
• common dex files (libraries)
· application specific dex files
• shared dirty
· library “live” dex structures
· shared copy-on-write heap (mostly not written)
•
• private dirty
• application “live” dex structures
• application heap
11. The Zygote process
· nascent VM process
· starts at boot time
· preloads and pre-initializes classes
· fork()s on command
From 2008-05-29-Presentation-Of-Dalvik-VM-Internals.pdf P29 page down