Example usage for android.os SystemClock sleep

List of usage examples for android.os SystemClock sleep

Introduction

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

Prototype

public static void sleep(long ms) 

Source Link

Document

Waits a given number of milliseconds (of uptimeMillis) before returning.

Usage

From source file:Main.java

/**
 * TODO tyc//from   ww w  .  j  a  v a  2  s . c o  m
 * Sleep for a while, without dealing with the exception.
 *
 * @param time The time to sleep, in milliseconds.
 */
public static void sleep(long time) {
    SystemClock.sleep(time);
}

From source file:Main.java

/**
 * @param s/*from   www.  j  a  v a  2s.  c om*/
 * @param b
 * @param startIdx
 * @param numBytes
 * @param timeout
 * @return
 * @throws IOException
 */
public static int inputStreamTimedRead(InputStream s, byte[] b, int startIdx, int numBytes, int timeout)
        throws IOException {
    int retval = -1;

    int i = 0;
    for (i = 0; i < timeout; i++) {
        if (s.available() > 0) {
            retval = s.read(b, startIdx, numBytes);
            break;
        }
        SystemClock.sleep(1000);
    }
    return retval;
}

From source file:com.commonsware.android.localcast.NoticeService.java

@Override
protected void onHandleIntent(Intent intent) {
    SystemClock.sleep(5000);
    LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
}

From source file:com.commonsware.android.prognotify.SillyService.java

@Override
protected void onHandleIntent(Intent intent) {
    NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    builder.setTicker(getText(R.string.ticker)).setContentTitle(getString(R.string.progress_notification))
            .setContentText(getString(R.string.busy)).setContentIntent(buildContentIntent())
            .setSmallIcon(R.drawable.ic_stat_notif_small_icon).setOngoing(true);

    for (int i = 0; i < 20; i++) {
        builder.setProgress(20, i, false);
        mgr.notify(NOTIFICATION_ID, builder.build());

        SystemClock.sleep(1000);
    }/*from  w ww.j a v  a  2 s . com*/

    builder.setContentText(getString(R.string.done)).setProgress(0, 0, false).setOngoing(false);

    mgr.notify(NOTIFICATION_ID, builder.build());
}

From source file:com.manning.androidhacks.hack021.MyService.java

@Override
protected void onHandleIntent(Intent intent) {
    SystemClock.sleep(5000L);
    Intent broadcast = new Intent(ACTION);
    broadcast.putExtra(MSG_KEY, new Date().toGMTString());
    sendBroadcast(broadcast);/*  w w  w.ja v a  2s . c o m*/
}

From source file:com.commonsware.android.hcnotify.SillyService.java

@Override
protected void onHandleIntent(Intent intent) {
    NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    builder.setContent(buildContent(0)).setTicker(getText(R.string.ticker), buildTicker())
            .setContentIntent(buildContentIntent()).setLargeIcon(buildLargeIcon())
            .setSmallIcon(R.drawable.ic_stat_notif_small_icon).setOngoing(true);

    Notification notif = builder.build();

    for (int i = 0; i < 20; i++) {
        notif.contentView.setProgressBar(android.R.id.progress, 100, i * 5, false);
        mgr.notify(NOTIFICATION_ID, notif);

        if (i == 0) {
            notif.tickerText = null;/* w ww. j  a  v  a 2  s  .c  o  m*/
            notif.tickerView = null;
        }

        SystemClock.sleep(1000);
    }

    mgr.cancel(NOTIFICATION_ID);
}

From source file:com.frostwire.android.MediaScanner.java

private static void scanFiles(final Context context, List<String> paths, int retries) {
    if (paths.size() == 0) {
        return;/*from w  w w  .ja  v a 2  s  .  c  o  m*/
    }

    LOG.info("About to scan files n: " + paths.size() + ", retries: " + retries);

    final LinkedList<String> failedPaths = new LinkedList<>();

    final CountDownLatch finishSignal = new CountDownLatch(paths.size());

    MediaScannerConnection.scanFile(context, paths.toArray(new String[0]), null, (path, uri) -> {
        try {
            boolean success = true;
            if (uri == null) {
                success = false;
                failedPaths.add(path);
            } else {
                // verify the stored size four faulty scan
                long size = getSize(context, uri);
                if (size == 0) {
                    LOG.warn("Scan returned an uri but stored size is 0, path: " + path + ", uri:" + uri);
                    success = false;
                    failedPaths.add(path);
                }
            }
            if (!success) {
                LOG.info("Scan failed for path: " + path + ", uri: " + uri);
            }
        } finally {
            finishSignal.countDown();
        }
    });

    try {
        finishSignal.await(10, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        // ignore
    }

    if (failedPaths.size() > 0 && retries > 0) {
        // didn't want to do this, but there is a serious timing issue with the SD
        // and storage in general
        SystemClock.sleep(2000);
        scanFiles(context, failedPaths, retries - 1);
    }
}

From source file:edu.cmu.cs.quiltview.RequestPullingService.java

@Override
protected void onHandleIntent(Intent intent) {
    //String macAddr = null;
    //Log.i(LOG_TAG, "Got mac address: " + macAddr);
    mSerialNumber = android.os.Build.SERIAL;
    Log.i(LOG_TAG, "Got serial number: " + mSerialNumber);

    Log.i(LOG_TAG, "Handling new intent.");

    int count = 0;
    while (InfiniteLoop || (count < PullRequestLimit)) {
        SystemClock.sleep(3000); // 3 seconds
        pullRequest();/*from   w  w  w. j a v  a  2  s  . co m*/
        count++;
    }

}

From source file:com.hippoapp.asyncmvp.http.RetryHandler.java

@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
    boolean retry;

    Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = (b != null && b.booleanValue());

    if (executionCount > DEFAULT_MAX_RETRIES) {
        retry = false;//w  w w .  j  a  v  a 2  s.c  o m
    } else if (sUnretriedExceptionSet.contains(exception.getClass())) {
        retry = false;
    } else if (sRetriedExceptionSet.contains(exception.getClass())) {
        retry = true;
    } else if (!sent) {
        // for most other errors, retry only if request hasn't been fully
        // sent yet
        retry = true;
    } else {
        // resend all idempotent requests
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        String requestType = currentReq.getMethod();
        if (!requestType.equals("POST")) {
            retry = true;
        } else {
            // otherwise do not retry
            retry = false;
        }
    }

    if (retry) {
        SystemClock.sleep(RETRY_SLEEP_TIME_IN_MILLS);
    } else {
        exception.printStackTrace();
    }

    return retry;
}

From source file:cn.com.dfc.pl.afinal.http.RetryHandler.java

@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
    boolean retry = true;

    Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = (b != null && b.booleanValue());

    if (executionCount > maxRetries) {
        // ?5/*from w  w  w  .  j a  v a 2 s .c  o m*/
        retry = false;
    } else if (exceptionBlacklist.contains(exception.getClass())) {
        // ??
        retry = false;
    } else if (exceptionWhitelist.contains(exception.getClass())) {
        retry = true;
    } else if (!sent) {
        retry = true;
    }

    if (retry) {
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        retry = currentReq != null && !"POST".equals(currentReq.getMethod());
    }

    if (retry) {
        //1???
        SystemClock.sleep(RETRY_SLEEP_TIME_MILLIS);
    } else {
        exception.printStackTrace();
    }

    return retry;
}