/* * Copyright (C) 2006 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.os; /** * Core timekeeping facilities. * *

Three different clocks are available, and they should not be confused: * *

* * There are several mechanisms for controlling the timing of events: * * */ public final class SystemClock { /** * This class is uninstantiable. */ private SystemClock() { // This space intentionally left blank. } /** * Waits a given number of milliseconds (of uptimeMillis) before returning. * Similar to {@link java.lang.Thread#sleep(long)}, but does not throw * {@link InterruptedException}; {@link Thread#interrupt()} events are * deferred until the next interruptible operation. Does not return until * at least the specified number of milliseconds has elapsed. * * @param ms to sleep before returning, in milliseconds of uptime. */ public static void sleep(long ms) { long start = uptimeMillis(); long duration = ms; boolean interrupted = false; do { try { Thread.sleep(duration); } catch (InterruptedException e) { interrupted = true; } duration = start + ms - uptimeMillis(); } while (duration > 0); if (interrupted) { // Important: we don't want to quietly eat an interrupt() event, // so we make sure to re-interrupt the thread so that the next // call to Thread.sleep() or Object.wait() will be interrupted. Thread.currentThread().interrupt(); } } /** * Sets the current wall time, in milliseconds. Requires the calling * process to have appropriate permissions. * * @return if the clock was successfully set to the specified time. */ native public static boolean setCurrentTimeMillis(long millis); /** * Returns milliseconds since boot, not counting time spent in deep sleep. * Note: This value may get reset occasionally (before it would * otherwise wrap around). * * @return milliseconds of non-sleep uptime since boot. */ native public static long uptimeMillis(); /** * Returns milliseconds since boot, including time spent in sleep. * * @return elapsed milliseconds since boot. */ native public static long elapsedRealtime(); /** * Returns nanoseconds since boot, including time spent in sleep. * * @return elapsed nanoseconds since boot. */ public static native long elapsedRealtimeNanos(); /** * Returns milliseconds running in the current thread. * * @return elapsed milliseconds in the thread */ public static native long currentThreadTimeMillis(); /** * Returns microseconds running in the current thread. * * @return elapsed microseconds in the thread * * @hide */ public static native long currentThreadTimeMicro(); /** * Returns current wall time in microseconds. * * @return elapsed microseconds in wall time * * @hide */ public static native long currentTimeMicro(); }