Home » Android » How to Collect info from IntentService and Update Android UI

How to Collect info from IntentService and Update Android UI

Posted by: admin May 14, 2020 Leave a comment


I’m learning Android and I’m stuck with my service.

My application connects via Socket to my server every X seconds, receives an XML, parses the information and it’s shows in a TextView.

I’d like to know how can I implement an IntenService to do this and how to communicate the info to the UI. I’m finding very hard to see good examples.

I appreciate any help you can give me.

Thank you!

How to&Answers:

Use a handler and send a message to parent activity from the intentservice

Parent Activity :

Declaring Handler

Handler handler = new Handler() {
    public void handleMessage(Message msg) {
            Bundle reply = msg.getData();
                            // do whatever with the bundle here

Invoking the intentservice:

        Intent intent = new Intent(this, IntentService1.class);
        intent.putExtra("messenger", new Messenger(handler));

Inside IntentService:

    Bundle bundle = intent.getExtras();
    if (bundle != null) {
        Messenger messenger = (Messenger) bundle.get("messenger");
        Message msg = Message.obtain();
        msg.setData(bundle); //put the data here
        try {
        } catch (RemoteException e) {
            Log.i("error", "error");