我是MVP和Dagger2的新手,但是当方法onLocationChanged()上的位置发生变化时,如何从我的位置服务到Presenter获取位置的问题。
我在这里尝试了一下:
位置服务:
public class LocationService extends Service implements LocationListener {
private LocationManager locationManager;
private Location currentLocation;
@Inject
EventBus bus;
@Override
public void onCreate() {
super.onCreate();
ServiceComponent component = DaggerServiceComponent.builder()
.applicationComponent(((MvpApplication) getApplication()).getComponent())
.build();
component.inject(this);
EventBus.getDefault().register(this);
}
@SuppressLint("MissingPermission")
@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
if (intent.getAction().equals("startListening")) {
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
else {
if (intent.getAction().equals("stopListening")) {
locationManager.removeUpdates(this);
locationManager = null;
}
}
return START_STICKY;
}
@Override
public IBinder onBind(final Intent intent) {
return null;
}
public void onLocationChanged(final Location location) {
currentLocation = location;
EventBus.getDefault().post(new LocationUpdateEvent(location.getLatitude(), location.getLongitude()));
// TODO this is where you'd do something like context.sendBroadcast()
}
public void onProviderDisabled(final String provider) {
}
public void onProviderEnabled(final String provider) {
}
public void onStatusChanged(final String arg0, final int arg1, final Bundle arg2) {
}
@Subscribe(threadMode = ThreadMode.MAIN)
public LocationUpdateEvent produceLocationUpdate() {
if (currentLocation != null) {
return new LocationUpdateEvent(currentLocation.getLatitude(),
currentLocation.getLongitude());
/* From here i want to send it to my presenter whenever location changed*/
} else {
return null;
}
}
@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
AppComponent:
@Singleton
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
void inject(MvpApplication app);
void inject(LocationService service);
@ApplicationContext
Context context();
Application application();
DataManager getDataManager();
EventBus eventBus();
}
服务组件:
@PerService
@Component(dependencies = ApplicationComponent.class, modules = ServiceModule.class)
public interface ServiceComponent {
void inject(LocationService service);
}
申请模块:
@Module
public class ApplicationModule {
private final Application mApplication;
public ApplicationModule(Application application) {
mApplication = application;
}
@Provides
@ApplicationContext
Context provideContext() {
return mApplication;
}
@Provides
Application provideApplication() {
return mApplication;
}
@Provides
@DatabaseInfo
String provideDatabaseName() {
return AppConstants.DB_NAME;
}
@Provides
@Singleton
DataManager provideDataManager(AppDataManager appDataManager) {
return appDataManager;
}
@Provides
@Singleton
DbHelper provideDbHelper(AppDbHelper appDbHelper) {
return appDbHelper;
}
@Provides
@Singleton
public EventBus eventBus() {
return new EventBus();
}
}
活动组件:
@PerActivity
@Component(dependencies = ApplicationComponent.class, modules = ActivityModule.class)
public interface ActivityComponent {
void inject(SplashActivity activity);
void inject(MainActivity activity);
}
这是我的主持人,我想要服务的位置:
public class MainActivityPresenter <V extends MainActivityMvpView> extends BasePresenter<V> implements MainActivityMvpPresenter<V> {
private MainActivityMvpView view;
@Inject
public MainActivityPresenter(DataManager dataManager, SchedulerProvider schedulerProvider, CompositeDisposable compositeDisposable) {
super(dataManager, schedulerProvider, compositeDisposable);
}
@Override
public void onStop() {
}
@Override
public void onAttach(V mvpView) {
super.onAttach(mvpView);
getCompositeDisposable().add(getDataManager()
.seeDatabaseUserInfo()
.subscribeOn(getSchedulerProvider().io())
.observeOn(getSchedulerProvider().ui())
.concatMap(new Function<Boolean, ObservableSource<Boolean>>() {
@Override
public ObservableSource<Boolean> apply(Boolean aBoolean) throws Exception {
return getDataManager().seeDatabaseUserInfo();
}
})
.subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Exception {
if (!isViewAttached()) {
return;
}
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
if (!isViewAttached()) {
return;
}
getMvpView().onError(R.string.error);
}
}));
view=getMvpView();
}
}
答案 0 :(得分:0)
为了简单起见,我要说你应该使用Publish Subject
(more here),拥有一个包含合同的单件包装器,以便为发布主题生成位置更新,并让您的演示者听取通过订阅更新并确定表示逻辑。