Sean's Note

2014年5月10日 星期六

Launch Mode for Activity

Activity 的 launch mode 可以透過 android:launchMode 來設定,總共有四種 launch mode:
  1. standard
    這是 default 的 launch mode,Activity 每次都會建立一個新的實體。
    EX1: A -> B(standard),在 B 啟動 B,A -> B -> B。
    EX2: A(standard) -> B,返回上層,Activity A 會被重新建立。
  2. singleTop
    如果當下的 Activity 在 stack 的最上面,則不會建立新的實體,反之則會。
    EX1: A(singleTop) -> B -> C,在 C 啟動 A,A -> B -> C -> A。
    EX2: A(singleTop) -> B,返回上層,Activity A 不會被重新建立。
  3. singleTask
    不會建立新的實體,而且總是在 stack 中的最前頭(下層)。
    (Google 說總是在 stack 中的最前頭(下層) 也不全然正確)
    EX1: A -> B(singleTask) -> C -> D ,在 D 啟動 B,A -> B,C 和 D 會被移除。
  4. singleInstance
    不會建立新的實體,而且是 stack 中唯一的 Activity。
    EX1: A -> B -> C ,在 C 啟動 D(singleInstance),[A -> B -> C] -> [D]。

2014年5月1日 星期四

Android 4.4 behavior changes for read/write external storage

在 Android 4.4 之後,應用程式不再可以任意的寫入 SD 卡:
我寫了一個間單的應用程式 (com.example.test) 跑在 HP 平板(Android 4.4) 上做測試。
呼叫新的 API getExternalFilesDirs(null) 去要在 SD 卡上的私有儲存資料夾,
得到了路徑: /storage/sdcard1/Android/data/com.example.test/files
在這個資料夾下做了一些操作,發現有以下的行為:
  1. 我的 test app 可以對該資料夾寫入跟讀取。 
  2. 該資料夾下的檔案可以被電腦端或其它應用程式如 “ES File Explorer” 瀏覽到。
  3. “ES File Explorer” 不能移除該資料夾下的檔案。 (無法被其它應用程式修改)
  4. 電腦端可以移除該資料夾下的檔案。 (可以被電腦端修改)
  5. 當移除我的應用程式後,該資料夾也會被移除。
Read More: https://developer.android.com/about/versions/android-4.4.html#Behaviors

2014年4月29日 星期二

How to get place and wheather information by latitude and longitude

How to get place information?


我們可以用 Facebook API 和 Foursquare API 來取得附近地點的資訊。

這裡主要介紹怎麼使用 Foursquare API,Foursquare 是一間提供社群服務的網站公司,

其所提供的 API目前是屬於免費的。

部分的版權說明:

Access to the API is currently provided for free, but Foursquare reserves the right to charge for access to the API in the future, at its sole discretion. If we do charge a fee for using the API or any feature thereof, you do not have any obligation to continue to use the API or the applicable feature.”

詳細的版權說明可參考: https://foursquare.com/legal/api/platformpolicy

使用步驟如下:
  1. 先到 Foursquare API 註冊一個帳號。
  2. 登入後,點選 My Apps -> CREATE A NEW APP,建立並填寫 app 的相關資訊。
  3. 建立完成後,會得到一組 Client IDClient Secret
  4. 接著我們只要送出一個 HTTPS request:
    https://api.foursquare.com/v2/venues/search?client_id=CLIENT_ID&client_secret=CLIENT_SECRET&v=20140430&ll=24.983163,121.536084
  5. 傳回的 response 就是附近地點啦!
    以上面的 request 為例,第一個回傳的地點就是 “XXX 燒臘小館”,
    回傳的資訊還包含了該地點的 phone, address, latitude, longitude, distance(36m), category name(Asian Restaurant) 等。
Note: Foursquare API 在中國可使用。

How to get weather information?


我們可以用 Weather Underground 所提供的 Weather API 來取得天氣資訊。

Weather Underground 是一間提供全球氣象資訊的網站公司,

其所提供的 API 根據流量來決定費用。

部分的版權說明:

“(a.)KEY; MONITORING. There is a daily limit and minute rate limit to the number of requests that can be made to the API for free. You may exceed this threshold only if you are or become a fee paying subscriber. WUL will monitor your daily usage of the API to determine if you have exceeded the free-use threshold by using an application programming interface key (the “Key”). Refer to the “Key Settings” page for product pricing, package features, and rate limit levels.”

詳細的版權說明可參考: http://www.wunderground.com/weather/api/d/terms.html

使用步驟如下:
  1. 先到 Weather API 註冊一個帳號。
  2. 登入後,點選 Key Settings,先選擇服務方案,再填寫 app 的相關資訊。
  3. 建立完成後,會得到一組 Key ID
  4. 接著我們只要送出一個 HTTP request:
    http://api.wunderground.com/api/69f520a20f42ecd9/conditions/settings/lang:TW/q/24.983163,121.536084.json
    (69f520a20f42ecd9 為官網提供的測試 ID 可直接使用)
  5. 傳回的 response 就是詳細的天氣資訊啦!
    以上面的 request 為例,會回傳微雨(weather)、21.4(temp_c)、東北偏東(wind_dir)、
    85%(relative_humidity) 等資訊。


2014年4月24日 星期四

Capturing HTTP traffic with Fiddler

在使用 RESTful API 諸如 Facebook API、Google API 或 Foursquare API
等等開發程式時,有時候會想要攔截 HTTP request 和 response 的值來
除錯,而這時候就可以使用 Fidder 來查看。
需要設定的步驟如下:
  1. 開起手機端的 Wifi 與 PC 端連上同樣的網域。
  2. 將手機端 Wifi 的 "Proxy 主機名稱" 設成 PC 端的 IP;port 設 8888。
  3. 如果要攔截 HTTPS request,需要在手機上安裝憑證。安裝的方法,
    只要開啟手機上任意的瀏覽器,在網址列輸入 PC 端的 IP 位置,如圖1
    所示,點選 FiddlerRoot certificate 下載憑證後,即可攔截 HTTPS request。
         
圖1. 手機上的 Chrome

2014年4月16日 星期三

[VS] 相對路徑

在使用任何函式用到相對路徑時,會有兩種情況:
  1. 直接跑 build 好的執行檔,這個路徑是相對於該執行檔的。
  2. 透過 VS 去執行時,這個路徑是相對於 $(ProjectDir) 的。想要相對於該執行檔,
    可以到專案的 Properties -> Configuration Properties -> Debugging -> Working Directory 設定執行檔的路徑。

[VS2010] MSVCRT.lib(crtexe.obj) : error LNK2001: unresolved external symbol _main

MSVCRT.lib(crtexe.obj) : error LNK2001: unresolved external symbol _main

如果 VC 遇到這種錯誤,大概你程式裡寫的 Win32 Application,

但建立專案的時候選成 Win32 Console Application。

此時,只要將專案的 Properties -> Configuration Properties -> Linker -> System -> SubSystem

改為 Windows (/SUBSYSTEM:WINDOWS) 即可。

2014年4月15日 星期二

[VS] VC++ Directories setting path

[VS2005]

Tools->Options->VC++ Directories

Path: %LocalAppData%\Microsoft\VisualStudio\8.0\VCComponents.dat

[VS2010]

Tools->Options->VC++ Directories (Deprecated)

如果第一次安裝有勾選載入舊版的設定,該設定會寫在 Path1

Path1: %LocalAppData%\Microsoft\MSBuild\v4.0\Microsoft.Cpp.Win32.user.props

Path2: C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Platforms\Win32\PlatformToolsets\v100\Microsoft.Cpp.Win32.v100.props