MainHandler.java 685 B

123456789101112131415161718192021222324252627282930313233
  1. package com.ytpm.hydtw.utils;
  2. import android.os.Handler;
  3. import android.os.Looper;
  4. public class MainHandler {
  5. private static final Looper mainLooper = Looper.getMainLooper();
  6. private static Handler mainHandler;
  7. private static Handler getMain() {
  8. if (mainHandler == null) {
  9. mainHandler = new Handler(mainLooper);
  10. }
  11. return mainHandler;
  12. }
  13. public static boolean postIfNotMain(Runnable r) {
  14. if (r == null) {
  15. return false;
  16. }
  17. if (Thread.currentThread() == mainLooper.getThread()) {
  18. r.run();
  19. return true;
  20. } else {
  21. return post(r);
  22. }
  23. }
  24. public static boolean post(Runnable r) {
  25. return getMain().post(r);
  26. }
  27. }