Android的細節知識彙總系列(一)

BaiHongHua發表於2019-02-25
Intent可轉遞的資料型別
  • 8 種資料型別(byte、char、short、int、long、float、double、boolean)及其陣列;
  • String(String實現了 Serializable)/CharSequence 例項型別的資料及其陣列;
  • 實現了 Parcelable 的物件及其陣列;
  • 實現了 Serializable 的物件及其陣列;
  • File在Java裡也是類,在Android裡也實現了Serializable介面;

Serializable :將 Java 物件序列化為二進位制檔案的 Java 序列化技術是 Java系列技術中一個較為重要的技術點,在大部分情況下,開發人員只需要瞭解被序列化的類需要實現 Serializable 介面,使用ObjectInputStream 和 ObjectOutputStream 進行物件的讀寫; 引入了CharSequence介面,實現了這個介面的類有:CharBuffer、String、StringBuffer、StringBuilder這個四個類;

Parcelable和Serializable 倆者異同
  • Serializable在序列化的時候會產生大量的臨時變數,從而引起頻繁的GC;
  • 在使用記憶體的時候,Parcelable比Serializable效能高,所以推薦使用Parcelable。
  • Parcelable不能使用在要將資料儲存在磁碟上的情況,因為Parcelable不能很好的保證資料的持續性在外界有變化的情況下。儘管Serializable效率低點,但此時還是建議使用Serializable 。
Android dvm
  • Android 執行時由兩部分組成: Android 核心庫集和 Dalvik 虛擬機器。其中核心庫集提供了 Java 語言核心庫所能使用的絕大部分功能,而虛擬機器則負責執行 Android 應用程式。
  • 每個 Android 應用程式都執行在單獨的 Dalvik 虛擬機器內(即每個 Android 應用程式對用一條 Dalvik 程式), Dalvik 專門針對同時高效地執行多個虛擬機器進行優化,因此 Android 系統以方便的實現對應用程式進行隔離。
  • DVM指dalivk的虛擬機器,每一個Andriod應用程式都在它自己的程式中執行,都擁有一個獨立的Dalivk虛擬機器例項,而每一個DVM都是在Linux中的一個程式,所以說可以認為是同一個概念
在同一執行緒中android.Handler和android.MessaegQueue的數量對應關係
  • Handler 必須在 Looper.prepare() 之後才能建立使用,其中就生成一個 MessageQueue;
  • Looper 與當前執行緒關聯,並且管理著一個 MessageQueue;
  • Message 是實現 Parcelable 介面的類;
  • 以一個執行緒為基準,他們的數量級關係是: Handler(N) : Looper(1) : MessageQueue(1) : Thread(1);
Activty啟動模式
  • standard模式。哪裡需要呼叫我我就去哪裡,可以多次例項化,可以幾個相同的Activity重疊;
  • singleTop模式。可以多次例項化,但是不可以多個相同的Activity重疊,當堆疊的頂部為相同的Activity時,會呼叫onNewIntent函式;
  • singleTask模式。同一個應用中呼叫該Activity時,如果該Activity沒有被例項化,會在本應用程式的Task內例項 化,如果已經例項化,會將Task中其上的Activity銷燬後,呼叫onNewIntent;其它應用程式呼叫該Activity時,如果該 Activity沒有被例項化,會建立新的Task並例項化後入棧,如果已經例項化,會銷燬其上的Activity,並呼叫onNewIntent。一句 話,singleTask就是“獨立門戶”,在自己的Task裡,並且啟動時不允許其他Activity凌駕於自己之上;
  • singleInstance模式。載入該Activity時如果沒有例項化,他會建立新的Task後,例項化入棧,如果已經存在,直接呼叫 onNewIntent,該Activity的Task中不允許啟動其它的Activity,任何從該Activity啟動的其他Activity都將被 放到其他task中,先檢查是否有本應用的task,沒有的話就建立;
Android獲取螢幕寬度的4種方法
  • 方法一
WindowManager wm = (WindowManager) this
				.getSystemService(Context.WINDOW_SERVICE);
		int width = wm.getDefaultDisplay().getWidth();
		int height = wm.getDefaultDisplay().getHeight();
複製程式碼
  • 方法二
WindowManager wm1 = this.getWindowManager();
		int width1 = wm1.getDefaultDisplay().getWidth();
		int height1 = wm1.getDefaultDisplay().getHeight();
複製程式碼

方法一與方法二獲取螢幕寬度的方法類似,只是獲取WindowManager 物件時的途徑不同。

  • 方法三
WindowManager manager = this.getWindowManager();
		DisplayMetrics outMetrics = new DisplayMetrics();
		manager.getDefaultDisplay().getMetrics(outMetrics);
		int width = outMetrics.widthPixels;
		int height = outMetrics.heightPixels;
複製程式碼
  • 方法四
Resources resources = this.getResources();
		DisplayMetrics dm = resources.getDisplayMetrics();
		float density = dm.density;
		int width = dm.widthPixels;
		int height = dm.heightPixels;
複製程式碼
Broadcast Receiver專注於接收廣播通知訊息
  • 由於BroadcastReceiver本質上是一種監聽器,所以建立BroadcastReceiver的方法也非常簡單,只需要建立一個BroadcastReceiver的子類然後重寫onReceive (Context context, Intentintent)方法即可;
  • BroadcastReceiver的生命週期,從物件呼叫它開始,到onReceiver方法執行完成之後結束。另外,每次廣播被接收後會重新建立BroadcastReceiver物件,並在onReceiver方法中執行完就銷燬,如果BroadcastReceiver的onReceiver方法中不能在10秒內執行完成,Android會出現ANR異常。所以不要在BroadcastReceiver的onReceiver方法中執行耗時的操作;
Android有5種佈局
  • FrameLayout
  • LinearLayout
  • TableLayout
  • RelativeLayou
  • AbsoluteLayout
用於對單選框進行分組的方法
  • 單選框操作:單選框在Android裡面隨處可見,它是由兩部分組成的,一部分是RadioGroup,一部分是RadioButton。一個RadioGroup裡面是有多個RadioButton。每個RadioButton就是一個單選項,而控制的時候是控制RadioGroup;
  • 核取方塊(checkBox):核取方塊就沒有單選框那樣有組的概念了,所以核取方塊的操作和單選框比起來就會比較複雜一點點,因為你要對每個核取方塊都進行一個事件響應;
程式的種類
  • 前臺程式
  • 可見程式
  • 服務程式
  • 後臺程式
  • 空程式

Android的細節知識彙總系列(一)

編寫Aidl檔案時,需要注意下面幾點
  • 介面名和aidl檔名相同;
  • 介面和方法前不用加訪問許可權修飾符public,private,protected等,也不能用final,static;
  • Aidl預設支援的型別包話java基本型別(int、long、boolean等)和(String、List、Map、 CharSequence),使用這些型別時不需要import宣告。對於List和Map中的元素型別必須是Aidl支援的型別。如果使用自定義型別作 為引數或返回值,自定義型別必須實現Parcelable介面;
  • 自定義型別和AIDL生成的其它介面型別在aidl描述檔案中,應該顯式import,即便在該類和定義的包在同一個包中;
  • 在aidl檔案中所有非Java基本型別引數必須加上in、out、inout標記,以指明引數是輸入引數、輸出引數還是輸入輸出引數;
  • Java原始型別預設的標記為in,不能為其它標記;
android中使用SQLiteOpenHelper輔助類
  • Android使用 getWritableDatabase() 和getReadableDatabase()方法都可以獲取一個用於運算元據庫的SQLiteDatabase例項。(getReadableDatabase()方法中會呼叫getWritableDatabase()方法)
  • getReadableDatabase()並不是以只讀方式開啟資料庫,而是先執行getWritableDatabase(),失敗的情況下才以只讀方式開啟資料庫.。
  • 但getWritableDatabase()方法以讀寫方式開啟資料庫,一旦資料庫的磁碟空間滿了,資料庫就只能讀而不能寫;
  • getWritableDatabase()開啟資料庫就會出錯。getReadableDatabase()方法先以讀寫方式開啟資料庫, 倘若使用如果資料庫的磁碟空間滿了,就會開啟失敗,當開啟失敗後會繼續嘗試以只讀方式開啟資料庫;
Android系統資源池
  • Message提供了訊息池,有靜態方法Obtain從訊息池中取物件;
  • Thread預設不提供資源池,除非使用執行緒池ThreadPool管理;
  • AsynTask是執行緒池改造的,池裡 預設提供(核數+1)個執行緒進行併發操作,最大支援(核數 * 2 + 1)個執行緒,超過後會丟棄其他任務;
  • Looper,每個Looper建立時建立一個訊息佇列和執行緒物件,也不是資源池;
IntentService與Service的關係
  • IntentService是繼承Service的,那麼它包含了Service的全部特性,當然也包含service的生命週期;
  • IntentService在執行onCreate操作的時候,內部開了一個執行緒,去你執行你的耗時操作;
When to use?
The service can be used in tasks with no UI,but should not be too long.If you need to perform long tasks,you must use threads within Service.
The intentService can be used in long tasks with no communication to Main thread.
If communication is required,can use MainThread Handler to broadcast intents.

How to trigger?
Service is triggered bu calling startService();
IntentService is triggered when using an Intent,is spawns a new worker thread and the method onHandleIntent() is called on this thread.

Triggered from
The service and intent service may be triggered from any thread,activity to other application component.

Runs on
The service runs in background but it runs on the main thread of the application.
The intent service runs on a separate worker thread

Limitations/Drawbacks
The service may may block the main thread of the application
The intent service can not run tasks in parallel.Hence all consecutive intents will go into the message queue for the worker thread and will execute sequentially.

When to stop

If you implement a Service,it is yout responsibility to stop the service when its work is done,by calling stopSelf() or stopService().

The intentService will stop the service after all start requests have been handled,so you never have to call stopSelf(); 
複製程式碼
SimpleAdapter作為 ListView的介面卡
  • 使用SimpleAdapter作為介面卡時,支援三種型別的 View,而且是按照如下順序進行匹配:
    • 繼承Checkable介面;
    • TextView;
    • ImageView;
Service 生命週期

Android的細節知識彙總系列(一)

Fragment 以及 Activity 生命週期

Android的細節知識彙總系列(一)

相關文章