發表文章

Java Singleton Pattern

在Java等OOP (Object Orientation Programming) Language 中,在使用Object時,必須使用new的語法來創建一個class(類別)的instance(實例),才能對其操作。 而在某些情境下,我們不希望在Runtime中使用太多來自同樣的class的instance以致資源的浪費或是爭奪而導致錯誤發生。 另外,很重要的一點,這也是確保 thread safe 的一種實作。 private static final Object mLock = new Object(); private static volatile MyClass instance = null; public static MyClass getInstance() {     if (instance == null) {         synchronized(mLock) {             if (instance == null) {                 instance = new MyClass();             }         }     }     return instance; }

Android 語音輸入(Speech To Text)

此功能使用Android提供的 RecognizerIntent  class來實現 public void promptSpeechInput() {     Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);     i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);     i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());     i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say Something!");     try {         startActivityForResult(i, 100);     } catch (ActivityNotFoundException a) {         Toast.makeText(MainActivity.this, "您的裝置不支援語音輸入", Toast.LENGTH_LONG).show();     } } public void onActivityResult (int request_code, int result_code, Intent i) {     super.onActivityResult(request_code, result_code, i);     switch (request_code) {         case 100:             if (i != null) {          ...

Android

強制螢幕直立不旋轉( 我只是不想處理旋轉更新的事 ): 在Manifest.xml中的activity加入 android:screenOrientation="portrait" 屬性即可,如下所示 <activity     android:name=".TimeTable"     android:screenOrientation="portrait" > </activity>

Android JSONArray之排序

當我們需要取得JSONArray中某個key的value,且希望得到的value是經過排序的,我們可以使用 Collections 中的sort()方法來對 整個JSONArray 進行排序,其關鍵作法要將原先的JSONArry用ArrayList存起來,再做排序;如此一來,當你想根據某個key的資料順序取得其他key的資料時就能方便許多,其作法如下: public static JSONArray sortJsonArray(JSONArray array) { ArrayList<JSONObject> jsons = new ArrayList<>(); for (int i = 0; i < array.length(); i++) { try { jsons.add(array.getJSONObject(i)); } catch (JSONException e) { e.printStackTrace(); } } Collections.sort(jsons, new Comparator<JSONObject>() { @Override public int compare(JSONObject t1, JSONObject t2) { String lid = ""; String rid = ""; try { lid = t1.getString("yourKEYname"); rid = t2.getString("yourKEYname"); } catch (JSONException e) { e.printStackTrace(); } return lid.compareTo(rid); }...

Android JSON Parse: JSONObject內又包了一個JSONObject

在Android中,當我們所需要的資料欄位位於JSON格式物件中的物件時,該怎麼取得我們要的欄位資料呢? 這邊使用 公共運輸整合資訊流通服務平台 提供的API所取得的台鐵站別片段JSON資料來做為範例: [   {     "StationID": "1001",     "StationName": {       "Zh_tw": "基隆" ,       "En": "Keelung"     },     "StationPosition": {       "PositionLat": 25.132483,       "PositionLon": 121.739134     },     "StationAddress": "基隆市仁愛區文昌里港西街5號",     "StationPhone": "02-24263743",     "OperatorID": "TRA",     "StationClass": 1   },   {     "StationID": "1002",     "StationName": {       "Zh_tw": "八堵" ,       "En": "Badu"     },     "StationPosition": {       "PositionLat": 25.108392,       "PositionLon": 121.729049     },     "StationAddress": "基隆市暖暖區八南里八堵路142號",     "Sta...