Johnathan Logan
Johnathan Logan

Reputation: 367

Running a bound service and a thread

I currently have a bound Service with my Main activity.

I was wondering if it was possible to have a Thread Running inside this bound Service that can pass integers to my Main Activity. This Service needs to auto update my Main Activity's Text View with any new Random number Integer without clicking a button.

Should i look into Handlers?? Or Message/ Bundles??

Any help would be appreciated ! Thank you !

Upvotes: 0

Views: 250

Answers (2)

Mr.Popular
Mr.Popular

Reputation: 845

You can define one Receiver your MainActivity and you can use send Broadcast to that receiver to update your UI.. That is a simple way to do it

In your service Just define one intent and put values into it like follwing

Intent i = new Intent();
i.setAction("RECEIVERACTION");
i.putExtra("data", "mydata");
sendBroadcast(i);

and in your activity

public static class Receiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String data = intent.getStringExtra("data");
        textView.setText(data);
    } 
} 

Hope it helps!!!

Upvotes: 1

Aditya K
Aditya K

Reputation: 456

Please go through api guides. Specifically

https://developer.android.com/guide/components/bound-services.html https://developer.android.com/guide/components/services.html

This should give you a pretty good idea to move forward and also code samples

Upvotes: 0

Related Questions