I'm rather perplexed about this one. I'm taking this Coursera course, and working on Assignment 5. I was able to complete the assignment with minimal difficulties, and then proceeded to try and make the code do my own purpose. I made a few tweaks to it as well, but I'm having a significant problem, which I can't for the life of me figure out. The Handler doesn't seem to take messages at all... Any idea what I'm doing wrong?
Here's a few bits of my code. Let me know if there's something else you'd like to see.
Handler:
public class DownloadHandler extends Handler {
private WeakReference<Context> mContext;
public DownloadHandler(Context context) {
super();
mContext = new WeakReference<Context>(context);
Log.v(TAG,"Created! "+this);
}
// Handle any messages that get sent to this Handler
@Override
public void handleMessage(Message msg) {
Log.v(TAG, "" + msg); //Never reached
...
}
}
Before I go any further, I've tested the handler by sending a simple message (handler.sendEmptyMessage(0);
, with no success. Any ideas?
Log.v(TAG,"handler="+handler);
Log.v(TAG,"Sending test "+handler.sendEmptyMessage(0));
And the logcat:
06-14 22:47:34.700: V/DownloadAddOnProducts(7406): handler=Handler (com.kd7uiy.hamfinder.DownloadHandler) {527724dc}
06-14 22:47:34.700: V/DownloadAddOnProducts(7406): Sending test true
To reduce this even further, I have a unit test:
public class DownloadTester extends InstrumentationTestCase {
public void setUp() {
targetContext=getInstrumentation().getTargetContext();
}
public void testDownloadHandler() {
Looper.myLooper().setMessageLogging(new LogPrinter(Log.VERBOSE,"DownloadLooper"));
DownloadHandler handler=new DownloadHandler(targetContext,Looper.myLooper());
Log.v(TAG,"handler="+handler);
handler.sendEmptyMessage(0);
Log.v(TAG,"Send empty message");
}
And the logcat shows:
06-14 23:03:46.068: V/DownloadHandler(8337): Created! Handler (com.kd7uiy.hamfinder.DownloadHandler) {52770be8}
06-14 23:03:46.068: V/DownloadHandler(8337): Created! Handler (com.kd7uiy.hamfinder.DownloadHandler) {52770f60}
06-14 23:03:46.068: V/DownloadTester(8337): handler=Handler (com.kd7uiy.hamfinder.DownloadHandler) {52770f60}
06-14 23:03:46.068: V/DownloadTester(8337): Send empty message
It created two DownloadHandlers because the other one is for another unit test in my set of unit tests.