Android?jar庫源碼Bolts原理解析
Bolts:
作用:
用于鏈?zhǔn)綀?zhí)行跨線程代碼,且傳遞數(shù)據(jù)栗子:
Task.call(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return true;
}
}, Task.UI_THREAD_EXECUTOR);
Task.callInBackground(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return false;
}
});
Task.callInBackground(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return true;
}
}).onSuccess(new Continuation<Boolean, Object>() {
@Override
public Object then(Task<Boolean> task) throws Exception {
if (task.getResult()) {
return null;
} else {
return new Object();
}
}
}, Task.BACKGROUND_EXECUTOR).continueWith(new Continuation<Object, Object>() {
@Override
public Object then(Task<Object> task) throws Exception {
if (task.getResult() == null) {
Toast.makeText(getBaseContext(), "null", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getBaseContext(), "not null", Toast.LENGTH_SHORT).show();
}
return null;
}
}, Task.UI_THREAD_EXECUTOR); 源碼解讀:
在內(nèi)部通過維護(hù)多中 ExecutorService 對(duì)象,并且通過串聯(lián)的方式進(jìn)行調(diào)用。
并且通過維護(hù)內(nèi)部變量在,在指定流程處,就是特定的,值,值通過Task的對(duì)象getResult拿到。
UIThread
/**
* An {@link java.util.concurrent.Executor} that runs tasks on the UI thread.
*/
private static class UIThreadExecutor implements Executor {
@Override
public void execute(Runnable command) {
new Handler(Looper.getMainLooper()).post(command);
}
}BackgroundThread
private BoltsExecutors() {
background = !isAndroidRuntime()
? java.util.concurrent.Executors.newCachedThreadPool()
: AndroidExecutors.newCachedThreadPool();
scheduled = Executors.newSingleThreadScheduledExecutor();
immediate = new ImmediateExecutor();
} 源碼:https://github.com/BoltsFramework/Bolts-Android
引入:
implementation 'com.parse.bolts:bolts-android:1.2.0'
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android TextView字幕效果實(shí)例
- Android自定義attr的各種坑
- Android 日歷控件庫,可左右滑動(dòng),顯示公歷,農(nóng)歷,節(jié)假日等功能
- Android開發(fā)之Picasso通過URL獲取用戶頭像的圓形顯示
- Android實(shí)現(xiàn)精確到天時(shí)分秒的搶購倒計(jì)時(shí)
- Android 系統(tǒng)實(shí)現(xiàn)多種開機(jī)動(dòng)畫和logo切換功能
- Android使用手勢(shì)監(jiān)聽器GestureDetector遇到的不響應(yīng)問題
- Android?狀態(tài)管理之Lifecycle淺析
相關(guān)文章
Android實(shí)現(xiàn)銀行卡、手機(jī)號(hào)帶空格格式
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)銀行卡、手機(jī)號(hào)帶空格的格式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12
Android編程實(shí)現(xiàn)的EditText彈出打開和關(guān)閉工具類
這篇文章主要介紹了Android編程實(shí)現(xiàn)的EditText彈出打開和關(guān)閉工具類,涉及Android輸入框EditText彈出打開和關(guān)閉功能簡(jiǎn)單實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-02-02
android使用TextView實(shí)現(xiàn)跑馬燈效果
這篇文章主要為大家詳細(xì)介紹了android使用TextView實(shí)現(xiàn)跑馬燈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
android實(shí)現(xiàn)藍(lán)牙文件發(fā)送的實(shí)例代碼,支持多種機(jī)型
這篇文章主要介紹了android實(shí)現(xiàn)藍(lán)牙文件發(fā)送的實(shí)例代碼,有需要的朋友可以參考一下2014-01-01
Android 使用Vibrator服務(wù)實(shí)現(xiàn)點(diǎn)擊按鈕帶有震動(dòng)效果
這篇文章主要介紹了Android 使用Vibrator服務(wù)實(shí)現(xiàn)點(diǎn)擊按鈕帶有震動(dòng)效果,,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)火鍋工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
Android自定義有限制區(qū)域圖例角度自識(shí)別涂鴉工具類
這篇文章主要為大家介紹了Android自定義有限制區(qū)域圖例角度自識(shí)別涂鴉工具類,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android studio 實(shí)現(xiàn)手機(jī)掃描二維碼功能
這篇文章主要介紹了Android studio 實(shí)現(xiàn)手機(jī)掃描二維碼功能,需要的朋友可以參考下2019-10-10
Flutter 包管理器和資源管理使用學(xué)習(xí)
這篇文章主要為大家介紹了Flutter 包管理器和資源管理使用學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12

