天天看點

精通Android4學習筆記-設定開發環境

  之前看過一些資料,做過幾個app,主要是通過視訊來學習的,效果還不錯。

  正好公司有個學習小組,我有了開發apk的任務,再從頭學習一遍,這次有了學習小組的規定教材-精通Android4.

  溫故而知新

1.avd的設定

snapshot可以提高AVD啟動的速度 ;

設定target的api高版本,可以相容更多基于低版本sdk開發的apk;

但是高版本的缺陷是記憶體等等資源占用比較多,因為一般來說螢幕比較大。

2.android應用程式架構的基本元件

view

activity

intent

content provider

service

fragment

AndroidManifest.xml

3.avd的進一步知識

可以在指令行建立avd

建立的avd,預設情況一般放在使用者目錄的.android\AVD檔案夾裡面;

也可以指定avd的檔案夾;

#得到target的清單

android list target

id: 2 or "Google Inc.:Google APIs:3"

     Name: Google APIs

     Type: Add-On

     Vendor: Google Inc.

     Revision: 3

     Description: Android + Google APIs

     Based on Android 1.5 (API level 3)

     Libraries:

      * com.google.android.maps (maps.jar)

          API for Google Maps

     Skins: QVGA-P, HVGA-L, HVGA (default), QVGA-L, HVGA-P

     ABIs : armeabi

#建立一個avd

android create avd -n CupcakeMaps -t 3 -c 16M -p c:\avd\CupcakeMaps\

就算這樣做,在%home%/.android/avd下面還有一個檔案CupcakeMaps.ini,裡面存放了該avd的資訊

avd.ini.encoding=ISO-8859-1

target=android-4

path=C:\Documents and Settings\jian.zhang\.android\avd\CupcakeMaps.avd

path.rel=avd\CupcakeMaps.avd

4.android 應用程式的源碼結構

AndroidManifest.xml

The Android application descriptor file. This file defines the

activities, content providers, services, and intent receivers of

the application. You can also use this file to declaratively

define permissions required by the application, as well as

grant specific permissions to other applications using the

services of the application. Moreover, the file can contain

instrumentation detail that you can use to test the application

or another application.

src

assets

res

drawable

animator

layout

menu

values

xml

raw

5.了解應用程式生命周期

   由系統根據使用者需求及可用資源等進行嚴格管理。

  盡管系統是最終的決定者,但它會遵從一些既定的和邏輯上的原則。

精通Android4學習筆記-設定開發環境

Listing 2–1. Life-Cycle Methods of an Activity

protected void onCreate(Bundle savedInstanceState);

protected void onStart();

protected void onRestart();

protected void onResume();

protected void onPause();

protected void onStop();

protected void onDestroy();

 記住:如果實作了這些生命周期方法,一定要確定也調用了超類的相應方法。

最常用的方法是onCreate,onResume和onPause

onPause()裡面,你可能希望儲存關鍵的資料,這個方法是系統結束應用程式前調用的最後一個安全的方法。無法保證onStop和onDestroy會被調用,是以不要依賴這些方法來實作關鍵邏輯。

Activity中可以擷取應用程式上下文。

6.簡單調試

LogCat

顯示android.util.Log,異常,System.out.println等發出的日志資訊。

盡量使用Log類,因為這個類的方法可以區分資訊的級别,以便于後面對資訊的過濾處理。

這些日志資訊的級别有info,warning,error等等,可以調用Log的不同方法來實作資訊的分級。

不要在準備生産部署的應用上留下冗長的調用,因為日志記錄會消耗記憶體和占用CPU資源。