简体   繁体   English

在Android中使用MVP模式时,应该在哪里调用android服务并调用GoogleAPIClient?

[英]Where should the android service calls and calls to GoogleAPIClient be written while using MVP pattern in android?

I am trying to implement MVP pattern in my android project by referring to this link : https://github.com/jpotts18/android-mvp 我试图通过引用此链接在我的android项目中实现MVP模式https//github.com/jpotts18/android-mvp

I have successfully implemented the view / presenter / interactor classes. 我已成功实现了view / presenter / interactor类。 I am not clear on 我不清楚

  • Where to put the service call code? 在哪里放置service电话代码?

Since i cannot get the context inside the presenter or interactor class, I am not able to put the service call there 由于我无法在演示者或者交互器类中获取上下文,因此我无法将service调用放在那里

  • Where to implement the GoogleApiClient class? 在哪里实施GoogleApiClient类?

Since GoogleApiClient also requires context to run, it also cannot be implemented inside the presenter or interactor without a context 由于GoogleApiClient还需要运行上下文,因此在没有上下文的情况下也无法在演示者或交互器中实现

Using dagger makes it easier to inject the Interactor on your Presenter. 使用匕首可以更轻松地在Presenter上注入Interactor。 Try this link ( https://github.com/spengilley/AndroidMVPService ) 试试这个链接( https://github.com/spengilley/AndroidMVPService

I'm trying to achieve it without dagger. 我试图在没有匕首的情况下实现它。 But this seems violates the MVP architecture. 但这似乎违反了MVP架构。

From the Activity, I created an instance of Interactor. 从Activity中,我创建了一个Interactor实例。 Then create instance of Presenter with the Interactor as one of the parameter. 然后使用Interactor创建Presenter实例作为参数之一。

Activity 活动

public class SomeActivity extends Activity implements SomeView {
   private SomePresenter presenter;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      SomeInteractor interactor = new SomeInteractorImpl(SomeActivity.this);
      presenter = new SomePresenterImpl(interactor,this);
   }

   @Override
   protected void onStart() {
     super.onStart();
     presenter.startServiceFunction();
   }

Presenter 主持人

public interface SomePresenter {
   public void startServiceFunction();
}

Presenter Implementation 演示者实施

public class SomePresenterImpl implements SomePresenter {
   private SomeInteractor interactor;
   private SomeView view;
   public SomePresenterImpl(SomeInteractor interactor,SomeView view){
      this.interactor = interactor;
      this.view = view;
   }
   @Override
   public void startServiceFunction() {
      interactor.startServiceFunction();
   }
}

Interactor 交互器

public interface SomeInteractor {
   public void startServiceFunction();
}

Interactor Implementation 交互式实施

public class SomeInteractorImpl implements SomeInteractor {
   private Context context;

   public SomeInteractorImpl(Context context) {
      this.context = context;
   }

   @Override
   public void startServiceFunction() {
      Intent intent = new Intent(context, SomeService.class);
      context.startService(intent);
   }
}

I am also searching for your first question. 我也在寻找你的第一个问题。 However, I have the answer of the second question. 但是,我有第二个问题的答案。

The answer is Dagger2. 答案是Dagger2。 ( http://google.github.io/dagger/ ) You can easily inject GoogleApiClient object by using Dagger2. http://google.github.io/dagger/ )您可以使用Dagger2轻松注入GoogleApiClient对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM