Example usage for android.os Handler post

List of usage examples for android.os Handler post

Introduction

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

Prototype

public final boolean post(Runnable r) 

Source Link

Document

Causes the Runnable r to be added to the message queue.

Usage

From source file:com.air.mobilebrowser.ActivityWatchService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    final Handler handler = new Handler();

    // Create timer to fire every 500ms to handle security
    mTimer = new Timer();
    mTimer.scheduleAtFixedRate(new TimerTask() {
        @Override/*from ww w  .  j av  a 2 s  .c o m*/
        public void run() {
            // User handler to hide application and detect system changes
            handler.post(new Runnable() {
                @Override
                public void run() {
                    hideApplications();
                    checkSettings();
                }
            });
        }
    }, 0, 500);

    // We want this service to continue running until it is explicitly
    // stopped, so return sticky.
    return START_STICKY;
}

From source file:com.example.hellogooglemaps.MarkerDemoActivity.java

@Override
public boolean onMarkerClick(final Marker marker) {
    if (marker.equals(mPerth)) {
        // This causes the marker at Perth to bounce into position when it is clicked.
        final Handler handler = new Handler();
        final long start = SystemClock.uptimeMillis();
        final long duration = 1500;

        final Interpolator interpolator = new BounceInterpolator();

        handler.post(new Runnable() {
            @Override/*w ww .j a  v  a  2  s . c o  m*/
            public void run() {
                long elapsed = SystemClock.uptimeMillis() - start;
                float t = Math.max(1 - interpolator.getInterpolation((float) elapsed / duration), 0);
                marker.setAnchor(0.5f, 1.0f + 2 * t);

                if (t > 0.0) {
                    // Post again 16ms later.
                    handler.postDelayed(this, 16);
                }
            }
        });
    } else if (marker.equals(mAdelaide)) {
        // This causes the marker at Adelaide to change color.
        marker.setIcon(BitmapDescriptorFactory.defaultMarker(new Random().nextFloat() * 360));
    }
    // We return false to indicate that we have not consumed the event and that we wish
    // for the default behavior to occur (which is for the camera to move such that the
    // marker is centered and for the marker's info window to open, if it has one).
    return false;
}

From source file:com.ece420.lab3.MainActivity.java

public void initializeStftBackgroundThread(int timeInMs) {
    final Handler handler = new Handler();
    Timer timer = new Timer();
    TimerTask doAsynchronousTask = new TimerTask() {
        @Override//w ww .  j a va  2 s . c  om
        public void run() {
            handler.post(new Runnable() {
                public void run() {
                    try {
                        UpdateStftTask performStftUiUpdate = new UpdateStftTask();
                        performStftUiUpdate.execute();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                    }
                }
            });
        }
    };
    timer.schedule(doAsynchronousTask, 0, timeInMs); // execute every 50 ms
}

From source file:com.android.deskclock.alarms.AlarmStateManager.java

/**
 * This will set the alarm instance to the SNOOZE_STATE and update
 * the application notifications and schedule any state changes that need
 * to occur in the future./* w  w w .j ava 2s .c  o m*/
 *
 * @param context application context
 * @param instance to set state to
 *
 */
public static void setSnoozeState(final Context context, AlarmInstance instance, boolean showToast) {
    // Stop alarm if this instance is firing it
    AlarmService.stopAlarm(context, instance);

    // Calculate the new snooze alarm time
    String snoozeMinutesStr = Utils.getDefaultSharedPreferences(context)
            .getString(SettingsActivity.KEY_ALARM_SNOOZE, DEFAULT_SNOOZE_MINUTES);
    final int snoozeMinutes = Integer.parseInt(snoozeMinutesStr);
    Calendar newAlarmTime = Calendar.getInstance();
    newAlarmTime.add(Calendar.MINUTE, snoozeMinutes);

    // Update alarm state and new alarm time in db.
    LogUtils.i("Setting snoozed state to instance " + instance.mId + " for "
            + AlarmUtils.getFormattedTime(context, newAlarmTime));
    instance.setAlarmTime(newAlarmTime);
    instance.mAlarmState = AlarmInstance.SNOOZE_STATE;
    AlarmInstance.updateInstance(context.getContentResolver(), instance);

    // Setup instance notification and scheduling timers
    AlarmNotifications.showSnoozeNotification(context, instance);
    scheduleInstanceStateChange(context, instance.getAlarmTime(), instance, AlarmInstance.FIRED_STATE);

    // Display the snooze minutes in a toast.
    if (showToast) {
        final Handler mainHandler = new Handler(context.getMainLooper());
        final Runnable myRunnable = new Runnable() {
            @Override
            public void run() {
                String displayTime = String.format(context.getResources()
                        .getQuantityText(R.plurals.alarm_alert_snooze_set, snoozeMinutes).toString(),
                        snoozeMinutes);
                Toast.makeText(context, displayTime, Toast.LENGTH_LONG).show();
            }
        };
        mainHandler.post(myRunnable);
    }

    // Instance time changed, so find next alarm that will fire and notify system
    updateNextAlarm(context);
}

From source file:io.selendroid.ServerInstrumentation.java

private Executor provideMainThreadExecutor(Looper mainLooper) {
    final Handler handler = new Handler(mainLooper);
    return new Executor() {
        @Override//www .  j  ava2  s  . co m
        public void execute(Runnable runnable) {
            handler.post(runnable);
        }
    };
}

From source file:com.onyx.deskclock.deskclock.alarms.AlarmStateManager.java

/**
 * This will set the alarm instance to the SNOOZE_STATE and update
 * the application notifications and schedule any state changes that need
 * to occur in the future./*from   w  w w . j av  a  2s.  com*/
 *
 * @param context application context
 * @param instance to set state to
 *
 */
public static void setSnoozeState(final Context context, AlarmInstance instance, boolean showToast) {
    // Stop alarm if this instance is firing it
    AlarmService.stopAlarm(context, instance);

    // Calculate the new snooze alarm time
    String snoozeMinutesStr = PreferenceManager.getDefaultSharedPreferences(context)
            .getString(SettingsActivity.KEY_ALARM_SNOOZE, DEFAULT_SNOOZE_MINUTES);
    final int snoozeMinutes = Integer.parseInt(snoozeMinutesStr);
    Calendar newAlarmTime = Calendar.getInstance();
    newAlarmTime.add(Calendar.MINUTE, snoozeMinutes);

    // Update alarm state and new alarm time in db.
    LogUtils.i("Setting snoozed state to instance " + instance.mId + " for "
            + AlarmUtils.getFormattedTime(context, newAlarmTime));
    instance.setAlarmTime(newAlarmTime);
    instance.mAlarmState = AlarmInstance.SNOOZE_STATE;
    AlarmInstance.updateInstance(context.getContentResolver(), instance);

    // Setup instance notification and scheduling timers
    AlarmNotifications.showSnoozeNotification(context, instance);
    scheduleInstanceStateChange(context, instance.getAlarmTime(), instance, AlarmInstance.FIRED_STATE);

    // Display the snooze minutes in a toast.
    if (showToast) {
        final Handler mainHandler = new Handler(context.getMainLooper());
        final Runnable myRunnable = new Runnable() {
            @Override
            public void run() {
                String displayTime = String.format(context.getResources()
                        .getQuantityText(R.plurals.alarm_alert_snooze_set, snoozeMinutes).toString(),
                        snoozeMinutes);
                Toast.makeText(context, displayTime, Toast.LENGTH_LONG).show();
            }
        };
        mainHandler.post(myRunnable);
    }

    // Instance time changed, so find next alarm that will fire and notify system
    updateNextAlarm(context);
}

From source file:com.mobshep.mobileshepherd.BrokenCrypto.java

private void startTimerTwo() {
    final Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        public void run() {

            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();/*w w  w. j ava 2  s  . c o  m*/
            }
            handler.post(new Runnable() {
                public void run() {
                    messageTwo.setVisibility(View.VISIBLE);
                }
            });

        }
    };
    new Thread(runnable).start();
}

From source file:com.mobshep.mobileshepherd.BrokenCrypto.java

private void startTimerThree() {
    final Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        public void run() {

            try {
                Thread.sleep(6000);
            } catch (InterruptedException e) {
                e.printStackTrace();//from   w ww. j  a va2  s .  com
            }
            handler.post(new Runnable() {
                public void run() {
                    messageThree.setVisibility(View.VISIBLE);
                }
            });

        }
    };
    new Thread(runnable).start();
}

From source file:com.mobshep.mobileshepherd.BrokenCrypto.java

private void startTimerFour() {
    final Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        public void run() {

            try {
                Thread.sleep(8000);
            } catch (InterruptedException e) {
                e.printStackTrace();/*from   w w w .  j a  va 2  s. co m*/
            }
            handler.post(new Runnable() {
                public void run() {
                    messageFour.setVisibility(View.VISIBLE);
                }
            });

        }
    };
    new Thread(runnable).start();
}

From source file:com.mobshep.mobileshepherd.BrokenCrypto.java

private void startTimerOne() {
    final Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        public void run() {

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();//from w w  w.  j av  a  2s . c  o m
            }
            handler.post(new Runnable() {
                public void run() {
                    messageOne.setVisibility(View.VISIBLE);

                }
            });

        }
    };
    new Thread(runnable).start();
}