开发者

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

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

I need to get my applicat开发者_JAVA技巧ion 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜