2018年10月31日 星期三

Android Callback、Handler 差別


Callback

因為Android 常使用other thread做資料處理的動作(background deal,not UI thread),所以當訊息處理完,時常需要回覆給UI thread處理完畢(但這時候回覆訊息到UI thread的部分,還是other thread負責,所以不能直接更新UI),所以這時候就有callback機制。

專案簡單說明:

1.MainActivity new 一個DownLoadService 準備要下載東西,但我需要一個回應說DownLoadService下載好了。

2.使用interface 做出一個OnDownLoadListener,給MainActivity實作出來方法。

3.MainActivity使用setOnDownLoadListener,將MainActivity實作出來的OnDownLoadListener,給downLoadService。


4.這時候downLoadService可以下載東西,下載完之後使用onDownLoadListener.onComplete("String")回應給mainActivity實作的方法,就可以達到不同trhead但可以異步訊息傳送。

注:加上Looper.prepare() and Looper.loop()

是因為toast.show要有一個Looper把taost(本身用到UI thread)傳遞給message queue,讓handler抓message(toast)出來處理。
而一個new thread沒有Looper,所以要Looper.prepare(),給他一個new looper,之後讓他loop.loop給他插入toast進入 message queue。

github:https://github.com/TakmingMark/PracticeCallback


Handler

主要是當處理完畢的資料,需要和UI講,講完之後就可以更新資料(Handler處理是在UI同一線程,所以可以更新UI)

github:https://github.com/TakmingMark/PracticeHandler

合起來使用的話,來講一下使用情況。

callback就像一個員工A,他要一直處理照片傳輸的問題。

handler就像一個員工B,他要監控員工A,當員工A把照片傳輸做完時,回傳一個訊息給員工B,然後員工B告訴老闆(UI update)說照片傳輸完畢

github:https://github.com/TakmingMark/CallbackAndHandler


參考文章:
https://blog.csdn.net/ErLiangCode/article/details/52117831
http://andy02172001.blogspot.com/2017/10/androidinterfacecall-back.html
https://blog.csdn.net/ErLiangCode/article/details/52117831

2018年10月8日 星期一

對於git一些常用指令紀錄



git reset —mixed 默認值,回歸某標籤,標籤以後檔案依舊會留存在Working directory,但更改內容不在staged Area裡面,要從新add and commit

git reset —hard  回歸某標籤,標籤以後檔案不會留存在Working directory,直接拋棄某標籤以後的修改。

git reset —soft 回歸某標籤,介於mixedhard間操作,標籤以後檔案依舊會留存在Working directory,更改內容也在staged Area裡面,不用從新add,但要commit


git reset [ae3034d] 指定標籤回歸,檔案依舊會留存在Working directory,但不在staged Area裡面,要從新add and commit

git reset [current~2] 當前往前數兩個回歸,檔案依舊會留存在Working directory,但不在staged Area裡面,要從新add and commit


git commit —amend  不會產生新的commit,會依照之前的commit來添加檔案內容,和可修改commit message內容
more fileName 得知檔案內容

git log —oneline 打印 history of commit ,並一個commit 印出一行資訊

git checkout -b [branchName] 創建一個分支,並轉換過去

git branch [branchName] 創建一個分支,不會轉換過去

git branch -a 看全部的分支

git checkout [branchName] 轉換分支

git branch 得知此分支名稱

git branch -D [branchName]刪除某分支

more [fileName] 印出檔案內容

more .git/config 觀看此專案的資訊

git remote -v 顯示遠端服務器的名稱

git diff [branchName] 用此分支和其他分支比較檔案差別。

git merge [branchName] 結合某分支

git fetch [origin] [branchName]獲取遠端服務器某分支的版本 

git merge FETCH_HEAD 本地使用fetch之後,用這個指令才能同步

git pull  直接一次性解決fetch and merge的問題,但通常不推薦,通常一步一步來,先fetch然後diff來了解是否merge

origin 代表服務器的名稱

git [instruction] -h 查看某指令使用方法

git push origin —delete [branchName]刪除遠端的分支

git branch -m [branchName] 更改分支名稱

git tag -l 目前標籤名稱列

git fetch -t 抓取遠端的標籤

git push —tag 本地標籤傳入遠端

git tag -d [tagName] 刪除某本地標籤

git push origin :refs/tags/[tagName] 刪除遠端標籤

git remote add [remoteName] [remoteLink] 增加一個遠端

產生push遠端不需要一直輸入帳號密碼

step1:ssh-keygen 生成public and private key in /root/.ssh/

step2:GitHub->settings->SSH and GPG keys 把本地電腦id_rsa_pub複製貼過去

step3:vim .git/config 如果使用後還是需要帳號密碼,代表我們走的是https,要更改成ssh(github上複製過來)

額外補充:ssh-copy-id -i /root/.ssh/id_rsa.pub [account]@[IP] 如果對方是linux電腦,可以使用此方法傳遞public key


平行別人專案步驟

step1:git remote add upstream GitHub@IP

step2:git fetch upstream

step3:git merge upstream/master

step4:git push origin master