0

I have an Android background thread whose creator is long gone. But the thread keeps running and logging data all the time. I want to pass or send this data to the current running activity that did not create the thread.

What do I have to do? Google? That brings up stuff about passing work to the background so as to not block the main UI thread. That's not what I want. I want to pass/send the data to the new currently running activity.

3 Answers 3

1

Typically if you are doing a background thread of that sort you want to use a Service to hold the thread. In your Activity you just bind to the Service, and when you get the Service instance you can pass it a Handler. The thread in the service can then access the Handler and send Messages through the Handler to the Activity. When the Activity finishes you unbind(), clear the Handler and its as if you were never there.

7
  • @Femi, Well unless you want to explain further, I guess you given me some things to look up: "use a Service to hold the thread?", "in your activity you just bind to the Service?", "Service instance?", "pass it a handler", "thread in the service?", "access the Handler?", "send Messages through the handler to the activity?", "when the activity finished you unbind()?", "clear the handler?"
    – Marie
    Jun 3, 2011 at 0:18
  • What is so deficient @Femi about a background thread that is needs all this other stuff to do its job?
    – Marie
    Jun 3, 2011 at 0:33
  • Its not so much that the background thread alone is deficient, but you need some handle to the Thread for future references. That is in general the recommended way to handle background threads on Android. See developer.android.com/guide/topics/fundamentals/services.html for fine detail.
    – Femi
    Jun 3, 2011 at 0:58
  • @Femi I just flagged this question for help from a moderator. Basically I asked this question prematurely. I did not have any code. All I had was a verbal scenario of the problem I wanted to solve. But now I have working code: Service, thread, handler, etc. It's not completely done. I need help. Now that I have code I can ask very specific questions where there is difficulty. Do you think it would be OK to post a completely new question based on the code I have now? Or should I substantially revise this question and hope for more answers?
    – Marie
    Jun 14, 2011 at 23:43
  • Ah, not sure: you could post additional questions. Honestly not sure what the best approach would be.
    – Femi
    Jun 14, 2011 at 23:56
0

OK, so this 'logging' thread is trundling along on its own, shoving input onto some disk file, or whatever. If some other thread wants to 'hook' this data, it has to signal the logging thread to call something else with the data as well as logging it. The logging thread is normally waiting on some input queue for stuff to log, yes? If so, send the thread a message on its input queue to tell it to call method XXXX with all the data to be logged. The logging thread can keep a list of all events to call on each logging action.

Does that make sense? Basically, it's an expansion on what Marie is suggesting. The logger thread would get messages and switch on some 'Ecommand' enumeration field in the message. Pseudo Pascal:

case inMessage.command of : ElogString:logThis(inMessage.logString); EaddNotification:notifierList.add(inMessage.event); EdeleteNotification:notifierList.delete(inMessage.event); end;

The 'logThis' function would write the logString to the disk file and then call every event in notifierList with the data.

Does this make sense? It did when I wrote it, but been on the Abbot ale tonight, (5%).

Rgds, Martin

1
  • I just flagged this question for help from a moderator. Basically I asked this question prematurely. I did not have any code. All I had was a verbal scenario of the problem I wanted to solve. But now I have working code: Service, thread, handler, etc. It's not completely done. I need help. Now that I have code I can ask very specific questions where there is difficulty. Do you think it would be OK to post a completely new question based on the code I have now? Or should I substantially revise this question and hope for more answers?
    – Marie
    Jun 14, 2011 at 23:45
0

ok the answer is very simple, use Handler and keep passing this handler to the methods that you use the handler then will be your connector to the main activity, and will be able to change the GUI too if needed, as follow :

final Handler someHandler=new Handler(){
public void handleMessage(Message msg)
{    switch(msg.what){
        case 0: dosomething(); break;
        case 1: dosomething(); break;
        case 2: dosomething(); break;
     }
}

declare this handler the same way as you declare any method don't nest it in any method every time that you need to pass some data to the main activity, or GUI, simple call to the handler for example :

public void downLoadData(Handler h)
{
    h.sendEmptyMessage(34);
    someThread=new threadClass(h);
}

and so on...

hope this helps, Best regards.

1
  • and @frieza I just flagged this question for help from a moderator. Basically I asked this question prematurely. I did not have any code. All I had was a verbal scenario of the problem I wanted to solve. But now I have working code: Service, thread, handler, etc. It's not completely done. I need help. Now that I have code I can ask very specific questions where there is difficulty. Do you think it would be OK to post a completely new question based on the code I have now? Or should I substantially revise this question and hope for more answers?
    – Marie
    Jun 14, 2011 at 23:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.