How to get Executor for main thread on API level < 28

后端 未结 3 1876
梦谈多话
梦谈多话 2021-02-07 16:25

On API level 28(Pie) a new method is introduced in the Context class to get Executor for the main thread getMainExecutor().

How to get this executor on API

3条回答
  •  独厮守ぢ
    2021-02-07 16:48

    You can use code snippet from retrofit https://github.com/square/retrofit/blob/master/retrofit/src/main/java/retrofit2/Platform.java

    public class MainThreadExecutor implements Executor {
        private final Handler handler = new Handler(Looper.getMainLooper());
    
        @Override 
        public void execute(Runnable r) {
            handler.post(r);
        }
    }   
    

提交回复
热议问题