ポーリングタイムアウト/実行ケース
説明:これはプロジェクトで使用される実際の操作です.関連するセグメント【タグ】を切り取ってください.
便利で自分も便利でみんなも便利です.
このクラスのインタフェースを後で実現すればよい
ここでJAvaではこのように使用されています
例えば、ポーリングされたタスクを実行しても、インタフェースはタイムアウト処理をしないだけです.
中で使われているEventBusについて
転送ゲート:
de.greenrobot.event.EventBus
https://github.com/greenrobot/EventBus
福利厚生(使いやすいオープンソースプロジェクト)を送る
http://blog.csdn.net/qq530918474/article/details/38920265
便利で自分も便利でみんなも便利です.
このクラスのインタフェースを後で実現すればよい
package org.bojoy.core.utils;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Handler;
/**
*
* @author luzhenyu
* PollingTimeOutTask
*
*/
public class PollingTimeoutTask {
@SuppressWarnings("unused")
private final String TAG = PollingTimeoutTask.class.getSimpleName();
private boolean start;
private int timeCounter;
private int period;
private int maxTime;
private int delay;
private Handler handler = new Handler();
private PollingListener listener;
private Timer timer;
private TimerTask task = new TimerTask() {
@Override
public void run() {
// LogProxy.i(TAG, "task run");
handler.post(new Runnable() {
@Override
public void run() {
/** */
if (maxTime == 0) {
listener.onExecute();
} else {
if (timeCounter < maxTime) {
listener.onExecute();
timeCounter += period;
} else {
listener.onTimeout();
suspendPolling();
}
}
}
});
}
};
/**
*
* @param period —— ( : )
* @param delay —— ( : )
* @param maxTime —— ( : ) 0-
* @param listener ——
*/
public PollingTimeoutTask(int period, int delay, int maxTime, PollingListener listener) {
this(period, delay, maxTime, listener, null);
}
/**
*
* @param period —— ( : )
* @param delay —— ( : )
* @param maxTime —— ( : ) 0-
* @param listener ——
* @param handler
*/
public PollingTimeoutTask(int period, int delay, int maxTime, PollingListener listener, Handler handler) {
this.period = period;
this.maxTime = maxTime;
this.delay = delay;
if (handler != null) {
this.handler = handler;
}
if (listener == null) {
throw new IllegalArgumentException("PollingListener can not null");
}
this.listener = listener;
timer = new Timer();
}
public void startPolling() {
start = true;
timeCounter = 0;
timer.schedule(task, delay, period);
}
public void suspendPolling() {
if (start) {
task.cancel();
timer.cancel();
task = null;
timer = null;
start = false;
}
}
public boolean isStart() {
return start;
}
public interface PollingListener {
public void onExecute();
public void onTimeout();
}
}
実装例package org.bojoy.gamefriendsdk.app.utils;
import org.bojoy.core.utils.PollingTimeoutTask;
import org.bojoy.core.utils.PollingTimeoutTask.PollingListener;
import de.greenrobot.event.EventBus;
/**
*
* @author luzhenyu
* GlobalPollingTimeoutTool
*
*/
public class GlobalPollingTimeoutTool implements PollingListener {
private final String TAG = GlobalPollingTimeoutTool.class.getSimpleName();
private static GlobalPollingTimeoutTool instance;
private final static Object block = new Object();
private PollingTimeoutTask pollingTask;
private EventBus eventBus = EventBus.getDefault();
private String taskTag;
private GlobalPollingTimeoutTool() {
}
public static GlobalPollingTimeoutTool getDefault() {
if (instance == null) {
synchronized (block) {
if (instance == null) {
instance = new GlobalPollingTimeoutTool();
}
}
}
return instance;
}
/**
*
* @param period
* @param delay
* @param maxTime
* @param tag
* @return , , , ,
*/
public boolean startPollingTask(int period, int delay, int maxTime, String tag) {
if (pollingTask != null && pollingTask.isStart()) {
return false;
}
taskTag = tag;
pollingTask = null;
pollingTask = new PollingTimeoutTask(period, delay, maxTime, this);
pollingTask.startPolling();
return true;
}
/**
*
*/
public void suspendGlobalPolling() {
if (pollingTask != null && pollingTask.isStart()) {
pollingTask.suspendPolling();
}
pollingTask = null;
}
public String getTaskTag() {
return taskTag;
}
public boolean isStart(String tag) {
if (tag == null) {
throw new IllegalArgumentException("Tag not null");
}
if (pollingTask == null) {
return false;
}
if (tag.equals(taskTag)) {
return pollingTask.isStart();
}
return false;
}
@Override
public void onExecute() {
eventBus.post(new PollingExecuteEvent(taskTag));
}
@Override
public void onTimeout() {
eventBus.post(new PollingTimeoutEvent(taskTag));
}
public class PollingExecuteEvent {
private String taskTag;
public PollingExecuteEvent(String tag) {
taskTag = tag;
}
public String getTaskTag() {
return taskTag;
}
}
public class PollingTimeoutEvent {
private String taskTag;
public PollingTimeoutEvent(String tag) {
taskTag = tag;
}
public String getTaskTag() {
return taskTag;
}
}
}
ここでJAvaではこのように使用されています
package org.bojoy.gamefriendsdk.app.dock.page.impl;
import org.bojoy.BJMGFCommon;
import org.bojoy.core.utils.ReflectResourceId;
import org.bojoy.gamefriendsdk.Resource;
import org.bojoy.gamefriendsdk.app.communication.request.BaseRequestSession;
import org.bojoy.gamefriendsdk.app.dock.activity.BJMGFActivity;
import org.bojoy.gamefriendsdk.app.dock.page.BaseActivityPage;
import org.bojoy.gamefriendsdk.app.eventhandler.event.BaseReceiveEvent;
import org.bojoy.gamefriendsdk.app.screen.page.PageManager;
import org.bojoy.gamefriendsdk.app.screen.widget.ClearEditText;
import org.bojoy.gamefriendsdk.app.utils.GlobalPollingTimeoutTool;
import org.bojoy.gamefriendsdk.app.utils.GlobalPollingTimeoutTool.PollingExecuteEvent;
import org.bojoy.gamefriendsdk.app.utils.GlobalPollingTimeoutTool.PollingTimeoutEvent;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
*
* @author luzhenyu
* DockCheckPhonePage
*
*/
public class DockCheckPhonePage extends BaseActivityPage {
private final String TAG = DockModifyPhonePage.class.getSimpleName();
private final int Timeout_Max = 60 * 2;
private int period = 1000,delay = 0, maxTime = 1000 * 60 * 2, resendTime = Timeout_Max;
private LinearLayout mBackLayout = null;
private TextView mToastTextView = null;
private ClearEditText mCodeEditText = null;
private TextView mReSendTextView,mTimeTextView;
private Button mSureButton = null;
/** bug 2 */
/** */
private View keyboardView;
private GlobalPollingTimeoutTool globalPolling = GlobalPollingTimeoutTool.getDefault();//【 】
public DockCheckPhonePage(Context context,
PageManager manager, BJMGFActivity activity) {
super(ReflectResourceId.getLayoutId(context,
Resource.layout.bjmgf_sdk_dock_account_checkphone), context, manager, activity);
}
public void onCreateView(View view) {
mBackLayout = (LinearLayout)view.findViewById(ReflectResourceId.getViewId(context,
Resource.id.bjmgf_sdk_float_account_manager_checkphone_backLlId));
mToastTextView = (TextView)view.findViewById(ReflectResourceId.getViewId(context,
Resource.id.bjmgf_sdk_float_account_manager_checkphone_messageToastTextViewId));
mReSendTextView = (TextView)view.findViewById(ReflectResourceId.getViewId(context,
Resource.id.bjmgf_sdk_float_account_manager_checkphone_resendTextViewId));
mSureButton = (Button)view.findViewById(ReflectResourceId.getViewId(context,
Resource.id.bjmgf_sdk_float_account_manager_checkphone_buttonId));
mCodeEditText = (ClearEditText)view.findViewById(ReflectResourceId.getViewId(context,
Resource.id.bjmgf_sdk_float_account_manager_checkphone_editTextId));
mTimeTextView = (TextView)view.findViewById(ReflectResourceId.getViewId(context,
Resource.id.bjmgf_sdk_checkPhoneTimeTvId));
keyboardView = (View)view.findViewById(ReflectResourceId.getViewId(context,
Resource.id.bjmgf_sdk_float_account_manager_checkphone_keyboard));
mBackLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
manager.previousPage();
}
});
mReSendTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
/** bug 1 */
/** TimeTask */
//
resendTime = Timeout_Max;
globalPolling.startPollingTask(period, delay, maxTime, TAG);//【 】
showProgressDialog();
communicator.sendRequest(BaseRequestSession.Request_BindPhone,BJMGFCommon.getMobile());//
}
});
mSureButton.setOnClickListener(new View.OnClickListener() {
//
@Override
public void onClick(View arg0) {
if (checkVerifyCodeLength(mCodeEditText.getEditText())) {
showProgressDialog();
communicator.sendRequest(BaseRequestSession.Request_BindPhone_Check_Code, mCodeEditText.getEditText());//
}else {
showToast(getString(Resource.string.bjmgf_sdk_register_dialog_checkVerifyCodeErrorStr));
}
}
});
/** bug 1 */
/** TimeTask */
globalPolling.startPollingTask(period, delay, maxTime, TAG);
super.onCreateView(view);
}
@Override
public void createdSendHttp() {
}
@Override
public void setView() {
String str = mToastTextView.getText().toString();
/**
* "xxxxxx158****5454xxxxxx"
* */
mToastTextView.setText(String.format(str, transformPhoneNumberType(BJMGFCommon.getMobile())));
resetTimeText();
}
/** bug 2 */
/** */
@Override
public View getKeyboardView() {
return keyboardView;
}
/**
* bug 1
*
* */
private String resetTimeText() {
return String.format(context.getResources().getString(ReflectResourceId.getStringId(context,
Resource.string.bjmgf_sdk_floatWindow_accountManager_checkPhone_receiveCodeToastLeftStr)),
resendTime);
}
public void onEventMainThread(BaseReceiveEvent revEvent) {
if (revEvent.getRequestType() == BaseRequestSession.Request_BindPhone_Check_Code) {
dismissProgressDialog();
if (revEvent.isSuccess()) {
if (globalPolling != null) {
globalPolling.suspendGlobalPolling();//【 】
}
showToast(getString(Resource.string.bjmgf_sdk_BindPhoneSuccessedStr));
DockAccountPage page = new DockAccountPage(context, manager, activity);
manager.clearTopPage(page);
}
} else
if (revEvent.getRequestType() == BaseRequestSession.Request_BindPhone) {
dismissProgressDialog();
if (revEvent.isSuccess()) {
showToast(getString(Resource.string.bjmgf_sdk_resendVerifyCodeStr));
}
}
}
public void onEventMainThread(PollingExecuteEvent executeEvent) {//【 】
if (executeEvent.getTaskTag().equals(TAG)) {
resendTime--;
mTimeTextView.setText(resetTimeText());
}
}
public void onEventMainThread(PollingTimeoutEvent timeoutEvent) {//【 】
if (timeoutEvent.getTaskTag().equals(TAG)) {
initPollingRsendTv(true);
}
}
private void initPollingRsendTv(boolean start) {
mReSendTextView.setVisibility(start? View.VISIBLE : View.GONE);
}
}
例えば、ポーリングされたタスクを実行しても、インタフェースはタイムアウト処理をしないだけです.
package org.bojoy.gamefriendsdk.app.utils;
import org.bojoy.BJMGFCommon;
import org.bojoy.core.utils.LogProxy;
import org.bojoy.core.utils.PollingTimeoutTask;
import org.bojoy.core.utils.PollingTimeoutTask.PollingListener;
import org.bojoy.core.utils.Util;
import org.bojoy.gamefriendsdk.app.BJMGFSdk;
import org.bojoy.gamefriendsdk.app.communication.Communicator;
import org.bojoy.gamefriendsdk.app.communication.request.BaseRequestSession;
import org.bojoy.gamefriendsdk.app.eventhandler.event.BJMGFSdkEvent;
import org.bojoy.gamefriendsdk.app.eventhandler.event.BaseReceiveEvent;
import org.bojoy.gamefriendsdk.app.model.BJMGFGlobalData;
import org.bojoy.gamefriendsdk.app.widget.pulltorefresh.internal.Utils;
import de.greenrobot.event.EventBus;
/**
* @author luzhenyu
* MessagePollingTool 5
* */
public class MessagePollingTool implements PollingListener {
private PollingTimeoutTask task;
private EventBus eventBus = EventBus.getDefault();
private BJMGFGlobalData bjmgfData = BJMGFGlobalData.getDefault();
public void start() {
task = new PollingTimeoutTask(5 * 60 *1000, 1000, 0, this);
task.startPolling();
eventBus.register(this);
}
public void stop() {//
if (task == null || !task.isStart()) {
return;
}
task.suspendPolling();
task = null;
eventBus.unregister(this);
}
@Override
public void onExecute() {
Communicator communicator = BJMGFSdk.getDefault().getCommunicator();
if (Util.stringIsEmpty(BJMGFCommon.getOfflineTime())) {
if (Util.stringIsEmpty(bjmgfData.get_OfflineTime())) {
communicator.sendRequest(BaseRequestSession.Request_Message_Offline);
}else {
BJMGFCommon.setOfflineTime(bjmgfData.get_OfflineTime());
communicator.sendRequest(BaseRequestSession.Request_Message_Offline);
}
} else {
communicator.sendRequest(BaseRequestSession.Request_Message_Offline);
}
}
@Override
public void onTimeout() {
<span style="white-space:pre"> </span>//
}
public void onEventMainThread(BaseReceiveEvent revEvent) {
if (revEvent.getRequestType() == BaseRequestSession.Request_Message_Offline) {
if (revEvent.isSuccess()) {
if (Util.stringIsEmpty(BJMGFCommon.getOfflineTime())) {
bjmgfData.saveOfflineTime("0");
} else {
bjmgfData.saveOfflineTime(BJMGFCommon.getOfflineTime());
}
eventBus.post(new BJMGFSdkEvent(BJMGFSdkEvent.Get_Offline_Message));
}
}
}
}
中で使われているEventBusについて
転送ゲート:
de.greenrobot.event.EventBus
https://github.com/greenrobot/EventBus
福利厚生(使いやすいオープンソースプロジェクト)を送る
http://blog.csdn.net/qq530918474/article/details/38920265