在Android中使用MVP模式时,应该写哪些Android服务调用和调用GoogleAPIClient?

前端之家收集整理的这篇文章主要介绍了在Android中使用MVP模式时,应该写哪些Android服务调用和调用GoogleAPIClient?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图通过参考这个链接https://github.com/jpotts18/android-mvp在我的Android项目中实现MVP模式

我已经成功实现了视图/演示者/交互者类.我不清楚

>哪里可以把服务电话代码

Since i cannot get the context inside the presenter or interactor
class,I am not able to put the service call there

>在哪里实现GoogleApiClient类?

Since GoogleApiClient also requires context to run,it also cannot be
implemented inside the presenter or interactor without a context

解决方法

使用匕首可以更容易地在演示者上注入Interactor.尝试此链接( https://github.com/spengilley/AndroidMVPService)

我试图实现它没有匕首.但这似乎违反了MVP架构.

从Activity开始,我创建了一个Interactor实例.然后使用Interactor创建Presenter的实例作为参数之一.

活动

  1. public class SomeActivity extends Activity implements SomeView {
  2. private SomePresenter presenter;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. SomeInteractor interactor = new SomeInteractorImpl(SomeActivity.this);
  7. presenter = new SomePresenterImpl(interactor,this);
  8. }
  9.  
  10. @Override
  11. protected void onStart() {
  12. super.onStart();
  13. presenter.startServiceFunction();
  14. }

主持人

  1. public interface SomePresenter {
  2. public void startServiceFunction();
  3. }

演示者实现

  1. public class SomePresenterImpl implements SomePresenter {
  2. private SomeInteractor interactor;
  3. private SomeView view;
  4. public SomePresenterImpl(SomeInteractor interactor,SomeView view){
  5. this.interactor = interactor;
  6. this.view = view;
  7. }
  8. @Override
  9. public void startServiceFunction() {
  10. interactor.startServiceFunction();
  11. }
  12. }

交互器

  1. public interface SomeInteractor {
  2. public void startServiceFunction();
  3. }

互动实现

  1. public class SomeInteractorImpl implements SomeInteractor {
  2. private Context context;
  3.  
  4. public SomeInteractorImpl(Context context) {
  5. this.context = context;
  6. }
  7.  
  8. @Override
  9. public void startServiceFunction() {
  10. Intent intent = new Intent(context,SomeService.class);
  11. context.startService(intent);
  12. }
  13. }

猜你在找的Android相关文章