在android中使用MVP模式时,android服务调用和对GoogleAPIClient的调用应该写在哪里?
Where should the android service calls and calls to GoogleAPIClient be written while using MVP pattern in android?
我正在尝试通过参考此 link 在我的 android 项目中实施 MVP 模式:https://github.com/jpotts18/android-mvp
我已经成功实现了 view / presenter / interactor classes。我不清楚
service
调用代码放在哪里?
Since i cannot get the context inside the presenter or interactor
class, I am not able to put the service
call there
- 在哪里实施
GoogleApiClient
class?
Since GoogleApiClient
also requires context to run, it also cannot be
implemented inside the presenter or interactor without a context
我也在搜索你的第一个问题。但是,我有第二个问题的答案。
答案是Dagger2。 (http://google.github.io/dagger/)您可以使用 Dagger2 轻松注入 GoogleApiClient 对象。
使用 dagger 可以更轻松地将 Interactor 注入 Presenter。试试这个 link (https://github.com/spengilley/AndroidMVPService)
我正在尝试不使用匕首来实现它。但这似乎违反了MVP架构。
从 Activity,我创建了一个 Interactor 实例。然后使用 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();
}
主持人
public interface SomePresenter {
public void startServiceFunction();
}
Presenter 实现
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);
}
}
我正在尝试通过参考此 link 在我的 android 项目中实施 MVP 模式:https://github.com/jpotts18/android-mvp
我已经成功实现了 view / presenter / interactor classes。我不清楚
service
调用代码放在哪里?
Since i cannot get the context inside the presenter or interactor class, I am not able to put the
service
call there
- 在哪里实施
GoogleApiClient
class?
Since
GoogleApiClient
also requires context to run, it also cannot be implemented inside the presenter or interactor without a context
我也在搜索你的第一个问题。但是,我有第二个问题的答案。
答案是Dagger2。 (http://google.github.io/dagger/)您可以使用 Dagger2 轻松注入 GoogleApiClient 对象。
使用 dagger 可以更轻松地将 Interactor 注入 Presenter。试试这个 link (https://github.com/spengilley/AndroidMVPService)
我正在尝试不使用匕首来实现它。但这似乎违反了MVP架构。
从 Activity,我创建了一个 Interactor 实例。然后使用 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();
}
主持人
public interface SomePresenter {
public void startServiceFunction();
}
Presenter 实现
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);
}
}