gpt4 book ai didi

android - 在android中使用MVP模式时,android服务调用和对GoogleAPIClient的调用应该写在哪里?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:08:53 24 4
gpt4 key购买 nike

我正在尝试通过引用此链接在我的 android 项目中实现 MVP 模式:https://github.com/jpotts18/android-mvp

我已经成功地实现了view/presenter/interactor 类。不清楚

  • 服务调用代码放在哪里?

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

最佳答案

使用 dagger 可以更轻松地将 Interactor 注入(inject) Presenter。试试这个链接(https://github.com/spengilley/AndroidMVPService)

我正在尝试不使用 Dagger 来实现它。但这似乎违反了 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);
}
}

关于android - 在android中使用MVP模式时,android服务调用和对GoogleAPIClient的调用应该写在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34771255/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com