Example usage for android.os Handler Handler

List of usage examples for android.os Handler Handler

Introduction

In this page you can find the example usage for android.os Handler Handler.

Prototype

public Handler() 

Source Link

Document

Default constructor associates this handler with the Looper for the current thread.

Usage

From source file:Main.java

public static void setSoftInputVisible(final View content, boolean visible, int delay) {
    final InputMethodManager imm = (InputMethodManager) content.getContext()
            .getSystemService(Context.INPUT_METHOD_SERVICE);

    if (visible) {
        if (delay > 0) {
            new Handler().postDelayed(new Runnable() {

                @Override//from   w w  w  .j a va 2s  .c o m
                public void run() {
                    imm.showSoftInput(content, 0);
                }
            }, delay);
        } else {
            imm.showSoftInput(content, 0);
        }

    } else {
        if (delay > 0) {
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    imm.hideSoftInputFromWindow(content.getWindowToken(), 0);
                }
            }, delay);
        } else {
            imm.hideSoftInputFromWindow(content.getWindowToken(), 0);
        }

    }
}

From source file:Main.java

private static void setDelayedClickable(final View v, final boolean clickable, int delayMillis) {
    new Handler().postDelayed(() -> {
        v.setClickable(clickable);//from w w w  .  j  a  va 2 s.  c o  m
        v.setEnabled(true);
    }, delayMillis); //
}

From source file:Main.java

public static void openKeyboard(final Context context, final EditText editText) {
    new Handler().postDelayed(new Runnable() {
        @Override// w ww . j a  v  a 2  s.co  m
        public void run() {
            editText.requestFocus();
            editText.setSelection(editText.getText().toString().length());
            InputMethodManager imm = (InputMethodManager) context
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
        }
    }, 300);
}

From source file:Main.java

public static void dispatchTouch(final View view, final long duration) {
    final long downTime = SystemClock.uptimeMillis();
    final long eventTime = SystemClock.uptimeMillis();
    final float x = view.getWidth() / 3;//0.0f;
    final float y = view.getHeight() / 3;//0.0f;
    // List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
    final int metaState = 0;
    MotionEvent motionEvent = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x, y, metaState);

    // Dispatch touch event to view
    view.dispatchTouchEvent(motionEvent);

    new Handler().postDelayed(new Runnable() {
        @Override/*  w  w  w  . j a  va 2s .co  m*/
        public void run() {
            MotionEvent motionEvent = MotionEvent.obtain(downTime + duration, eventTime + duration,
                    MotionEvent.ACTION_UP, x, y, metaState);
            view.dispatchTouchEvent(motionEvent);
        }
    }, duration);
}

From source file:Main.java

public static void acquireTemporaryWakelocks(Context context, long timeout) {
    if (wakeLock == null) {
        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP
                | PowerManager.ON_AFTER_RELEASE, "PushSMS");
    }//from www  .  j  a  v  a  2  s .c o m
    wakeLock.acquire(timeout);

    if (wifiLock == null) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        wifiLock = wifiManager.createWifiLock("PushSMS");
    }

    wifiLock.acquire();
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            wifiLock.release();
        }
    }, timeout);
}

From source file:Main.java

private static AlertDialog buildErrorDialog(final Activity context, String title, String description,
        String buttonText) {//from www.  jav  a  2 s  . c o  m

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle(title);
    builder.setMessage(description);
    builder.setCancelable(false);
    builder.setPositiveButton(buttonText, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(final DialogInterface dialog, int which) {
            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    dialog.dismiss();
                    context.finish();
                }
            });
        }
    });

    return builder.create();
}

From source file:MainActivity.java

public void startProgress(View view) {
    mDialog = new ProgressDialog(this);
    mDialog.setMessage("Doing something...");
    mDialog.setCancelable(false);/*from ww  w . j av  a 2  s . com*/
    mDialog.show();
    new Handler().postDelayed(new Runnable() {
        public void run() {
            mDialog.dismiss();
        }
    }, THIRTY_SECONDS);
}

From source file:com.wahyuadityanugraha.mvpexample.app.finditems.FeedInteractorImpl.java

@Override
public void findItems(final OnFinishedListener listener) {
    new Handler().postDelayed(new Runnable() {
        @Override/*from w  ww  .  j a  v  a  2 s  .  co  m*/
        public void run() {

        }
    }, 2000);
}

From source file:com.demo.koolcloud.mvptest.interactors.LoginInteractorImpl.java

@Override
public void login(final String username, final String password, final OnLoginFinishedListener listener) {
    // Mock login. I'm creating a handler to delay the answer a couple of seconds
    new Handler().postDelayed(new Runnable() {
        @Override/* w ww . j  ava 2  s.  c om*/
        public void run() {
            Log.i("LoginInteractorImpl", "login");
            boolean error = false;
            if (TextUtils.isEmpty(username)) {
                listener.onUsernameError();
                error = true;
            }
            if (TextUtils.isEmpty(password)) {
                listener.onPasswordError();
                error = true;
            }
            if (!error) {
                SmartPosServices service = SmartPosServices.getInstance(App.getContext());
                JSONObject loginResult = service.login("teddy", "123456", "999290053110041");
                Log.d("LoginInteractorImpl", loginResult.toString());

                JSONObject jsonData = JsonUtil.getResponseData(loginResult);
                if (null != jsonData) {
                    int responseCode = jsonData.optInt("responseCode");
                    if (responseCode == 0) {
                        listener.onSuccess();
                    } else {
                        listener.onError();
                        ;
                    }
                }
            }
        }
    }, 500);
}

From source file:net.niyonkuru.koodroid.webview.BlockingWebView.java

private BlockingWebView(Context context) {
    super(context);

    mHandler = new Handler();
}