Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass information from a service the foreground activity

I have a service in android which is constantly pulling sensor information and sending them through the network interface.

In the other side I have several activities that will use those sensor values in different ways. Since the service gets the values in a event driven way, how can I pass those values only to the activity that is in the foreground?

Thanks in advance

like image 777
biquillo Avatar asked Feb 24 '11 22:02

biquillo


People also ask

How you can pass the data from activity to services?

How pass data from activity to services in Android? Explanation. Using putExtra() method, we can send the data. While using it, we need to call setResult() method in services.

What is foreground activity?

A foreground service executes an action that is visible to the user. A foreground service, for example, would be used by an audio app to play an audio track. A single, concentrated item that the user may accomplish is referred to as an activity. A Notification must be displayed by foreground services.

How do I stop foreground services from activity?

To start a foreground service, call startForeground() . To stop it, call stopForeground() .

What is foreground service why we use this?

A foreground service performs some operation that is noticeable to the user. For example, an audio app would use a foreground service to play an audio track. Foreground services must display a Notification. Foreground services continue running even when the user isn't interacting with the app.


2 Answers

I'll suppose it's the most common case of a "local" service, that is, running in the same process as the activities.

There are two solutions, one with the service sending intents to activities that register and deregister a proper BroadcastReceiver in onStart() and onStop().

The other works with a listener mechanism. You can get a reference to the service instance as explained here. Writing in the service a setListener(ServiceListener l) method and a ServiceListener interface you'll be able to implement a listener in your activity and set it on the service.

The intent way might be simpler and less risky, but you'll have to stick to intent extras for data exchange.

The listener way is more flexible but I've read that one must be sure to deregister the listener or you might end up mem. leaking whole activities.

like image 181
bigstones Avatar answered Oct 12 '22 22:10

bigstones


In my experience I've found it best to have my Activities bind to the service using

bindService(new Intent( this, ClassroomService.class), mConnection, Context.BIND_AUTO_CREATE );

Once the service is bound, I send a message from my activity to the service registering the activities message handler in the Activities ServiceConnection onServiceConnected().

public void onServiceConnected( ComponentName className, IBinder service )
{
    MyService mService = new Messenger(service);
    Message msg = Message.obtain(null, MyService.MSG_REGISTER_CLIENT );
    msg.replyTo = mMessenger;
    mService.send(msg);
}

On the service side, when it received this message, I store off the handler passed in the replyTo member and use that handler to send messages back to the "registered" Activity. If no Activities are currently registered, the message is ignored.

When unbinding from the service, I also send a MyService.MSG_UNREGISTER_CLIENT to tell the service to clear the stored handler reference.

If you bind to the service in your activities onResume() and unbind on the onPause() then the activity will only receive messages when it's in the foreground.

Hope that helps.

like image 30
Zoccadoum Avatar answered Oct 13 '22 00:10

Zoccadoum