Commit 9cc36a03 by gao.chao

组件化工具-延时回调

parent 3d9d83f3
......@@ -5,10 +5,14 @@ import android.support.annotation.Nullable;
import android.view.View;
import com.alibaba.fastjson.JSON;
import com.gc.call.CallConstant;
import com.gc.call.CallManage;
import com.gc.call.CallParticipationBean;
import com.gc.call.CallReturnBean;
import com.gc.call.CallReturnListener;
import com.mayi.fastdevelop.base.BaseAPI;
import com.mayi.fastdevelop.base.BaseActivity;
import com.mayi.fastdevelop.base.BasePresenter;
import com.mayi.fastdevelop.bean.LocationBean;
import com.mayi.fastdevelop.bean.UserInfo;
import com.mayi.fastdevelop.comnon.Constant;
import com.mayi.fastdevelop.okhttp.NetWorkBuilder;
......@@ -102,17 +106,17 @@ public class FunctionActivity extends BaseActivity {
findViewById(R.id.b8).setOnClickListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
// LocationUtils.startLocation(v.getContext(), new LocationUtils.LocationCallback() {
// @Override
// public void onFail(String msg) {
// showToast(msg);
// }
//
// @Override
// public void onSuccess(LocationBean bean) {
// showToast(JSON.toJSONString(bean));
// }
// });
CallParticipationBean bean = new CallParticipationBean();
bean.setTag("map_location");
bean.setContext(FunctionActivity.this);
bean.setLooper(CallConstant.LOOPER_NO_MAIN);
bean.setListener(new CallReturnListener() {
@Override
public void onReturn(CallReturnBean bean) {
showToast(JSON.toJSONString(bean));
}
});
CallManage.getInstance().handleTarget(bean);
}
});
}
......
......@@ -28,6 +28,7 @@ public class CallManage {
map.put(callTarget.getTag(), callTarget);
}
//处理call
public void handleTarget(final CallParticipationBean bean) {
Log.i(TAG, "handleTarget:" + bean.getTag());
CallTarget callTarget = map.get(bean.getTag());
......@@ -37,31 +38,86 @@ public class CallManage {
callReturn.setMsg("没有找到对应的CallTarget");
callReturn.setCode(CallConstant.CODE_NO_TARGET);
bean.getListener().onReturn(callReturn);
bean.getListener().onReturn(callReturn);
}
} else {
if (callTarget instanceof CallImmediatelyHandlerTarget) {
onImmediatelyHandler(bean, (CallImmediatelyHandlerTarget) callTarget);
} else if (callTarget instanceof CallTimeHandTarget) {
onTimeHandHandler(bean, (CallTimeHandTarget) callTarget);
}
}
}
private void onImmediatelyHandler(final CallParticipationBean bean, final CallImmediatelyHandlerTarget target) {
//处理耗时不能立刻返回的call
private void onTimeHandHandler(final CallParticipationBean bean, final CallTimeHandTarget target) {
handler.post(new Runnable() {
@Override
public void run() {
if (bean.getLooper() == CallConstant.LOOPER_MAIN) {
target.delayProcessing(bean, new CallTimeHandlerListener() {
@Override
public void timeHandler(CallReturnBean c) {
if (bean.getListener() != null) {
bean.getListener().onReturn(c);
}
}
});
} else if (bean.getLooper() == CallConstant.LOOPER_NO_MAIN) {
AsyncTask asyncTask = new AsyncTask() {
@Override
protected Object doInBackground(Object[] objects) {
target.delayProcessing(bean, new CallTimeHandlerListener() {
@Override
public void timeHandler(CallReturnBean c) {
if (bean.getListener() != null) {
bean.getListener().onReturn(c);
}
}
});
return null;
}
};
asyncTask.execute();
} else if (bean.getLooper() == CallConstant.LOOPER_NO_MAIN_HANDLER_AND_MIAN_RETURN) {
AsyncTask asyncTask = new AsyncTask() {
@Override
protected CallReturnBean doInBackground(Object[] objects) {
target.delayProcessing(bean, new CallTimeHandlerListener() {
@Override
public void timeHandler(final CallReturnBean c) {
handler.post(new Runnable() {
@Override
public void run() {
CallReturnBean c = target.onHandle(bean);
if (bean.getListener() != null) {
bean.getListener().onReturn(c);
}
}
});
}
});
return null;
}
};
asyncTask.execute();
}
}
});
}
//处理不耗时能立刻返回的call
private void onImmediatelyHandler(final CallParticipationBean bean,
final CallImmediatelyHandlerTarget target) {
handler.post(new Runnable() {
@Override
public void run() {
if (bean.getLooper() == CallConstant.LOOPER_MAIN) {
CallReturnBean c = target.onHandle(bean);
if (bean.getListener() != null) {
bean.getListener().onReturn(c);
}
} else if (bean.getLooper() == CallConstant.LOOPER_NO_MAIN) {
AsyncTask asyncTask = new AsyncTask() {
@Override
......@@ -96,20 +152,5 @@ public class CallManage {
});
}
// private CallReturnBean onImmediatelyHandle(CallParticipationBean bean) {
// CallTarget callTarget = map.get(bean.getTag());
// if (callTarget != null) {
// if (callTarget instanceof CallImmediatelyHandlerTarget) {
// return ((CallImmediatelyHandlerTarget) callTarget).onHandle(bean);
// }
// } else {
// CallReturnBean callReturn = new CallReturnBean();
// callReturn.setMsg("没有找到对应的CallTarget");
// callReturn.setCode(CallConstant.CODE_NO_TARGET);
// bean.getListener().onReturn(callReturn);
// return callReturn;
// }
// return null;
// }
}
......@@ -20,7 +20,7 @@ public class LocationUtils {
public void onLocationChanged(AMapLocation amapLocation) {
if (amapLocation != null) {
if (amapLocation.getErrorCode() == 0) {
LogUtils.e("amapLocation" + amapLocation);
LogUtils.i("amapLocation" + amapLocation);
if (callback != null) {
LocationBean bean = new LocationBean();
bean.setAddress(amapLocation.getAddress());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment