快速提问有人可以帮助我吗?我只是想知道我的Asynctask实现是否正确实现,我是MVP Clean Architecure模式设计的初学者,并且不确定它是否是一个好方法。 基本上,我在Interactor类中有一个方法,在这里我调用了Asynctask并回调了那里的数据,然后使用一个接口将数据返回给我的演示者,然后更新视图。我希望有人能帮助我。
顺便说一句,这种方式实际上可以正常工作,但是速度很慢
public class MapPresenterImpl implements MapPresenter,MapInteractorImpl.onDataChanged{
private MapView view;
private MapInteractor interactor;
public MapPresenterImpl(MapView view) {
this.view = view;
this.interactor = new MapInteractorImpl(this);
}
@Override
public void setInfo(LocationManager manager,Context mContext) {
if (view != null) {
view.showProgress(true);
interactor.getCurrentInfMaps(manager,mContext);
}
}
@Override
public void notifyPresenterDataChanged(Location location) {
view.showProgress(false);
if (null != location){
view.setInfo(location);
}
}
@Override
public void notifyPresenterGpsDisabled(boolean isDisabledGps) {
if (isDisabledGps){
view.showProgress(false);
view.showMessage("The gps is disabled please enable first");
}
}
}
public class MapInteractorImpl implements MapInteractor,GpsProvider.listener{
private final String TAG = getClass().getSimpleName();
private onDataChanged listener;
public MapInteractorImpl(onDataChanged listener){
this.listener = listener;
}
@Override
public void getCurrentInfMaps(LocationManager manager, Context context) {
new GpsProvider(manager,context,this);
}
@Override
public void dataChanged(Location location) {
if (location!=null){
listener.notifyPresenterDataChanged(location);
Log.i("MapInteractorImpl() ",String.format("Lat: %s, Long: %s",String.valueOf(location.getLatitude()),String.valueOf(location.getLongitude())));
} else{
listener.notifyPresenterGpsDisabled(true);
}
}
public interface onDataChanged{
void notifyPresenterDataChanged(Location location);
void notifyPresenterGpsDisabled(boolean isDisabledGps);
}
}
public class GpsProvider implements android.location.LocationListener{
private LocationManager manager;
private Context context;
private listener listener;
public GpsProvider(LocationManager manager,Context context,listener listener){
this.manager = manager;
this.context = context;
this.listener = listener;
init();
}
public interface listener{
void dataChanged(Location location);
}
private final String TAG = getClass().getSimpleName();
@Override
public void onLocationChanged(Location location) {
if (location!=null){
Log.i(TAG,String.format("Lat: %s, Long: %s",String.valueOf(location.getLatitude()),String.valueOf(location.getLongitude())));
listener.dataChanged(location);
} else{
Log.i(TAG,"Location is null");
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
Log.i(TAG,"CurrentProvider enable: "+provider);
}
@Override
public void onProviderDisabled(String provider) {
Log.i(TAG,"CurrentProvider disable: "+provider);
}
@SuppressLint("MissingPermission")
public void init(){
manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (isGpsEnabled()){
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
2000,
10, this);
} else{
Log.i(TAG,"The gps is disabled");
listener.dataChanged(null);
}
}
public boolean isGpsEnabled(){
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER))
return true;
return false;
}
}
这是我在GitHub中的SampleMVP! https://github.com/DjangoLC/SampleMvp 如果您有空闲时间,我将不胜感激!