SORU
15 Mart 2013, Cuma


Nasıl Otto olay otobüs ile etkinliğe hizmetinden olay göndermek için?

Basit BusProvider.getInstance().post() istisna main threadgetirin. Nasıl Otto olay otobüs ile etkinliğe hizmetinden olay göndermek için?

CEVAP
15 Mart 2013, Cuma


Herhangi bir iş parçacığı (ana veya arka plan) post ve ana iş parçacığı almak için, gibi bir şey deneyin

public class MainThreadBus extends Bus {
  private final Handler mHandler = new Handler(Looper.getMainLooper());

  @Override
  public void post(final Object event) {
    if (Looper.myLooper() == Looper.getMainLooper()) {
      super.post(event);
    } else {
      mHandler.post(new Runnable() {
        @Override
        public void run() {
          MainThreadBus.super.post(event);
        }
      });
    }
  }

Not: kredi Jake Wharton gider ve "" https://github.com/square/otto/issues/38 genel yaklaşım. pommedeterresaute Ben sadece kapsayıcı bir sınıf yerine bir alt sınıf ile gerçekleştirdi.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • CaptainDisillusion

    CaptainDisil

    18 EYLÜL 2007
  • Random Act of Kindness Video Contest

    Random Act o

    18 EYLÜL 2012
  • Drakinen

    Drakinen

    1 EYLÜL 2008