/** 暂停并且清除定时任务 */ public void pauseClearDelayed() { pause(); if (mVideoHandler.hasMessages(HANDLER_MESSAGE_PARSE)) mVideoHandler.removeMessages(HANDLER_MESSAGE_PARSE); if (mVideoHandler.hasMessages(HANDLER_MESSAGE_LOOP)) mVideoHandler.removeMessages(HANDLER_MESSAGE_LOOP); }
@SuppressWarnings("unchecked") @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); switch (msg.what) { case IS_REQUSTING: mDialog = Utils.createLoadingDialog(mContext, "正在请求设备周围WiFi列表..."); mDialog.show(); break; case REQUST_TIMEOUT: if (mDialog != null) mDialog.dismiss(); if (handler.hasMessages(REQUST_TIMEOUT)) { handler.removeMessages(REQUST_TIMEOUT); } Toast.makeText(mContext, "请求WiFi列表失败,请重试! ", Toast.LENGTH_SHORT).show(); break; case R.id.requst_wifi_list_id: if (handler.hasMessages(REQUST_TIMEOUT)) { handler.removeMessages(REQUST_TIMEOUT); if (mDialog != null) mDialog.dismiss(); int resultCode = msg.arg1; if (resultCode == 0) { showWiFiListDialog((ArrayList<HashMap<String, Object>>) msg.obj); } else { Toast.makeText(mContext, "设备附近暂时没有WiFi信号! ", Toast.LENGTH_SHORT).show(); } } else { handler.removeMessages(R.id.requst_wifi_list_id); } break; case IS_SETTING: mDialog = Utils.createLoadingDialog(mContext, "正在配置设备WiFi网络..."); mDialog.show(); break; case SET_TIMEOUT: if (mDialog != null) mDialog.dismiss(); if (handler.hasMessages(SET_TIMEOUT)) { handler.removeMessages(SET_TIMEOUT); } Toast.makeText(mContext, "配置设备WiFi网络超时,请重试! ", Toast.LENGTH_SHORT).show(); break; case R.id.set_term_wifi_id: if (handler.hasMessages(SET_TIMEOUT)) { handler.removeMessages(SET_TIMEOUT); if (mDialog != null) mDialog.dismiss(); int resultCode = msg.arg1; if (resultCode == 1) { Toast.makeText(mContext, "配置设备WiFi网络成功! ", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(mContext, "配置设备WiFi网络失败,请重试! ", Toast.LENGTH_SHORT).show(); } } else { handler.removeMessages(R.id.set_term_wifi_id); } break; } }
@Test public void sendEmptyMessage_addMessageToQueue() { ShadowLooper.pauseMainLooper(); Handler handler = new Handler(); assertThat(handler.hasMessages(123)).isFalse(); handler.sendEmptyMessage(123); assertThat(handler.hasMessages(456)).isFalse(); assertThat(handler.hasMessages(123)).isTrue(); ShadowLooper.idleMainLooper(0); assertThat(handler.hasMessages(123)).isFalse(); }
@Test public void sendEmptyMessageDelayed_sendsMessageAtCorrectTime() { ShadowLooper.pauseMainLooper(); Handler handler = new Handler(); handler.sendEmptyMessageDelayed(123, 500); assertThat(handler.hasMessages(123)).isTrue(); ShadowLooper.idleMainLooper(100); assertThat(handler.hasMessages(123)).isTrue(); ShadowLooper.idleMainLooper(400); assertThat(handler.hasMessages(123)).isFalse(); }
void elementHandler(int x, int y, int action) { for (Point point : points.getValues()) if (point.getRegion().contains(x, y)) return; switch (action) { case MotionEvent.ACTION_DOWN: mHandler.sendMessageDelayed( Message.obtain(mHandler, CODE_ELEMENT_LONGCLICK), ViewConfiguration.getLongPressTimeout()); fX = x - getScVal(this.x); fY = y - getScVal(this.y); break; case MotionEvent.ACTION_MOVE: if (mMoovingPermitted) { dX = getGridCoord((x - fX) / getScaleFactor()); dY = getGridCoord((y - fY) / getScaleFactor()); if (this.x != dX | this.y != dY) { this.x = dX; this.y = dY; for (Link link : links) link.measure(); onUpdate(); getSdk().getSdkFragment().update(); isPossibleToRemoving = getSdk().getSdkFragment().getRemovingElementView().checkRemoving(this); } } break; case MotionEvent.ACTION_UP: if (mMoovingPermitted) { mMoovingPermitted = false; getSdk().getSdkFragment().lockScroll(false); getSdk().getSdkFragment().setRemovingViewState(false); MainActivity.mainFragment.showTabs(); } else if (mLinkCreationToggle) { mLinkCreationToggle = false; } else { if (!isSelected()) sdk.setSelectedElement(this); } if (mHandler.hasMessages(CODE_ELEMENT_LONGCLICK)) mHandler.removeMessages(CODE_ELEMENT_LONGCLICK); if (isPossibleToRemoving) getSdk().getSdkFragment().getRemovingElementView().remove(this); break; case MotionEvent.ACTION_CANCEL: if (mHandler.hasMessages(CODE_ELEMENT_LONGCLICK)) mHandler.removeMessages(CODE_ELEMENT_LONGCLICK); break; } }
@Test public void sendMessageAtTime_sendsMessageAtCorrectTime() { ShadowLooper.pauseMainLooper(); Handler handler = new Handler(); Message message = handler.obtainMessage(123); handler.sendMessageAtTime(message, 500); assertThat(handler.hasMessages(123)).isTrue(); ShadowLooper.idleMainLooper(100); assertThat(handler.hasMessages(123)).isTrue(); ShadowLooper.idleMainLooper(400); assertThat(handler.hasMessages(123)).isFalse(); }
@Test public void testHasMessagesWithWhatAndObject() { ShadowLooper.pauseMainLooper(); Object testObject = new Object(); Handler handler = new Handler(); Message message = handler.obtainMessage(123, testObject); assertFalse(handler.hasMessages(123, testObject)); handler.sendMessage(message); assertTrue(handler.hasMessages(123, testObject)); }
private static void releaseWakeLockIfHandlerIdle() { if (mHandler.hasMessages(MSG_LONGPRESS_TIMEOUT) || mHandler.hasMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT)) { if (DEBUG) Log.v(TAG, "Handler still has messages pending, not releasing wake lock"); return; } if (mWakeLock != null) { if (DEBUG) Log.v(TAG, "Releasing wake lock"); mWakeLock.release(); mWakeLock = null; } }
/** 区域内循环播放 */ public void loopDelayed(int startTime, int endTime) { if (mVideoHandler.hasMessages(HANDLER_MESSAGE_PARSE)) mVideoHandler.removeMessages(HANDLER_MESSAGE_PARSE); if (mVideoHandler.hasMessages(HANDLER_MESSAGE_LOOP)) mVideoHandler.removeMessages(HANDLER_MESSAGE_LOOP); int delayMillis = endTime - startTime; seekTo(startTime); if (!isPlaying()) start(); if (mVideoHandler.hasMessages(HANDLER_MESSAGE_LOOP)) mVideoHandler.removeMessages(HANDLER_MESSAGE_LOOP); mVideoHandler.sendMessageDelayed( mVideoHandler.obtainMessage(HANDLER_MESSAGE_LOOP, getCurrentPosition(), delayMillis), delayMillis); }
private void m() { t = m.getCurrentPosition(); if (J.hasMessages(7)) { J.removeMessages(7); } if (J.hasMessages(9)) { J.removeMessages(9); } if (b.getVisibility() == 0) { b.setVisibility(8); } new Thread(new jw(this)).start(); }
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equalsIgnoreCase(ACTION_SHOW_PROGRESSBAR)) { setSupportProgressBarIndeterminateVisibility(true); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } else if (action.equalsIgnoreCase(ACTION_HIDE_PROGRESSBAR)) { setSupportProgressBarIndeterminateVisibility(false); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } else if (action.equalsIgnoreCase(ACTION_SHOW_TEXTINFO)) { String info = intent.getStringExtra("info"); int max = intent.getIntExtra("max", 0); int progress = intent.getIntExtra("progress", 100); mInfoText.setText(info); mInfoProgress.setMax(max); mInfoProgress.setProgress(progress); if (info == null) { /* Cancel any upcoming visibility change */ mHandler.removeMessages(ACTIVITY_SHOW_INFOLAYOUT); mInfoLayout.setVisibility(View.GONE); } else { /* Slightly delay the appearance of the progress bar to avoid unnecessary flickering */ if (!mHandler.hasMessages(ACTIVITY_SHOW_INFOLAYOUT)) { Message m = new Message(); m.what = ACTIVITY_SHOW_INFOLAYOUT; mHandler.sendMessageDelayed(m, 300); } } } }
public boolean moveTetrominoRight() { Log.d(TAG, "move tetromino to right"); synchronized (lockObj) { if (currentTetromino.moveRight(oldMap)) { currentMap.copyFrom(oldMap); currentTetromino.putOnMap(currentMap); lastMoveMap.copyFrom(currentMap); // if old position will collision but new moved position will not, then need remove the new // tetromino message and send tetromino move message= if (!currentTetromino.isCollisionY( currentTetromino.getY() + 1, currentTetromino.getSharp(), oldMap)) { if (handler.hasMessages(MSG_NEW_TETROMINO)) { handler.removeMessages(MSG_NEW_TETROMINO); handler.sendEmptyMessageDelayed(MSG_TETROMINO_MOVE, TETROMINO_MOVE_INTERVAL_RESEND); } } for (ITetrisListener listener : customizedListeners) { listener.onTetrominoMove(); } return true; } return false; } }
void scheduleCheckFocusLocked(View view) { Handler vh = view.getHandler(); if (vh != null && !vh.hasMessages(ViewRoot.CHECK_FOCUS)) { // This will result in a call to checkFocus() below. vh.sendMessage(vh.obtainMessage(ViewRoot.CHECK_FOCUS)); } }
@Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); switch (msg.what) { case IS_REQUESTING: mDialog = Utils.createLoadingDialog(mContext, "正在提交修改..."); mDialog.show(); break; case REQUEST_TIMEOUT: if (mDialog != null) mDialog.dismiss(); if (handler.hasMessages(REQUEST_TIMEOUT)) { handler.removeMessages(REQUEST_TIMEOUT); } Value.isNeedReqTermListFlag = false; Toast.makeText(mContext, "修改终端名称失败,网络超时!", Toast.LENGTH_SHORT).show(); break; case R.id.modify_device_name_id: if (handler.hasMessages(REQUEST_TIMEOUT)) { handler.removeMessages(REQUEST_TIMEOUT); if (mDialog != null) mDialog.dismiss(); int resultCode = msg.arg1; if (resultCode == 0) { Toast.makeText(mContext, "修改终端名称成功!", Toast.LENGTH_SHORT).show(); xmlData.updateItemName(mDeviceId, mDeviceName); Bundle bundle = new Bundle(); bundle.putString("deviceId", mDeviceId); bundle.putString("deviceName", mDeviceName); Intent intent = new Intent(); intent.putExtras(bundle); setResult(1, intent); finish(); overridePendingTransition(0, R.anim.down_out); } else { Toast.makeText( mContext, "修改终端名称失败," + Utils.getErrorReason(resultCode) + "!", Toast.LENGTH_SHORT) .show(); } } else { handler.removeMessages(R.id.modify_device_name_id); } break; } }
@Test public void removeMessages_takesMessageOutOfQueue() { ShadowLooper.pauseMainLooper(); Handler handler = new Handler(); handler.sendEmptyMessageDelayed(123, 500); handler.removeMessages(123); assertThat(handler.hasMessages(123)).isFalse(); }
@Test public void removeMessage_withSpecifiedObject() throws Exception { ShadowLooper.pauseMainLooper(); Handler handler = new Handler(); Message.obtain(handler, 123, "foo").sendToTarget(); Message.obtain(handler, 123, "bar").sendToTarget(); assertThat(handler.hasMessages(123)).isTrue(); assertThat(handler.hasMessages(123, "foo")).isTrue(); assertThat(handler.hasMessages(123, "bar")).isTrue(); assertThat(handler.hasMessages(123, "baz")).isFalse(); handler.removeMessages(123, "foo"); assertThat(handler.hasMessages(123)).isTrue(); handler.removeMessages(123, "bar"); assertThat(handler.hasMessages(123)).isFalse(); }
/** * If a force cancel is pending, remove it. This is usually called when a command returns and * thereby does not need to be canceled anymore. */ protected void removeForceCancel() { if (DEBUG) { if (mHandler.hasMessages(AsyncCommandHandler.MSG_FORCE_CANCEL)) { Log.i(LOG_TAG, "[FORCE CANCEL] Removed"); } } mHandler.removeMessages(AsyncCommandHandler.MSG_FORCE_CANCEL); }
@Override public void onChange(boolean selfChange, Uri uri) { super.onChange(selfChange, uri); Log.i(TAG, "[onChange]uri = " + uri); if (mVoiceHandler.hasMessages(MSG_GET_CONTACTS_NAME)) { mVoiceHandler.removeMessages(MSG_GET_CONTACTS_NAME); } mVoiceHandler.sendEmptyMessage(MSG_GET_CONTACTS_NAME); }
public void start() { if (!mHandler.hasMessages(MSG_UPDATE)) { mRunning = true; mHandler.sendEmptyMessage(MSG_UPDATE); if (mAnimationListener != null) { mAnimationListener.onAnimationStart(this); } } }
private void triggerPing() { Log.d(this, "triggerPing(): " + mPingEnabled + " " + this); if (mPingEnabled && !mPingHandler.hasMessages(PING_MESSAGE_WHAT)) { ping(); if (ENABLE_PING_AUTO_REPEAT) { mPingHandler.sendEmptyMessageDelayed(PING_MESSAGE_WHAT, PING_REPEAT_DELAY_MS); } } }
protected void onPause() { super.onPause(); e = false; if (a.hasMessages(2)) { a.removeMessages(2); a(); } b.n(); }
public void markMessageAsRead(Message message) { if (unreadMessageHandler == null) { Log.e("zulipApp", "markMessageAsRead called before unreadMessageHandler was instantiated"); return; } unreadMessageQueue.offer(message.getID()); if (!unreadMessageHandler.hasMessages(0)) { unreadMessageHandler.sendEmptyMessageDelayed(0, 2000); } }
private void requestReloadingDialpads() { boolean isReloadTimeout = false; if (mHandler.hasMessages(RELOAD_DIALPADS_TIMEOUT)) { isReloadTimeout = true; mHandler.removeMessages(RELOAD_DIALPADS_TIMEOUT); } mHandler.sendEmptyMessageDelayed(RELOAD_DIALPADS_TIMEOUT, RELOAD_DIALPADS_DELAYTIME); loge("isReloadTimeout=" + isReloadTimeout); mHandler.removeMessages(RELOAD_DIALPADS); mHandler.sendEmptyMessageDelayed( RELOAD_DIALPADS, isReloadTimeout ? RELOAD_DIALPADS_DELAYTIME : 0); }
private void batch(BitmapHunter hunter) { if (hunter.isCancelled()) { return; } if (useBatch) { batch.add(hunter); if (!handler.hasMessages(HUNTER_DELAY_NEXT_BATCH)) { handler.sendEmptyMessageDelayed(HUNTER_DELAY_NEXT_BATCH, BATCH_DELAY); } } else { mainThreadHandler.sendMessage(mainThreadHandler.obtainMessage(REQUEST_COMPLETE, hunter)); } }
@Test public void testSendToTarget() { ShadowLooper.pauseMainLooper(); Object testObject = new Object(); Handler handler = new Handler(); Message message = handler.obtainMessage(123, testObject); assertThat(handler).isEqualTo(message.getTarget()); message.sendToTarget(); assertTrue(handler.hasMessages(123, testObject)); }
@Test public void testSendMessageAtFrontOfQueueThenRunMainLooperOneMsgAtATime_shouldRunFrontOfQueueMsgFirst() throws Exception { Handler handler = new Handler(); ShadowLooper.pauseMainLooper(); // Post two messages to handler. Handle first message and confirm that msg posted // to front is removed. handler.obtainMessage(123).sendToTarget(); Message frontMsg = handler.obtainMessage(345); boolean result = handler.sendMessageAtFrontOfQueue(frontMsg); assertTrue(result); assertTrue(handler.hasMessages(123)); assertTrue(handler.hasMessages(345)); ShadowLooper.runMainLooperOneTask(); assertTrue(handler.hasMessages(123)); assertFalse(handler.hasMessages(345)); ShadowLooper.runMainLooperOneTask(); assertFalse(handler.hasMessages(123)); assertFalse(handler.hasMessages(345)); }
private void hideZoomLevelInTime() { if (!showUIHandler.hasMessages(SHOW_ZOOM_LEVEL_MSG_ID)) { Message msg = Message.obtain( showUIHandler, new Runnable() { @Override public void run() { showZoomLevel = false; view.refreshMap(); } }); msg.what = SHOW_ZOOM_LEVEL_MSG_ID; showUIHandler.sendMessageDelayed(msg, SHOW_ZOOM_LEVEL_DELAY); } }
// If the Camera is idle, update the parameters immediately, otherwise // accumulate them in mUpdateSet and update later. private void setCameraParametersWhenIdle(int additionalUpdateSet) { mUpdateSet |= additionalUpdateSet; if (mCameraDevice == null) { // We will update all the parameters when we open the device, so // we don't need to do anything now. mUpdateSet = 0; return; } else if (isCameraIdle()) { setCameraParameters(mUpdateSet); mUpdateSet = 0; } else { if (!mHandler.hasMessages(SET_CAMERA_PARAMETERS_WHEN_IDLE)) { mHandler.sendEmptyMessageDelayed(SET_CAMERA_PARAMETERS_WHEN_IDLE, 1000); } } }
public void handleMessage(android.os.Message msg) { if (mHandler.hasMessages(1) && !flag) { // 过滤掉第一条消息 mHandler.removeMessages(1); flag = false; } switch (msg.what) { case 1: currentPager++; mViewPager.setCurrentItem(currentPager); mHandler.sendEmptyMessageDelayed(1, 2000); break; case 2: currentPager = msg.arg1; break; } }
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER && app.getInternalAPI().accessibilityEnabled()) { if (!uiHandler.hasMessages(LONG_KEYPRESS_MSG_ID)) { Message msg = Message.obtain( uiHandler, new Runnable() { @Override public void run() { app.getLocationProvider().emitNavigationHint(); } }); msg.what = LONG_KEYPRESS_MSG_ID; uiHandler.sendMessageDelayed(msg, LONG_KEYPRESS_DELAY); } return true; } else if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) { mapActions.openOptionsMenuAsList(); return true; } else if (keyCode == KeyEvent.KEYCODE_SEARCH && event.getRepeatCount() == 0) { Intent newIntent = new Intent(MapActivity.this, OsmandIntents.getSearchActivity()); // causes wrong position caching: newIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); LatLon loc = getMapLocation(); newIntent.putExtra(SearchActivity.SEARCH_LAT, loc.getLatitude()); newIntent.putExtra(SearchActivity.SEARCH_LON, loc.getLongitude()); startActivity(newIntent); newIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); return true; } else if (!app.getRoutingHelper().isFollowingMode() && OsmandPlugin.getEnabledPlugin(AccessibilityPlugin.class) != null) { // Find more appropriate plugin for it? if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && event.getRepeatCount() == 0) { if (mapView.isZooming()) { changeZoom(mapView.getZoom() + 2); } else { changeZoom(mapView.getZoom() + 1); } return true; } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN && event.getRepeatCount() == 0) { changeZoom(mapView.getZoom() - 1); return true; } } return super.onKeyDown(keyCode, event); }