4

我希望将应用程序与 Google Play 服务和 Mortar 集成以检索用户的当前位置。

https://developer.android.com/training/location/retrieve-current.html#CheckServices

这需要在 Google Play 服务不可用的情况下处理 onActivityResult()。

我对如何做到这一点的第一直觉是getFirstScreen()返回Main.java一个空白的加载屏幕。 https://github.com/square/mortar/blob/master/mortar-sample/src/main/java/com/example/mortar/core/Main.java

注入后,在onCreate()执行期间检查 Google Play Services 是否可用。如果是,则调用flow.goTo(<location using screen>),如果不是,则什么也不做,等待onActivityResult()被调用。然后,当onActivityResult()发生火灾时,只需调用flow.goTo(<location using screen>).

以上对我来说似乎有点hacky。(如果您需要澄清,请告诉我)。所以我认为另一个解决方案可能是做一些类似于这个Mortar + Flow 的事情,第三方库连接到活动生命周期并连接onActivityResult()到演示者。这样做的问题是我无法Activity从演示者那里访问,这使得无法调用GooglePlayServicesUtil.getErrorDialog(...),因为它需要Activity.

我认为onActivityResult()是非常重要的。也许它应该是 Mortar 库的一部分?

4

1 回答 1

9

以下是我们通常如何处理 Square Register 中的 startActivityForResult。

public SomePresenter extends Presenter<SomePresenter.Activity> {
  public interface Activity {
    void startActivityForResult(android.content.Intent intent, int requestCode);
  }

  public final void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Make sure it's the expected requestCode and resultCode and do your thing.
    // If it isn't, no-op, someone else will handle it.
  }
}

活动看起来像:

public MyActivity extends Activity implements SomePresenter.Activity {


  @Override protected void onCreate(Bundle bundle) {
    // Do the usual mortar init stuff

    somePresenter.takeView(this);
  }

  @Override protected void onActivityResult(int requestCode, int   resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    somePresenter.onActivityResult(requestCode, resultCode, data);
  }

  @Override protected void onDestroy() {
    somePresenter.dropView(this);
    super.onDestroy();
  }
}

这对错误对话框没有帮助,但这对我来说听起来像是一个单独的问题。似乎您可以在那里使用 Popup / PopupPresenter 对。(我对 Popup 并不感到兴奋,但它可以完成工作,直到我们有更好的想法。)或者也许活动应该继续进行并自行处理?我对 Play Services 不是很熟悉,还没有处理过它们。

于 2014-09-22T19:10:57.390 回答