2013年5月14日 星期二

[Android] onSaveInstanceState/onRestoreInstanceState

  • 在離開畫面時 onSaveInstanceState() 會被呼叫並保存下當時畫面狀態於 bundle 中,以預防此畫面被不預期關閉; 這樣要重新開啟時,onRestoreInstanceState() 能藉由保存下來的資料來回到關閉前的狀態。
  • 預設會保留住 setContentView 狀態 (ex: 輸入值)。
  • onSaveInstanceState 一定會發生在 onStop 前 但不一定是在 onPause 前或後。
  • 從 Honeycomb 開始 在收到 onStop 回傳前 都不是 kiiable status 這時 onSaveInstanceState 可能會在 onPause 後才被呼叫。

Activity a to Activity b running orders:
  • Open b in a
    • a: onSaveInstanceState -> onPause -> onStop
  • Change orientation:
    • a: onSaveInstanceState -> onPause -> onStop -> onDestroy -> onCreate -> onStart -> onRestoreInstanceState -> onResume
  • Open b with dialog theme in a:
    • a: onSaveInstanceState -> onPause (因為還能看到 a 所以不會呼叫 onStop()!)


以下狀態下不會呼叫 onRestoreInstanceState
  • 從程式集開啟 app 時。
    • onCreate -> onStart -> onResume
  • 從 Activity b 返回 Activity a。
    • onRestart -> onStart -> onResume


* Reference
- onSaveInstanceState
- onRestoreInstanceState

[Linux] shell

當你要電腦傳輸出來『音樂』的時候,你的電腦需要什麼東西呢?
  • 硬體:當然就是需要你的硬體有『音效卡晶片』這個配備,否則怎麼會有聲音;
  • 核心管理:作業系統的核心可以支援這個晶片組,當然還需要提供晶片的驅動程式囉;
  • 應用程式:需要使用者 (就是你) 輸入發生聲音的指令囉!
步驟:
  1. 你必須要『輸入』一個指令之後,『硬體』才會透過你下達的指令來工作!
  2. 透過『 Shell 』將我們輸入的指令與 Kernel 溝通,kernel 控制硬體工作。

from http://linux.vbird.org/linux_basic/0105computers/os_01.gif

  • 只要能夠操作應用程式的介面都能夠稱為殼程式。
  • 狹義的殼程式指的是指令列方面的軟體,ex: bash 等; 廣義的殼程式則包括圖形介面的軟體。
  • 殼程式可以呼叫其他軟體。
    • ex: man, chmod, chown, vi, fdisk, mkfs 等等指令。
      • 這些指令都是獨立的應用程式,透過殼程式 (就是指令列模式) 來操作這些應用程式,讓這些應用程式呼叫核心來運作所需的工作。
  • shell 可分為多種:
    • 在 Sun 裡頭預設的 C SHell、 商業上常用的 K SHell、 TCSH 等等。
    • 至於 Linux 使用的這一種版本就稱為『 Bourne Again SHell (簡稱 bash) 』。
    • 是 Bourne Shell 的增強版本,也是基準於 GNU 的架構下發展出來的!


* Reference
- 鳥哥的 Linux 私房菜 -- 學習 bash shell

2013年5月12日 星期日

[Graphic] Vector Drawables

Drawable 分為兩類:


Bitmap Drawables
  • Bitmap files, ex:
    • PNG which are rendered as images by the OS.
    • 9-patch bitmaps which enable the OS to anamorphically distort the image to fit a particular region.
Vector Drawables
  • Consist of XML files which contain some basic vector drawing command which the OS renders dynamically at runtime.
  • 缺點:
    • Require more processing power to render,particularly gradients; 
    • The drawing primitives are not as flexible as those in your favorite image manipulation software.


兩種差異是單一的 vector drawable 在不同顯示規格下的縮放表現較好(a single vector drawable will scale much better on a variety of different display types)。


* Reference
- Vector Drawables – part 1

[Android] context for SharedPreferences

  • Does not support use across multiple processes
  • SharedPreferences of specific name is a single instance of this class that all clients share.


Relative post
[Android] Context


* Reference
- SharedPreferences
- getSharedPreferences (String name, int mode)
- android - Application context for SharedPreferences? - Stack Overflow
- android - SharedPreferences application context vs activity context - Stack Overflow

2013年3月26日 星期二

[Android] Directory path for APIs


  • getFilesDir = /data/data/{package name}/files
  • getDatabasePath = /data/data/{package name}/databases/{database name}.db
  • // 私人的檔案,當移除 APP,此路徑下的也都會移除。
  • getExternalFilesDir = /mnt/sdcard/Android/data/{package name}/files/{file}
  • getDownloadCacheDirectory = /cache
  • getDataDirectory = /data
  • // 與應用程式的關聯直接分開的,當移除 APP,此路徑下的也不會被移除。
  • getExternalStorageDirectory = /mnt/sdcard 
  • getExternalStoragePublicDirectory = /mnt/sdcard/pub_test

2013年2月21日 星期四

[Android] Optimised ImageView

在 4.0 前,使用 ImageView.setImageDrawable(),會執行下列動作:
  1. Set the new Drawable (and keep a reference to it).
  2. Request a layout pass.
  3. Invalidate itself to display the new contents.

step 1 and step 3 是必要的
step 2 是定位 drawable 自 ImageView 中的位置,因此只需要在 drawable 大小改變時使用到。

也就是若 AdapterView 中使用的都是大小相同的 drawable,則能省略 step 2,讓滑動的效能獲得改善


在 4.0 中會先比對大小才呼叫 requestLayout()(run the step 2),4.0 後則能使用 reference 中所提供的 OptimisedImageView。


Relative post
- [Android] Avoid Memory leak


* Reference
- Snippet: ImageView Layout Optimisation | senab

2013年1月31日 星期四

[Android] Errors when sign projecct

Failure [INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES]

重新 sign 前 沒有刪掉以下檔案 安裝時便會發生此錯誤。

\META-INF\CERT.RSA
\META-INF\CERT.SF


jarsigner: unable to open jar file: test.apk

沒有找到 test.apk。


jarsigner: unable to sign jar: java.util.zip.ZipException: invalid entry compressed size (expected 483 but got 480 bytes)

要 sign 的檔案已經 sign 過。

2013年1月26日 星期六

[Android] get system values when reboot

Error Message

若是 livewallpaper app

重開機時 取到的 system values 會是 0 (ex: imei, iccid...)



Solution

若已設定 livewallpaper 然後重開機

在開機中 system 便會呼叫 livewallpaper

此時所取到的 system values 便會是 0

可等接收到 BOOT_COMPLETED action 後 再去取 便能取到正確值

或是判定為 SIM_STATE_READY 才將取到的值視為有效值


飛安模式 -> SIM_STATE_UNKNOWN -> 取不到

關閉網路 -> SIM_STATE_READY -> OK


測試中(HTC One V) 轉換飛安模式和一般模式的行為 會使得物件等被清除

仍不確定是否依手機而異 與影響範圍

2013年1月21日 星期一

[Web] object from request parameters

Error Message

ERROR - [Ljava.lang.String; cannot be cast to java.lang.String
java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String


Solution

注意使用的物件 type,Map<String, String[]> or Map<String, String>。


Map<String, String[]> requestParameters = request.getParameterMap();
Map<String, String> parameters = new Map<String, String>();
for(String key: requestParameters.keySet())  {
    parameters.put(key, requestParameters.get(key)[0]);
}

[JPA] EntityNotFoundException

Error Message

javax.persistence.EntityNotFoundException: Unable to find ObjectA with id ObjectB.


Solution

join ObjectA 時,對應不到 ObjectA 時會發生該 exception。

Using @NotFound(action = NotFoundAction.IGNORE)

optional="tru|false"
表示該屬性是否允許為 null,預設為 true。


* Reference
- Annotation Type NotFound
- @Basic(fetch=FetchType,optional=true)
- @NotFound(action=NotFoundAction.IGNORE)

[ibatis] removeFirstPrepend


    

  • true/false
    • 是否移除 iterator 全部後的 statement 的結果的第一個 prepend。
  • iterate
    • 移除每次 iterator 中 statement 的第一個 prepend。


* Reference
- ibatis中的dynamic sql特性
- ibatis教程

2013年1月20日 星期日

[ibatis] object type in

Error Message

com.ibatis.sqlmap.client.SqlMapException: ParameterObject or property was not a Collection, Array or Iterator.

Solution

<iterator property="Object">

property 只能放入 type Collection, Array or Iterator.


[Android] Service notices

  • Service 會被開啟在和 caller 同個 process 中,並且執行在 main thread 中,所以若是要在 service 中執行耗時動作時,必須執行在 background thread 中。
  • 如果想讓 service 執行在另一個 process 中,則可在 AndroidManifest 中宣告 android:process=":process_description"
    • 此 service 有自己的分配空間。
    • a garbage collection, will not affect the user interface of your Activity。
    • 其中的 ":" 是表示此 service 為我們 application 私有的,若是沒有宣告,則表示是全域的而其他人也能存在到。
    • 當 service 執行在另外的 process,則需透過 interprocess communication (IPC) 與其溝通。


Binding to local service
  • If the Service is started in the same process as the Activity
    • The Activity can directly bind to the service a call of the bindService() method. This method has the ServiceConnection parameter.
    • The onServiceConnected() method is called on this object once the Service is available.
    • The Service return on its onBind() method an object of type IBinder which can be used to call to the service.



android.intent.action.BOOT_COMPLETED
  • 若 application 被安裝在 SD card 則接收不到 android.intent.action.BOOT_COMPLETED,因此必須再 register android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE。 (?)
    • If you application is installed on the SD card, then it is not available after the android.intent.action.BOOT_COMPLETED event. Register yourself in this case for the android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE event.
  • 在 Android 3.0,則是至少 user 開啟過 application 一次, application 才能接收到 android.intent.action.BOOT_COMPLETED。



* Reference
- Android Service Tutorial

2013年1月19日 星期六

[Android] Process has died

Error Message

12-28 11:25:03.191: I/ActivityManager(1614): Process com.package.name (pid 17766) has died.



Testing

  • Register BroadcastReceiver in ServiceA will not active when process is died but except register in manifest.
  • If create ServiceA in another process(android:process) and BroadcastReceiver will be create in this process, too.
    • So that even though process of activity that creates service is died service will not died immediately.


2013年1月6日 星期日

[Javascript] iframe(child) and its parent

//--- parent to child

// get child's object
$('#childId').contents().find('childObjectSelector')


//--- child to parent

// call the method defined in parent.
parent.methodInParent();

// get parent's object
$('parentObjectSelector', window.parent.document);