Example usage for android.os HandlerThread start

List of usage examples for android.os HandlerThread start

Introduction

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

Prototype

public synchronized void start() 

Source Link

Document

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

Usage

From source file:Main.java

/**
 * Get a Handler that is running on a background thread.
 * NOTE!!! The HandlerThread that is passed as a param MUST be stopped via
 * handlerThread.quitSafely() or handlerThread.quit();
 * @param handlerThread {@link HandlerThread}Cannot be null since this will start
 *                                           the thread when it is passed.
 * @return {@link Handler}/*from  ww  w  . ja v a2s .c om*/
 */
public static Handler getBackgroundHandler(@NonNull HandlerThread handlerThread) {
    handlerThread.start();
    Handler handler = new Handler(handlerThread.getLooper());
    return handler;
}

From source file:com.owncloud.android.ui.notifications.NotificationUtils.java

public static void cancelWithDelay(final NotificationManager notificationManager, final int notificationId,
        long delayInMillis) {

    HandlerThread thread = new HandlerThread(
            "NotificationDelayerThread_" + (new Random(System.currentTimeMillis())).nextInt(),
            Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();

    Handler handler = new Handler(thread.getLooper());
    handler.postDelayed(new Runnable() {
        public void run() {
            notificationManager.cancel(notificationId);
            ((HandlerThread) Thread.currentThread()).getLooper().quit();
        }/* w ww  . j av  a2  s . com*/
    }, delayInMillis);

}

From source file:org.sufficientlysecure.keychain.service.PassphraseCacheService.java

/**
 * Gets a cached passphrase from memory by sending an intent to the service. This method is
 * designed to wait until the service returns the passphrase.
 *
 * @param context//from  w w w . ja  va 2  s  . co  m
 * @param keyId
 * @return passphrase or null (if no passphrase is cached for this keyId)
 */
public static String getCachedPassphrase(Context context, long keyId) {
    Log.d(TAG, "getCachedPassphrase() get masterKeyId for " + keyId);

    Intent intent = new Intent(context, PassphraseCacheService.class);
    intent.setAction(ACTION_PASSPHRASE_CACHE_GET);

    final Object mutex = new Object();
    final Bundle returnBundle = new Bundle();

    HandlerThread handlerThread = new HandlerThread("getPassphraseThread");
    handlerThread.start();
    Handler returnHandler = new Handler(handlerThread.getLooper()) {
        @Override
        public void handleMessage(Message message) {
            if (message.obj != null) {
                String passphrase = ((Bundle) message.obj).getString(EXTRA_PASSPHRASE);
                returnBundle.putString(EXTRA_PASSPHRASE, passphrase);
            }
            synchronized (mutex) {
                mutex.notify();
            }
            // quit handlerThread
            getLooper().quit();
        }
    };

    // Create a new Messenger for the communication back
    Messenger messenger = new Messenger(returnHandler);
    intent.putExtra(EXTRA_KEY_ID, keyId);
    intent.putExtra(EXTRA_MESSENGER, messenger);
    // send intent to this service
    context.startService(intent);

    // Wait on mutex until passphrase is returned to handlerThread
    synchronized (mutex) {
        try {
            mutex.wait(3000);
        } catch (InterruptedException e) {
        }
    }

    if (returnBundle.containsKey(EXTRA_PASSPHRASE)) {
        return returnBundle.getString(EXTRA_PASSPHRASE);
    } else {
        return null;
    }
}

From source file:com.musenkishi.wally.util.PaletteLoader.java

private static void setupHandlers(Context context) {
    HandlerThread handlerThread = new HandlerThread("palette-loader-background");
    handlerThread.start();
    backgroundHandler = new Handler(handlerThread.getLooper(), sCallback);
    uiHandler = new Handler(context.getMainLooper(), sCallback);
}

From source file:com.musenkishi.atelier.Atelier.java

private static void setupHandlers(Context context) {
    HandlerThread handlerThread = new HandlerThread("palette-loader-background");
    handlerThread.start();
    backgroundHandler = new Handler(handlerThread.getLooper(), callback);
    uiHandler = new Handler(context.getMainLooper(), callback);
}

From source file:com.wbtech.ums.UmsAgent.java

public static void onEvent(Context context, String event_id, int acc) {
    if (handler == null) {
        HandlerThread localHandlerThread = new HandlerThread("UmsAgent");
        localHandlerThread.start();
        handler = new Handler(localHandlerThread.getLooper());
    }//from   www .  j a v a2  s.c o  m
    EventController.postEventInfo(handler, context, new PostObjEvent(event_id, null, acc + "", context));
}

From source file:mobisocial.musubi.service.AddressBookUpdateHandler.java

public static AddressBookUpdateHandler newInstance(Context context, SQLiteOpenHelper dbh,
        ContentResolver resolver) {/*from www .j av  a  2 s. c  om*/
    HandlerThread thread = new HandlerThread("AddressBookUpdateThread");
    Process.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
    thread.start();
    AddressBookUpdateHandler abuh = new AddressBookUpdateHandler(context, dbh, thread, resolver);
    return abuh;
}

From source file:org.tlc.whereat.modules.schedule.Scheduler.java

/**
 * Periodically deletes all records older than a certain time-to-live threshold from DB
 *
 * @param interval Interval for running `forget` (in millis)
 * @param ttl Amount of time (in millis) that a loc record should live before being forgotten
 * @param now Current time in millis since 1970 (paramaterized as optional for mocking in tests)
 *//*from  w w  w .jav a2s . c om*/

public void forget(long interval, long ttl, long... now) {

    HandlerThread thread = new HandlerThread("HandlerThread");
    thread.start();
    mForgetHandler = new Handler(Looper.getMainLooper());

    mForgetRunnable = new Runnable() {
        @Override
        public void run() {
            long rightNow = now.length > 0 ? now[0] : new Date().getTime();
            broadcastForget(mLbm, mCtx, rightNow - ttl);
            mForgetHandler.postDelayed(this, interval);
        }
    };

    mForgetHandler.postDelayed(mForgetRunnable, interval);
}

From source file:org.linkdroid.WebhookPostService.java

@Override
public void onCreate() {
    LogsService.logSystemDebugMessage(this, getString(R.string.webhook_post_service_creating));

    HandlerThread thread = new HandlerThread(TAG);
    thread.start();

    serviceLooper = thread.getLooper();//from   w w  w  .ja v  a  2  s.  c om
    serviceHandler = new ServiceHandler(serviceLooper);
}

From source file:com.amgems.uwschedule.services.LoginService.java

@Override
public void onCreate() {
    HandlerThread handlerThread = new HandlerThread("HANDLERTHREAD", Process.THREAD_PRIORITY_BACKGROUND);
    handlerThread.start();

    mServiceLooper = handlerThread.getLooper();
    mHandler = new LoginHandler(mServiceLooper);
}