Java Code Examples for android.os.Handler#sendMessageDelayed()

The following examples show how to use android.os.Handler#sendMessageDelayed() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChronometerNotificationThread.java    From ClockPlus with GNU General Public License v3.0 6 votes vote down vote up
@SuppressLint("HandlerLeak")
@Override
protected void onLooperPrepared() {
    // This is called after the looper has completed initializing, but before
    // it starts looping through its message queue. Right now, there is no
    // message queue, so this is the place to create it.
    // By default, the constructor associates this handler with this thread's looper.
    mHandler = new Handler() {
        @Override
        public void handleMessage(Message m) {
            updateNotification(true);
            sendMessageDelayed(Message.obtain(this, MSG_WHAT), 1000);
        }
    };
    // Once the handler is initialized, we may immediately begin our work.
    mHandler.sendMessageDelayed(Message.obtain(mHandler, MSG_WHAT), 1000);
}
 
Example 2
Source File: Utils.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
/**
 * Prior to Android 5, HandlerThread always keeps a stack local reference to the last message
 * that was sent to it. This method makes sure that stack local reference never stays there
 * for too long by sending new messages to it every second.
 */
static void flushStackLocalLeaks(Looper looper) {
  Handler handler = new Handler(looper) {
    @Override public void handleMessage(Message msg) {
      sendMessageDelayed(obtainMessage(), THREAD_LEAK_CLEANING_MS);
    }
  };
  handler.sendMessageDelayed(handler.obtainMessage(), THREAD_LEAK_CLEANING_MS);
}
 
Example 3
Source File: HandlerUtils.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * Enqueue a message into the message queue after all pending messages before (current time + delayMillis).
 * The message contains what value and a bundle with key and a int value.
 * @param handler
 * @param what
 * @param key
 * @param value
 * @param delayTime
 */
public static void sendMessageHandlerDelay(Handler handler, int what, String key, int value, long delayTime) {
    Message message = new Message();
    message.what = what;
    Bundle bundle = new Bundle();
    bundle.putInt(key, value);
    message.setData(bundle);
    // handler.sendMessage(message);
    handler.sendMessageDelayed(message, delayTime);
}
 
Example 4
Source File: HandlerUtils.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * Enqueue a message into the message queue after all pending messages before (current time + delayMillis).
 * The message contains what value and a bundle with key and a String value.
 * @param handler
 * @param what
 * @param key
 * @param value
 * @param delayTime
 */
public static void sendMessageHandlerDelay(Handler handler, int what, String key, String value, long delayTime) {
    Message message = new Message();
    message.what = what;
    Bundle bundle = new Bundle();
    bundle.putString(key, value);
    message.setData(bundle);
    // handler.sendMessage(message);
    handler.sendMessageDelayed(message, delayTime);
}
 
Example 5
Source File: HandlerUtils.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
public static void sendMessageHandlerDelay(Handler handler, int what, String key, int value, long delayTime) {
    Message message = new Message();
    message.what = what;
    Bundle bundle = new Bundle();
    bundle.putInt(key, value);
    message.setData(bundle);
    // handler.sendMessage(message);
    handler.sendMessageDelayed(message, delayTime);
}
 
Example 6
Source File: HandlerUtils.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
public static void sendMessageHandlerDelay(Handler handler, int what, String key, String value, long delayTime) {
    Message message = new Message();
    message.what = what;
    Bundle bundle = new Bundle();
    bundle.putString(key, value);
    message.setData(bundle);
    // handler.sendMessage(message);
    handler.sendMessageDelayed(message, delayTime);
}
 
Example 7
Source File: HandlerUtils.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * Enqueue a message into the message queue after all pending messages before (current time + delayMillis).
 * The message contains what value and a bundle with key and a int value.
 * @param handler
 * @param what
 * @param key
 * @param value
 * @param delayTime
 */
public static void sendMessageHandlerDelay(Handler handler, int what, String key, int value, long delayTime) {
    Message message = new Message();
    message.what = what;
    Bundle bundle = new Bundle();
    bundle.putInt(key, value);
    message.setData(bundle);
    // handler.sendMessage(message);
    handler.sendMessageDelayed(message, delayTime);
}
 
Example 8
Source File: HandlerUtils.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * Enqueue a message into the message queue after all pending messages before (current time + delayMillis).
 * The message contains what value and a bundle with key and a String value.
 * @param handler
 * @param what
 * @param key
 * @param value
 * @param delayTime
 */
public static void sendMessageHandlerDelay(Handler handler, int what, String key, String value, long delayTime) {
    Message message = new Message();
    message.what = what;
    Bundle bundle = new Bundle();
    bundle.putString(key, value);
    message.setData(bundle);
    // handler.sendMessage(message);
    handler.sendMessageDelayed(message, delayTime);
}
 
Example 9
Source File: CosuUtils.java    From android-testdpc with Apache License 2.0 5 votes vote down vote up
public static Long startDownload(DownloadManager dm, Handler handler, String location) {
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(location));
    Long id = dm.enqueue(request);
    handler.sendMessageDelayed(handler.obtainMessage(MSG_DOWNLOAD_TIMEOUT, id),
            DOWNLOAD_TIMEOUT_MILLIS);
    if (DEBUG) Log.d(TAG, "Starting download: DownloadId=" + id);
    return id;
}
 
Example 10
Source File: Utils.java    From picasso with Apache License 2.0 5 votes vote down vote up
/**
 * Prior to Android 5, HandlerThread always keeps a stack local reference to the last message
 * that was sent to it. This method makes sure that stack local reference never stays there
 * for too long by sending new messages to it every second.
 */
static void flushStackLocalLeaks(Looper looper) {
  Handler handler = new Handler(looper) {
    @Override public void handleMessage(Message msg) {
      sendMessageDelayed(obtainMessage(), THREAD_LEAK_CLEANING_MS);
    }
  };
  handler.sendMessageDelayed(handler.obtainMessage(), THREAD_LEAK_CLEANING_MS);
}
 
Example 11
Source File: AwarenessImpl.java    From Myna with Apache License 2.0 5 votes vote down vote up
@Override
public void init(Context context) {
    client = new GoogleApiClient.Builder(context)
            .addApi(Awareness.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    client.connect();

    HandlerThread googleHandleThread = new HandlerThread("googleHandleThread");
    googleHandleThread.start();
    googleHandler = new Handler(googleHandleThread.getLooper()){
        @Override
        public void handleMessage(Message msg) {
            Awareness.SnapshotApi.getDetectedActivity(client)
                    .setResultCallback(new ResultCallback<DetectedActivityResult>(){
                        @Override
                        public void onResult(@NonNull DetectedActivityResult detectedActivityResult) {
                            handleResult(detectedActivityResult);
                        }
                    });
            Message newMsg = new Message();
            newMsg.what = 0;
            googleHandler.sendMessageDelayed(newMsg, 5000);
        }
    };
}
 
Example 12
Source File: OnDoubleClickListener.java    From sctalk with Apache License 2.0 5 votes vote down vote up
/**
 * Called when a touch event is dispatched to a view. This allows listeners to
 * get a chance to respond before the target view.
 *
 * @param v     The view the touch event has been dispatched to.
 * @param event The MotionEvent object containing full information about
 *              the event.
 * @return True if the listener has consumed the event, false otherwise.
 */
@Override
public boolean onTouch(final View v, MotionEvent event) {
    if (MotionEvent.ACTION_DOWN == event.getAction()) {
        count++;
        if (count == 1) {
            Handler handler = new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    switch (msg.what){
                        case HANDLER_ON_CLICK:{
                            if(count == 1){
                                count = 0;
                                onClick(v);
                            }

                        }break;
                    }
                }
            };

            Message m = Message.obtain(handler);
            m.what = HANDLER_ON_CLICK;
            handler.sendMessageDelayed(m, 200);
            /**事件没有被消费完*/
            return false;
        } else if (count == 2) {
            count = 0;
            onDoubleClick(v);
            /**事件不再往下面传递*/
            return true;
        }
    }
    return false;
}
 
Example 13
Source File: Utils.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
/**
 * Prior to Android 5, HandlerThread always keeps a stack local reference to the last message
 * that was sent to it. This method makes sure that stack local reference never stays there
 * for too long by sending new messages to it every second.
 */
static void flushStackLocalLeaks(Looper looper) {
  Handler handler = new Handler(looper) {
    @Override public void handleMessage(Message msg) {
      sendMessageDelayed(obtainMessage(), THREAD_LEAK_CLEANING_MS);
    }
  };
  handler.sendMessageDelayed(handler.obtainMessage(), THREAD_LEAK_CLEANING_MS);
}
 
Example 14
Source File: BaseFragment.java    From mobile-manager-tool with MIT License 4 votes vote down vote up
protected void sendMessageDelayed(Handler handler,int what, Object message, int delay){
    Message msg = Message.obtain();
    msg.obj = message;
    msg.what = what;
    handler.sendMessageDelayed(msg, delay);
}
 
Example 15
Source File: LoaderActivity.java    From EasyVPN-Free with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_loader);

    progressBar = (NumberProgressBar)findViewById(R.id.number_progress_bar);
    commentsText = (TextView)findViewById(R.id.commentsText);

    if (getIntent().getBooleanExtra("firstPremiumLoad", false))
        ((TextView)findViewById(R.id.loaderPremiumText)).setVisibility(View.VISIBLE);

    progressBar.setMax(100);

    updateHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.arg1) {
                case LOAD_ERROR: {
                    commentsText.setText(msg.arg2);
                    progressBar.setProgress(100);
                } break;
                case DOWNLOAD_PROGRESS: {
                    commentsText.setText(R.string.downloading_csv_text);
                    progressBar.setProgress(msg.arg2);

                } break;
                case PARSE_PROGRESS: {
                    commentsText.setText(R.string.parsing_csv_text);
                    progressBar.setProgress(msg.arg2);
                } break;
                case LOADING_SUCCESS: {
                    commentsText.setText(R.string.successfully_loaded);
                    progressBar.setProgress(100);
                    Message end = new Message();
                    end.arg1 = SWITCH_TO_RESULT;
                    updateHandler.sendMessageDelayed(end,500);
                } break;
                case SWITCH_TO_RESULT: {
                    if (!BuildConfig.DEBUG)
                        Answers.getInstance().logCustom(new CustomEvent("Time servers loading")
                            .putCustomAttribute("Time servers loading", stopwatch.getElapsedTime()));

                    if (PropertiesService.getConnectOnStart()) {
                        Server randomServer = getRandomServer();
                        if (randomServer != null) {
                            newConnecting(randomServer, true, true);
                        } else {
                            startActivity(new Intent(LoaderActivity.this, HomeActivity.class));
                        }
                    } else {
                        startActivity(new Intent(LoaderActivity.this, HomeActivity.class));
                    }
                }
            }
            return true;
        }
    });
    progressBar.setProgress(0);


}
 
Example 16
Source File: HandlerUtils.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
public static void sendMessageHandlerDelay(Handler handler, int what, long delayTime) {
    Message message = new Message();
    message.what = what;
    handler.sendMessageDelayed(message, delayTime);
}
 
Example 17
Source File: HandlerUtils.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
public static void sendMessageHandlerDelay(Handler handler, int what, Object obj, long delayTime) {
    Message message = new Message();
    message.what = what;
    message.obj = obj;
    handler.sendMessageDelayed(message, delayTime);
}
 
Example 18
Source File: ArcTipViewController.java    From timecat with Apache License 2.0 4 votes vote down vote up
private ArcTipViewController(Context application) {
        mContext = application;
        mWindowManager = (WindowManager) application.getSystemService(Context.WINDOW_SERVICE);

        MAX_LENGTH = DEFAULT_MAX_LENGTH * SPHelper.getFloat(Constants.FLOATVIEW_SIZE, 100) / 100f;
        MIN_LENGTH = DEFAULT_MIN_LENGTH * SPHelper.getFloat(Constants.FLOATVIEW_SIZE, 100) / 100f;

        int resourceId = application.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            mStatusBarHeight = application.getResources().getDimensionPixelSize(resourceId);
        }


        mainHandler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                synchronized (ArcTipViewController.this) {
                    switch (msg.what) {
                        case MOVETOEDGE:
                            int desX = (int) msg.obj;
                            if (desX == 0) {
                                layoutParams.x = (int) (layoutParams.x - density * 10);
                                if (layoutParams.x < 0) {
                                    layoutParams.x = 0;
                                }
                            } else {
                                layoutParams.x = (int) (layoutParams.x + density * 10);
                                if (layoutParams.x > desX) {
                                    layoutParams.x = desX;
                                }
                            }
                            updateViewPosition(layoutParams.x, layoutParams.y);
                            if (layoutParams.x != desX) {
                                mainHandler.sendMessageDelayed(mainHandler.obtainMessage(MOVETOEDGE, desX), 10);
                            } else {
                                isMovingToEdge = false;
                                if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
                                    SPHelper.save(Constants.FLOAT_VIEW_PORT_X, layoutParams.x);
                                    SPHelper.save(Constants.FLOAT_VIEW_PORT_Y, layoutParams.y);
                                } else {
                                    SPHelper.save(Constants.FLOAT_VIEW_LAND_X, layoutParams.x);
                                    SPHelper.save(Constants.FLOAT_VIEW_LAND_Y, layoutParams.y);
                                }
                            }
                            break;
                        case HIDETOEDGE:
                            if (layoutParams.x <= mScaledTouchSlop && ((layoutParams.gravity & (Gravity.TOP | Gravity.LEFT)) == (Gravity.TOP | Gravity.LEFT))) {
                                floatImageView.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.floatview_hide_left));
                            } else {
                                floatImageView.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.floatview_hide_right));
                            }
                            iconFloatView.setOnTouchListener(ArcTipViewController.this);
                            acrFloatView.setOnTouchListener(ArcTipViewController.this);
                            floatImageView.setContentDescription(mContext.getString(R.string.float_view_hide));
                            LinearLayout.LayoutParams layoutParams_ = (LinearLayout.LayoutParams) floatImageView.getLayoutParams();
                            layoutParams_.width = (int) (ViewUtil.dp2px(DEFAULT_MIN_WIDTH_HIDE) * SPHelper.getFloat(Constants.FLOATVIEW_SIZE, 100) / 100f);
                            layoutParams_.height = ViewUtil.dp2px(MIN_LENGTH);
                            layoutParams_.gravity = Gravity.NO_GRAVITY;
                            floatImageView.setLayoutParams(layoutParams_);
                            floatImageView.setPadding(0, 0, 0, 0);
                            //TODO 不贴边的问题
//                            layoutParams.width = (int) ViewUtil.dp2px(DEFAULT_MIN_WIDTH_HIDE);
                            reuseSavedWindowMangerPosition((int) (ViewUtil.dp2px(DEFAULT_MIN_WIDTH_HIDE) * SPHelper.getFloat(Constants.FLOATVIEW_SIZE, 100) / 100f), ViewUtil.dp2px(MIN_LENGTH));
                            updateViewPosition(layoutParams.x, layoutParams.y);

                            break;
                    }
                }
            }
        };

        mActionListener = new ArrayList<>();
        mScaledTouchSlop = (int) (ViewUtil.dp2px(DEFAULT_MIN_WIDTH_HIDE) * SPHelper.getFloat(Constants.FLOATVIEW_SIZE, 100) / 100f);
        initView();
        applySizeChange();
        isRemoved = true;
    }
 
Example 19
Source File: HandlerUtils.java    From UltimateAndroid with Apache License 2.0 3 votes vote down vote up
/**
 * Enqueue a message containing what value and object into the message queue after all pending messages before (current time + delayMillis).
 *
 * @param handler
 * @param what
 * @param obj
 * @param delayTime
 */
public static void sendMessageHandlerDelay(Handler handler, int what, Object obj, long delayTime) {
    Message message = new Message();
    message.what = what;
    message.obj = obj;
    handler.sendMessageDelayed(message, delayTime);
}
 
Example 20
Source File: HandlerUtils.java    From UltimateAndroid with Apache License 2.0 3 votes vote down vote up
/**
 * Enqueue a message containing what value and object into the message queue after all pending messages before (current time + delayMillis).
 *
 * @param handler
 * @param what
 * @param obj
 * @param delayTime
 */
public static void sendMessageHandlerDelay(Handler handler, int what, Object obj, long delayTime) {
    Message message = new Message();
    message.what = what;
    message.obj = obj;
    handler.sendMessageDelayed(message, delayTime);
}