1

I'm writing a library, which does some intensive network work. So, I'm using a HandlerThread to carry out this operation like this:

Looper.prepare();
libraryThread = new LibraryHandlerThread(context);
libraryThread.start();
libraryThread.getLooper();

LibraryHandlerThread does the network operations and other time consuming operations.

When I call this code from a worker thread (any thread other than Main thread), it works fine. But complains about "can't initialize another looper when one is already active". I believe that a Looper runs on Main thread by default, and complains about Looper.prepare(). I can do something like following to make it working from Main thread:

Looper.getMainLooper().quit();
Looper.prepare();
libraryThread = new LibraryHandlerThread(context);
libraryThread.start();
libraryThread.getLooper();

My question is: what would be the impact on Main thread. In my ideal world, I want to run my library's operations on a separate thread without impacting the main thread. How can I achieve this without much destruction?

bianca
  • 7,004
  • 12
  • 43
  • 58

2 Answers2

0

What you will want to look into is using a Asynctask to run this in the background which will allow your main thread to keep working http://developer.android.com/reference/android/os/AsyncTask.html

Or you can create a service if this is something that you will need constantly running. http://developer.android.com/reference/android/app/Service.html

TheHamstring
  • 712
  • 2
  • 6
  • 22
  • 1
    Quoting from the link you shared: "AsyncTasks should ideally be used for short operations (a few seconds at the most.)" – Nitin Sethi Sep 12 '13 at 17:16
  • Good point! That raises the question what is extensive in this case. If we know what kind of work that is being done the best option becomes apparent. Nitin Sethi I do think that you are most likely correct in this case. – TheHamstring Sep 12 '13 at 17:21
  • As for your second paragraph, it's important to remember that a using a service does not mean using a different thread, unless the service passes work on to some internal thread it has explicitly created. – Chris Stratton Sep 12 '13 at 17:56
0

Try the below code:

private static final HandlerThread sWorkerThread = new HandlerThread("network-operations");
static {
    sWorkerThread.start();
}
private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
jbihan
  • 3,053
  • 2
  • 24
  • 34
Nitin Sethi
  • 1,416
  • 1
  • 11
  • 19
  • How can I define handleMessage() method/behavior, if handler is created this way? – bianca Sep 12 '13 at 17:48
  • You can pass on the reference of your Activity in the constructor of the class that contains the above code and use it to call the instance methods in the Activity class. You can have a look the the LauncherModel class from AOSP in the link: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/4.2.1_r1.2/com/android/launcher2/LauncherModel.java – Nitin Sethi Sep 12 '13 at 17:56