2015年1月14日 星期三

Android Studio-安裝Google Play services SDK

為什麼要安裝Google Play services SDK?

      安裝此SDK可以使用Google所提供的免費及商用服務,如Google Map(地圖服務)、Place API(商家資訊)、Google Direction API(導航及算兩點距離)等服務,透過Android Studio安裝Google Play services SDK步驟如下:

Step1.打開Android Studio任一個專案->點選「SDK Manager」圖案如下圖紅色框框

Step2.滾輪滑到最下面->將「Google Play services」勾勾打勾如下圖->並將此案裝。(備註:下圖是已經安裝的結果,如果已經安裝可以忽略。)

 

2015年1月12日 星期一

Android Studio-Override快捷鍵

使用Android時,在Coding的地方,按下Crtl+O即可開啟可實作的Override。

上圖在Coding區塊
上圖按下Crtl+O

Android Studio-Google Map 取得SHA1 Key

在使用Android Studio時,不像Eclipse可以在Windows --> Preferences --> Android -->Build 上找到SHA1 Key,所以解決方案如下:

Step1.打開Cmd,位置為C:\Program Files\Java\jdk1.8.0_25\bin底下(jdk1.8.0_25依自己安裝的JDK做修正),並打入keytool.exe -list -v -keystore C:\Users\<自己的使用者名稱>\.android\debug.keystore

Step2.出現上圖,打上「android」,即可找到SHA1的Key了!(Key很重要,所以我用紅色框框遮住了~)

2014年6月23日 星期一

Android SQLite Error: attempt to re-open an already-closed object

attempt to re-open an already-closed object

例外事件是因為你的SQLite資源沒有關閉所造成的問題。

修正方式:
在使用SQLiteDatabase、ContentValues、Cursor三個物件類別時,在最後要記得使用close();方法關閉。
且在使用Cursor如下範例:
SQLiteDatabase db=this.getWritableDatabase(); //在使用Cursor類別時,建議加上這段,因為我把它寫成全域變數,並在一開始Create實作會出錯,所以我都在用它(Cursor)前,都會寫上這段就沒問題了~
Cursor c = db.query(xxxx); //SQL查詢函數用法參考此網站。
實作內容.......
用完後在最後加上
c.close();
db.close();
就可以了~建議順序是這樣喔!!



2014年6月5日 星期四

Android Search

Android Search 實作分兩種
1.Search Dialog
2.Search Widget
差別在於Search Dialog只會顯示在最上方,而Search widget可以放置在任意地方。

Search Widget必須實作 SearchView,如下:
//取得SearchView和設定 searchable configuration
SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchView searchView=(SearchView)findViewById(R.id.searchView1);

searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); //效果是一開始出現輸入的方框,如果沒這行,則是點下icon才出現輸入方框
searchView.setSubmitButtonEnabled(true); //出現submit 按鈕在方框右邊。


Android SQLite truncate table (清空table的方法)

SQLite沒有像SQL有 truncate語法,所以我們可以如下操作:
//清空data
public void cleanHistoryTable(){
SQLiteDatabase db=this.getWritableDatabase();
db.execSQL("DROP TABLE IF EXISTS history"); //刪除history table
this.onCreate(db); //在onCreate去新增
db.close();
}

Android AlertDialog 彙整

AlertDialog.Builder 方法:
AlertDialog.Builder dialog=new AlertDialog.Builder(getActivity()); //實作
1. dialog.setCancelable(false); //此設定是你必須按下dialog裡的按鈕,不能按下如「返回鍵」、其他區塊跳離。
2.dialog.show(); //顯示出dialog。