下面列出了android.os.Handler#removeCallbacks ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public TimeoutBase(final Handler handler, long delay) {
this.delay = delay;
this.handlerish = new Handlerish() {
@Override
public void post(Runnable r) {
handler.post(r);
}
@Override
public Object postDelayed(Runnable r, long delay) {
handler.postDelayed(r, delay);
return r;
}
@Override
public void removeAllCallbacks(Object cancellable) {
if (cancellable == null)
return;
handler.removeCallbacks((Runnable)cancellable);
}
};
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
{
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
try {
handler.removeCallbacks(runnable);
}
finally {
finish();
goToMainActivity();
handler.removeCallbacks(runnable);
}
}
};
handler.postDelayed(runnable, 2000);
}
}
private boolean onTouchUp(final MotionEvent event) {
switch (mTouchMode) {
case TOUCH_MODE_DOWN:
case TOUCH_MODE_TAP:
case TOUCH_MODE_DONE_WAITING:
return onTouchUpTap(event);
case TOUCH_MODE_SCROLLING:
return onTouchUpScrolling(event);
}
setPressed(false);
invalidate(); // redraw selector
final Handler handler = getHandler();
if (handler != null) {
handler.removeCallbacks(mPendingCheckForLongPress);
}
recycleVelocityTracker();
mActivePointerId = INVALID_POINTER;
return true;
}
@SuppressLint("InlinedApi")
private void setNavVisibility(boolean visible)
{
int newVis = SYSTEM_UI_FLAG_LAYOUT_STABLE
| SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
if (!visible)
newVis |= SYSTEM_UI_FLAG_FULLSCREEN | SYSTEM_UI_FLAG_IMMERSIVE | SYSTEM_UI_FLAG_HIDE_NAVIGATION;
final boolean changed = newVis == getSystemUiVisibility();
// Unschedule any pending event to hide navigation if we are
// changing the visibility, or making the UI visible.
if (changed || visible)
{
Handler h = getHandler();
if (h != null)
h.removeCallbacks(_navHider);
}
// Set the new desired visibility.
setSystemUiVisibility(newVis);
}
@PerFragment
@Provides
@IntoMap
@ListenerModuleKey(LiveDataFragment.class)
public OnItemViewSelectedListener provideOnItemViewSelectedListener(final Activity activity,
final DisplayMetrics metrics, final BackgroundManager backgroundManager,
final RequestOptions defaultPlaceHolder, final Drawable finalDrawable, final Handler mainHandler) {
return new OnItemViewSelectedListener() {
@Override
public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
RowPresenter.ViewHolder rowViewHolder, Row row) {
VideoEntity selectedVideo = (VideoEntity) item;
RunnableClass backgroundRunnable = new RunnableClass(selectedVideo, activity,
metrics, backgroundManager, defaultPlaceHolder, finalDrawable);
if (lastTime != null) {
mainHandler.removeCallbacks(lastTime);
}
mainHandler.postDelayed(backgroundRunnable, BACKGROUND_UPDATE_DELAY);
lastTime = backgroundRunnable;
}
};
}
/**
* Sets the handler that will be used to invoke callbacks. By default, the handler set in
* {@link BleManager} will be used.
*
* @param handler The handler to invoke callbacks for this request.
* @return The request.
*/
@NonNull
public Request setHandler(@NonNull final Handler handler) {
this.handler = new CallbackHandler() {
@Override
public void post(@NonNull final Runnable r) {
handler.post(r);
}
@Override
public void postDelayed(@NonNull final Runnable r, final long delayMillis) {
handler.postDelayed(r, delayMillis);
}
@Override
public void removeCallbacks(@NonNull final Runnable r) {
handler.removeCallbacks(r);
}
};
return this;
}
void scheduleProgressUpdater() {
Handler h = getHandler();
if (h != null) {
if (mNavVisible && !mPaused) {
h.removeCallbacks(mProgressUpdater);
h.post(mProgressUpdater);
} else {
h.removeCallbacks(mProgressUpdater);
}
}
}
private boolean startScrollIfNeeded( int x ) {
// Check if we have moved far enough that it looks more like a
// scroll than a tap
final int deltaX = x - mMotionX;
final int distance = Math.abs( deltaX );
final boolean overscroll = getScrollX() != 0;
if ( overscroll || distance > mTouchSlop ) {
createScrollingCache();
if ( overscroll ) {
mTouchMode = TOUCH_MODE_OVERSCROLL;
mMotionCorrection = 0;
} else {
mTouchMode = TOUCH_MODE_SCROLL;
mMotionCorrection = deltaX > 0 ? mTouchSlop : -mTouchSlop;
}
final Handler handler = getHandler();
// Handler should not be null unless the AbsListView is not attached to a
// window, which would make it very hard to scroll it... but the monkeys
// say it's possible.
if ( handler != null ) {
handler.removeCallbacks( mPendingCheckForLongPress );
}
setPressed( false );
View motionView = getChildAt( mMotionPosition - mFirstPosition );
if ( motionView != null ) {
motionView.setPressed( false );
}
reportScrollStateChange( OnScrollListener.SCROLL_STATE_TOUCH_SCROLL );
// Time to start stealing events! Once we've stolen them, don't let anyone
// steal from us
final ViewParent parent = getParent();
if ( parent != null ) {
parent.requestDisallowInterceptTouchEvent( true );
}
scrollIfNeeded( x );
return true;
}
return false;
}
private void init() {
inflate(getContext(), R.layout.view_record_button, this);
mAnalytics = Fa.get();
mStopButton = findViewById(R.id.red_square);
mRecordingBackground = findViewById(R.id.recording_background);
mProgressBar = findViewById(R.id.progress);
//
// set up timer
//
mHandler = new Handler();
mRecordingRunnable = new Runnable() {
@Override
public void run() {
long counterDuration = getCurrentCounterDuration();
mProgressBar.setCurrentDuration(counterDuration, MAX_DURATION);
if (counterDuration >= MAX_DURATION) {
setRecording(false);
mHandler.removeCallbacks(mRecordingRunnable);
} else if (mCounterRunning) {
mHandler.postDelayed(mRecordingRunnable, mTick);
}
}
};
}
/**
* Starts a scroll that moves the difference between y and our last motions y
* if it's a movement that represents a big enough scroll.
*/
private boolean startScrollIfNeeded(final int y) {
final int deltaY = y - mMotionY;
final int distance = Math.abs(deltaY);
// TODO : Overscroll?
// final boolean overscroll = mScrollY != 0;
final boolean overscroll = false;
if (overscroll || distance > mTouchSlop) {
if (overscroll) {
mMotionCorrection = 0;
}
else {
mTouchMode = TOUCH_MODE_SCROLLING;
mMotionCorrection = deltaY > 0 ? mTouchSlop : -mTouchSlop;
}
final Handler handler = getHandler();
if (handler != null) {
handler.removeCallbacks(mPendingCheckForLongPress);
}
setPressed(false);
View motionView = getChildAt(mMotionPosition - mFirstPosition);
if (motionView != null) {
motionView.setPressed(false);
}
final ViewParent parent = getParent();
if (parent != null) {
parent.requestDisallowInterceptTouchEvent(true);
}
scrollIfNeeded(y);
return true;
}
return false;
}
public boolean startScrollIfNeeded(int delta) {
// Check if we have moved far enough that it looks more like a
// scroll than a tap
final int distance = Math.abs(delta);
if (distance > mTouchSlop) {
createScrollingCache();
mTouchMode = TOUCH_MODE_SCROLL;
mMotionCorrection = delta;
final Handler handler = getHandler();
// Handler should not be null unless the TwoWayAbsListView is not attached to a
// window, which would make it very hard to scroll it... but the monkeys
// say it's possible.
if (handler != null) {
handler.removeCallbacks(mPendingCheckForLongPress);
}
setPressed(false);
View motionView = getChildAt(mMotionPosition - mFirstPosition);
if (motionView != null) {
motionView.setPressed(false);
}
reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
// Time to start stealing events! Once we've stolen them, don't let anyone
// steal from us
requestDisallowInterceptTouchEvent(true);
return true;
}
return false;
}
public void cancelDelayed(Class<?> eventClass, Handler handler) {
Task task = null;
synchronized (this) {
if (mDelayedTasks != null) {
task = mDelayedTasks.remove(eventClass);
}
}
if (task != null) {
handler.removeCallbacks(task);
task.recycle();
}
}
public Discoverer(Context ctx) {
mCtx = ctx;
mListeners = new ArrayList<>();
mHandler = new Handler(mCtx.getMainLooper());
mTimeoutRunnable = new Runnable() {
@Override
public void run() {
notifyDiscoveryTimedOut();
mHandler.removeCallbacks(mTimeoutRunnable);
}
};
initBroadcastReceiver();
}
public static void removeUiThreadRunnable(Runnable theRunnable) {
final Handler mainHandler = new Handler(xdrip.getAppContext().getMainLooper());
mainHandler.removeCallbacks(theRunnable);
}
boolean onTouchEvent(MotionEvent me) {
if (mState == STATE_NONE) {
return false;
}
final int action = me.getAction();
if (action == MotionEvent.ACTION_DOWN) {
if (isPointInside(me.getX(), me.getY())) {
setState(STATE_DRAGGING);
if (mSections == null ) {// Jota Text Editor
getSectionsFromIndexer();
}
if (mList != null) {
// Jota Text Editor
// mList.requestDisallowInterceptTouchEvent(true);
// mList.reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
}
cancelFling();
return true;
}
} else if (action == MotionEvent.ACTION_UP) { // don't add ACTION_CANCEL here
if (mState == STATE_DRAGGING) {
if (mList != null) {
// ViewGroup does the right thing already, but there might
// be other classes that don't properly reset on touch-up,
// so do this explicitly just in case.
// Jota Text Editor
// mList.requestDisallowInterceptTouchEvent(false);
// mList.reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
}
setState(STATE_VISIBLE);
final Handler handler = mHandler;
handler.removeCallbacks(mScrollFade);
handler.postDelayed(mScrollFade, 1000);
return true;
}
} else if (action == MotionEvent.ACTION_MOVE) {
if (mState == STATE_DRAGGING) {
long now = System.currentTimeMillis();
long diff = (now-mLastEventTime);
if ( diff > 30 ){
mLastEventTime = now;
final int viewHeight = mList.getHeight();
// Jitter
int newThumbY = (int) me.getY() - mThumbH / 2;// Jota Text Editor
if (newThumbY < 0) {
newThumbY = 0;
} else if (newThumbY + mThumbH > viewHeight) {
newThumbY = viewHeight - mThumbH;
}
if (Math.abs(mThumbY - newThumbY) < 2) {
return true;
}
mThumbY = newThumbY;
// Jota Text Editor
// // If the previous scrollTo is still pending
// if (mScrollCompleted) {
scrollTo((float) mThumbY / (viewHeight - mThumbH));
// }
}
return true;
}
}
return false;
}
public static void requestRoot(final Context context) {
// Don't request root when read logs permission is already granted
if (haveReadLogsPermission(context)) {
failedToObtainRoot = true;
return;
}
Handler handler = new Handler(Looper.getMainLooper());
Runnable toastRunnable = new Runnable() {
@Override
public void run() {
Toast.makeText(context, R.string.toast_request_root, Toast.LENGTH_LONG).show();
}
};
handler.postDelayed(toastRunnable, 200);
Process process = null;
try {
// Preform su to get root privileges
process = Runtime.getRuntime().exec("su");
// confirm that we have root
DataOutputStream outputStream = new DataOutputStream(process.getOutputStream());
outputStream.writeBytes("echo hello\n");
// Close the terminal
outputStream.writeBytes("exit\n");
outputStream.flush();
process.waitFor();
if (process.exitValue() != 0) {
showWarningDialog(context);
failedToObtainRoot = true;
} else {
// success
PreferenceHelper.setJellybeanRootRan(context);
}
} catch (IOException | InterruptedException e) {
log.w(e, "Cannot obtain root");
showWarningDialog(context);
failedToObtainRoot = true;
}
handler.removeCallbacks(toastRunnable);
}
public static void removeUiThreadRunnable(Runnable theRunnable) {
final Handler mainHandler = new Handler(xdrip.getAppContext().getMainLooper());
mainHandler.removeCallbacks(theRunnable);
}
private static void brightnessControl(MotionEvent event) {
try {
final int action = event.getAction();
final int x = (int) event.getRawX();
final int y = (int) event.getRawY();
Handler handler = (Handler) XposedHelpers.getObjectField(mPhoneStatusBar, "mHandler");
int statusBarHeaderHeight = (int) XposedHelpers.callMethod(mHeader, "getCollapsedHeight");
if (action == MotionEvent.ACTION_DOWN) {
if (y < statusBarHeaderHeight) {
mLinger = 0;
mInitialTouchX = x;
mInitialTouchY = y;
mJustPeeked = true;
mScreenWidth = (float) mContext.getResources().getDisplayMetrics().widthPixels;
handler.removeCallbacks(mLongPressBrightnessChange);
handler.postDelayed(mLongPressBrightnessChange,
BRIGHTNESS_CONTROL_LONG_PRESS_TIMEOUT);
}
} else if (action == MotionEvent.ACTION_MOVE) {
if (y < statusBarHeaderHeight && mJustPeeked) {
if (mLinger > BRIGHTNESS_CONTROL_LINGER_THRESHOLD) {
adjustBrightness(x);
} else {
final int xDiff = Math.abs(x - mInitialTouchX);
final int yDiff = Math.abs(y - mInitialTouchY);
final int touchSlop = ViewConfiguration.get(mContext).getScaledTouchSlop();
if (xDiff > yDiff) {
mLinger++;
}
if (xDiff > touchSlop || yDiff > touchSlop) {
handler.removeCallbacks(mLongPressBrightnessChange);
}
}
} else {
if (y > mPeekHeight) {
mJustPeeked = false;
}
handler.removeCallbacks(mLongPressBrightnessChange);
}
} else if (action == MotionEvent.ACTION_UP ||
action == MotionEvent.ACTION_CANCEL) {
handler.removeCallbacks(mLongPressBrightnessChange);
}
} catch (Throwable t) {
XposedBridge.log(t);
}
}
public static void requestRoot(final Context context) {
// Don't request root when read logs permission is already granted
if (haveReadLogsPermission(context)) {
failedToObtainRoot = true;
return;
}
Handler handler = new Handler(Looper.getMainLooper());
Runnable toastRunnable = new Runnable() {
@Override
public void run() {
Toast.makeText(context, R.string.toast_request_root, Toast.LENGTH_LONG).show();
}
};
handler.postDelayed(toastRunnable, 200);
Process process = null;
try {
// Preform su to get root privileges
process = Runtime.getRuntime().exec("su");
// confirm that we have root
DataOutputStream outputStream = new DataOutputStream(process.getOutputStream());
outputStream.writeBytes("echo hello\n");
// Close the terminal
outputStream.writeBytes("exit\n");
outputStream.flush();
process.waitFor();
if (process.exitValue() != 0) {
showWarningDialog(context);
failedToObtainRoot = true;
} else {
// success
PreferenceHelper.setJellybeanRootRan(context);
}
} catch (IOException | InterruptedException e) {
log.w(e, "Cannot obtain root");
showWarningDialog(context);
failedToObtainRoot = true;
}
handler.removeCallbacks(toastRunnable);
}
private void cropImage(final int originalImageWidth, final int originalImageHeight) {
final Handler messageHandler = mMessageHandler;
if (messageHandler == null) {
// We can't do anything right now.
return;
}
if (mCropRunnable != null) {
messageHandler.removeCallbacks(mCropRunnable);
}
if ((!mAutoFaceDetection && (mEndX == 0) && (mEndY == 0) && (mEndScale == 1)) || (
originalImageWidth <= 0) || (originalImageHeight <= 0)) {
final ScaleType scaleType = super.getScaleType();
final ScaleType originalScaleType = mOriginalScaleType;
if (scaleType != originalScaleType) {
super.setScaleType(originalScaleType);
}
return;
}
mCropRunnable = new CropRunnable(originalImageWidth, originalImageHeight);
if (Looper.getMainLooper() == Looper.myLooper()) {
mCropRunnable.run();
} else {
messageHandler.post(mCropRunnable);
}
}