I've tried many ways to use handlers to receive messages on a background thread, I have not been successful.
Is there a sure fire way to test this? Is there a sample code I can use to see how it is done?
I've tried many ways to use handlers to receive messages on a background thread, I have not been successful.
Is there a sure fire way to test this? Is there a sample code I can use to see how it is done?
 
    
    Yes, try the answer by @FoamyGuy. In the sample code he has sent back an empty message. I'm extending his code to pass strings. If you want to send some message back to the handler(eg: string), you can send some string or something else as follows:
Handler h = new Handler(){
    @Override
    public void handleMessage(Message msg){
        if(msg.what == 1){
           //Success
           String msg = (String)msg.obj;
           Log.d("", "Msg is:"+msg);
        }else{
           //Failure
           String msg = (String)msg.obj;
           Log.d("", "Msg is:"+msg);    
        }
    }
};
Thread t = new Thread() {
    @Override
    public void run(){
        doSomeWork();
        if(succeed){
            //we can't update the UI from here so we'll signal our handler. and it will do it for us.
               Message m = h.obtainMessage(1, "Success message string");
               m.sendToTarget();
        }else{
               Message m = h.obtainMessage(0, "Your Failed!");
               m.sendToTarget();
        }
    }   
}
 
    
     
    
    On a non-UI thread? All you need to do is create a Looper on that thread, then create the handler on it. That will automatically cause that Handler to be associated with that Looper. Then run Looper.loop
So
Looper.prepare();
Handler myHandler = new Handler();
Looper.loop()
and myHandler will be on the thread.
