我试图通过参考这个链接:
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 theservice
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的实例作为参数之一.
活动
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(); }
主持人
public interface SomePresenter { public void startServiceFunction(); }
演示者实现
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(); } }
交互器
public interface SomeInteractor { public void startServiceFunction(); }
互动实现
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); } }