Example usage for android.os HandlerThread HandlerThread

List of usage examples for android.os HandlerThread HandlerThread

Introduction

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

Prototype

public HandlerThread(String name) 

Source Link

Usage

From source file:info.staticfree.mqtt_camera.fragment.CameraFragment.java

/**
 * Starts a background thread and its {@link Handler}.
 *//*from   w  w w.  ja v  a2  s.  c  o  m*/
private void startBackgroundThread() {
    backgroundThread = new HandlerThread("CameraBackground");
    backgroundThread.start();
    backgroundHandler = new Handler(backgroundThread.getLooper());
}

From source file:io.realm.TypeBasedNotificationsTests.java

@Test
@RunTestInLooperThread/*from w  w w  .  j  a va 2  s .  c o m*/
public void callback_with_relevant_commit_from_different_looper_realmobject_async() {
    final CountDownLatch looperThread1Done = new CountDownLatch(1);
    final CountDownLatch looperThread2Done = new CountDownLatch(1);
    final CountDownLatch looperThread3Done = new CountDownLatch(1);
    final HandlerThread looperThread1 = new HandlerThread("looperThread1");
    final HandlerThread looperThread2 = new HandlerThread("looperThread2");
    final HandlerThread looperThread3 = new HandlerThread("looperThread3");
    looperThread1.start();
    looperThread2.start();
    looperThread3.start();
    final Handler looperHandler1 = new Handler(looperThread1.getLooper());
    final Handler looperHandler2 = new Handler(looperThread2.getLooper());
    final Handler looperHandler3 = new Handler(looperThread3.getLooper());
    final Realm realm = looperThread.realm;
    realm.addChangeListener(new RealmChangeListener() {
        @Override
        public void onChange() {
            globalCommitInvocations.incrementAndGet();
        }
    });

    final Dog dog = realm.where(Dog.class).findFirstAsync();
    assertTrue(dog.load());
    dog.addChangeListener(new RealmChangeListener() {
        @Override
        public void onChange() {
            switch (typebasedCommitInvocations.incrementAndGet()) {
            case 1: // triggered by COMPLETED_ASYNC_REALM_OBJECT from calling dog.load()
                assertTrue(dog.isLoaded());
                assertFalse(dog.isValid());

                looperHandler1.post(new Runnable() {
                    @Override
                    public void run() {
                        Realm realmLooperThread1 = Realm.getInstance(realm.getConfiguration());
                        realmLooperThread1.beginTransaction();
                        realmLooperThread1.commitTransaction();
                        realmLooperThread1.close();
                        looperThread1Done.countDown();
                    }
                });
                break;
            case 2: // triggered by the irrelevant commit (not affecting Dog table) from LooperThread1
                assertTrue(dog.isLoaded());
                assertFalse(dog.isValid());

                looperHandler2.post(new Runnable() {
                    @Override
                    public void run() {
                        Realm realmLooperThread2 = Realm.getInstance(realm.getConfiguration());
                        // trigger first callback invocation
                        realmLooperThread2.beginTransaction();
                        Dog dog = realmLooperThread2.createObject(Dog.class);
                        dog.setName("Akamaru");
                        realmLooperThread2.commitTransaction();
                        realmLooperThread2.close();
                        looperThread2Done.countDown();
                    }
                });
                break;

            case 3: // triggered by relevant commit from LooperThread2
                assertEquals("Akamaru", dog.getName());
                realm.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        // trigger second callback invocation
                        looperHandler3.post(new Runnable() {
                            @Override
                            public void run() {
                                Realm realmLooperThread3 = Realm.getInstance(realm.getConfiguration());
                                realmLooperThread3.beginTransaction();
                                realmLooperThread3.where(Dog.class).findFirst().setAge(17);
                                realmLooperThread3.commitTransaction();
                                realmLooperThread3.close();
                                looperThread3Done.countDown();
                            }
                        });
                    }
                });
                break;
            case 4:
                assertEquals("Akamaru", dog.getName());
                assertEquals(17, dog.getAge());
                // posting as an event will give the handler a chance
                // to deliver the notification for globalCommitInvocations
                // otherwise, test will exit before the callback get a chance to be invoked
                realm.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        assertEquals(3, globalCommitInvocations.get());
                        assertEquals(4, typebasedCommitInvocations.get());
                        looperThread1.quit();
                        looperThread2.quit();
                        looperThread3.quit();
                        TestHelper.awaitOrFail(looperThread1Done);
                        TestHelper.awaitOrFail(looperThread2Done);
                        TestHelper.awaitOrFail(looperThread3Done);
                        looperThread.testComplete();
                    }
                });
                break;
            }
        }
    });

}

From source file:com.mobicage.rogerthat.MainService.java

private void createIOWorkerThread() {
    T.UI();// w  w w  .  j av  a  2s.  com
    mIOWorkerThread = new HandlerThread("rogerthat_io_worker");
    mIOWorkerThread.start();
    final Looper looper = mIOWorkerThread.getLooper();
    mIOHandler = new Handler(looper);
    T.setIOThread("MainService.createIOWorkerThread()", mIOWorkerThread);
}

From source file:camera2basic.Camera2BasicFragment.java

/**
 * Starts a background thread and its {@link Handler}.
 *///from   w  w w .j  a v  a  2 s  . co  m
private void startBackgroundThread() {
    mBackgroundThread = new HandlerThread("CameraBackground");
    mBackgroundThread.start();
    mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
}

From source file:com.mobicage.rogerthat.MainService.java

private void createHttpWorkerThread() {
    T.UI();// w  w w .ja v a 2 s.c  o  m
    mBizzWorkerThread = new HandlerThread("rogerthat_http_worker");
    mBizzWorkerThread.start();
    final Looper looper = mBizzWorkerThread.getLooper();
    mBizzHandler = new Handler(looper);
    T.setBizzThread("HttpCommunicator", mBizzWorkerThread);
}

From source file:com.vest.album.fragment.CameraBasicFragment.java

/**
 * Starts a background thread and its {@link Handler}.
 *//*from  w w w .  j  a v  a  2 s  .  c  om*/
private void startBackgroundThread() {
    mBackgroundThread = new HandlerThread("CameraBackground");
    mBackgroundThread.start();
    mBackgroundHandler = new Handler(mBackgroundThread.getLooper());

}

From source file:com.amaze.carbonfilemanager.activities.MainActivity.java

/**
 * Initializes an interactive shell, which will stay throughout the app lifecycle
 * The shell is associated with a handler thread which maintain the message queue from the
 * callbacks of shell as we certainly cannot allow the callbacks to run on same thread because
 * of possible deadlock situation and the asynchronous behaviour of LibSuperSU
 *//*from   www .  j  a va2 s . co m*/
private void initializeInteractiveShell() {
    // only one looper can be associated to a thread. So we're making sure not to create new
    // handler threads every time the code relaunch.
    if (rootMode) {
        handlerThread = new HandlerThread("handler");
        handlerThread.start();
        handler = new Handler(handlerThread.getLooper());
        shellInteractive = (new Shell.Builder()).useSU().setHandler(handler).open();

        // check for busybox
        /*try {
        if (!RootUtils.isBusyboxAvailable()) {
            Toast.makeText(this, getString(R.string.error_busybox), Toast.LENGTH_LONG).show();
            closeInteractiveShell();
            sharedPref.edit().putBoolean(PreferenceUtils.KEY_ROOT, false).apply();
        }
        } catch (RootNotPermittedException e) {
        e.printStackTrace();
        sharedPref.edit().putBoolean(PreferenceUtils.KEY_ROOT, false).apply();
        }*/
    }
}

From source file:com.blestep.sportsbracelet.view.TimelineChartView.java

private synchronized void setupBackgroundHandler() {
    if (mBackgroundHandler == null) {
        // Create a background thread
        mBackgroundHandlerThread = new HandlerThread(TAG + "BackgroundThread");
        mBackgroundHandlerThread.start();
        mBackgroundHandler = new Handler(mBackgroundHandlerThread.getLooper(), mMessenger);
    }//from  w w w .j a v  a  2s  .c  o m
}

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

public static AddressBookUpdateHandler newInstance(Context context, SQLiteOpenHelper dbh,
        ContentResolver resolver) {//from   w  w  w. j a  v  a 2s.  co m
    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:androidx.media.MediaController2.java

/**
 * Create a {@link MediaController2} from the {@link SessionToken2}.
 * This connects to the session and may wake up the service if it's not available.
 *
 * @param context Context/*from  w  ww.j a  v  a2  s.  c  o  m*/
 * @param token token to connect to
 * @param executor executor to run callbacks on.
 * @param callback controller callback to receive changes in
 */
public MediaController2(@NonNull Context context, @NonNull SessionToken2 token, @NonNull Executor executor,
        @NonNull ControllerCallback callback) {
    super();
    if (context == null) {
        throw new IllegalArgumentException("context shouldn't be null");
    }
    if (token == null) {
        throw new IllegalArgumentException("token shouldn't be null");
    }
    if (callback == null) {
        throw new IllegalArgumentException("callback shouldn't be null");
    }
    if (executor == null) {
        throw new IllegalArgumentException("executor shouldn't be null");
    }
    mContext = context;
    mHandlerThread = new HandlerThread("MediaController2_Thread");
    mHandlerThread.start();
    mHandler = new Handler(mHandlerThread.getLooper());
    mToken = token;
    mCallback = callback;
    mCallbackExecutor = executor;
    mDeathRecipient = new IBinder.DeathRecipient() {
        @Override
        public void binderDied() {
            MediaController2.this.close();
        }
    };

    initialize();
}