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

后端 未结 3 629
离开以前
离开以前 2021-02-07 16:09

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:38

    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);
        }
    }   
    

提交回复
热议问题