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'
以上就是本文的全部內(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使用手勢監(jiān)聽器GestureDetector遇到的不響應(yīng)問題
- Android?狀態(tài)管理之Lifecycle淺析
相關(guān)文章
android截圖事件監(jiān)聽的原理與實(shí)現(xiàn)
本篇文章主要介紹了android截圖事件監(jiān)聽的原理與實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07ListView滑動(dòng)隱藏顯示ToolBar的實(shí)例
下面小編就為大家分享一篇ListView滑動(dòng)隱藏顯示ToolBar的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01Android ListView實(shí)現(xiàn)上拉加載更多和下拉刷新功能
這篇文章主要為大家詳細(xì)介紹了Android ListView實(shí)現(xiàn)上拉加載更多和下拉刷新功能,介紹了ListView刷新原理及實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下2016-05-05Android?WebView的使用與后退鍵處理詳細(xì)討論
在android開發(fā)中我們有時(shí)候根據(jù)項(xiàng)目的需求多少會(huì)加載一些webview,加載webview,我們有時(shí)候會(huì)根據(jù)UI來自定義返回鍵,下面這篇文章主要給大家介紹了關(guān)于Android?WebView的使用與后退鍵處理的相關(guān)資料,需要的朋友可以參考下2024-04-04Flutter學(xué)習(xí)之矢量圖SVG的區(qū)域填色示例詳解
這篇文章主要為大家介紹了Flutter學(xué)習(xí)之矢量圖SVG的區(qū)域填色示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04