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
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.
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.
To start a foreground service, call startForeground() . To stop it, call stopForeground() .
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With