views:

29

answers:

2

Hi

I have an application that get/send data from/to a remote DB on internet.

I need to get my application working in background mode, then i supose that i have to put all the send/get remote data in a service.....

but.... How can this service change values of variables and UI textfields of my activities?

i can't find any information about this, all the tutorials i am finding are of simple services that doesn't do something like that

can someone explain me how to do it please?

A: 

Hi,

my suggestion to you is, create a handler for the UI part which updates the text field or UI components.

Secondly, have notifications from the service to the activity by way of interface class.

Vinay
this is starting to going so munch hard for me... there is not a simple way to do it without handler?
AndroidUser99
+1  A: 

Use a BroadcastReceiver

In your Activity place the following code:

private BroadcastReceiver onBroadcast = new BroadcastReceiver() {
    @Override
    public void onReceive(Context ctxt, Intent i) {
        // do stuff to the UI
    }
};

Register the receiver in your onResume():

registerReceiver(onBroadcast, new IntentFilter("mymessage"));

Be sure to unregister in onPause():

unregisterReceiver(onBroadcast);

In your Service, you can post the message to the Application, which will be heard by your Activity:

getApplicationContext().sendBroadcast(new Intent("mymessage");

If you need to, you can add data to the Intent's bundle to pass to your Activity as well.

Andrew
wow, that's sounds cool!!! now i know how to pass objects from my service to my activity, but..... ¿how to pass objects from my activity to my service?
AndroidUser99
http://developer.android.com/reference/android/app/IntentService.html You start it just like a regular Service. You pass an Intent (which you can Bundle data into). Inside your IntentService, you Override the onHandleIntent handler. This is the code that will be executed when the Service begins. IntentService will shut down automatically when finished. The code is run in a thread. It will queue up Intents. So say you call startService twice, the second Intent will wait for the first one to finish; and then be processed.
Andrew