--- title: 【Android】取得所在(預設)地點的貨幣 date: 2016-03-21 is_modified: false disqus: cynthiahackmd categories: - "程式設計 › 程式語言與架構" tags: - "Android" --- {%hackmd @CynthiaChuang/Github-Page-Theme %} <br> 這篇還是還是匯率相關 XDDD 在實作匯率 App 時,我想讓 App 預先載入使用者最常使用及查詢的貨幣,讓使用者有比較好的 UX,只是到底怎麼定義使用者最常使用及查詢的貨幣,讓我有點傷透腦筋。 <!--more--> ## 使用者最常使用及查詢的貨幣? 一開始,是有想說透過 GPS 取得使用者所在位置,然後取得所在位置的幣別,作為最常使用的貨幣,想說你都在當地應該會用那邊的貨幣吧? 只是如此一來在 OnCreate 要做的是就會很繁重,要取得位置及幣別,還要取得即時匯率,應該會被 ANR(Application Not Responding),而且我覺得匯率換算 App 跟使用者要位置的權限也頗奇怪。 最後是採用了所在(預設)位置,就是一開始在設定手機時,所選擇的國家(位置),然後取出它的幣別,當作我的預設幣別。 另外一方面,最常查詢的貨幣也是頗令人頭痛的東西。 原本是想說,有沒有辦法在得知預設幣別後,去找出這個國家最常兌換的貨幣,例如臺灣最常查詢日幣、美金、人民幣...,只是 servey 不到可用的 API,所以目前只好先寫死固定的貨幣,之後再來修正吧 QAQ ## 取得所在(預設)地點的貨幣 在這邊使用了兩個類別,分別是 [Locale](http://developer.android.com/intl/zh-cn/reference/java/util/Locale.html) 及 [Currency](http://developer.android.com/intl/zh-tw/reference/java/util/Currency.html) > Locale represents a language/country/variant combination. Locales are used to alter the presentation of information such as numbers or dates to suit the conventions in the region they describe. <br> 語言環境(Locale) 代表一個語言/國家/ variant 組合。這三個參數足夠的資訊來描述該地區的語言文化及行為。不過說是這說,我有點搞不清楚最後的 variant 是在描述哪一部分,在 Android 官網中是給了這樣個範例: ```java new Locale("en", "US", "POSIX") ``` <br> 先不管 variant 的部分,目前我想取出的是 **國家** 這項資訊: 1. **取出預設位置** ```java= // 取得預設的語言環境 Locale mDefault = Locale.getDefault() // 取得目前使用語言 String mLanguage = Locale.getDefault().getLanguage(); // 取得目前國家區域 String mCountry = Locale.getDefault().getCountry(); ``` <br> 2. **取得該位置所對應貨幣代碼** 在 [Currency](https://www.tutorialspoint.com/java/util/currency_getinstance.htm) 中提供了一個 function,可藉由傳入 Locale 參數取得到該 Locale 國家所對應的貨幣,一旦得知該貨幣,就可取出得該貨幣的代碼 ```java= // 藉由所取得的預設local,得到所對應的 Currency Currency curr = Currency.getInstance(mDefault); // 取出該 Currency 所對應的ISO 4217 currency code String code = curr.getCurrencyCode() ``` <br><br> > **本文作者**: 辛西亞.Cynthia > **本文連結**: [辛西亞的技能樹](https://cynthiachuang.github.io/Getting-Country-Currency-Based-on-Location-Android) / [hackmd 版本](https://hackmd.io/@CynthiaChuang/Getting-Country-Currency-Based-on-Location-Android) > **版權聲明**: 部落格中所有文章,均採用 [姓名標示-非商業性-相同方式分享 4.0 國際](https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en) (CC BY-NC-SA 4.0) 許可協議。轉載請標明作者、連結與出處!
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up