Title: How can I send multiple messages to a running service?
Tags: android;background-service
Question: I have created a voice recording service. I have implemented all methods like startRecording() and stopRecording() , saveFile() etc...
I want to start recording when I send first notification from Firebase and stop recording by second notification and save the file when recording was stopped. This is my current solution by starting and stopping service in FirebaseNotificationService:
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() &gt; 0) {
String body = remoteMessage.getNotification().getBody();
final String tag = remoteMessage.getNotification().getTag();
if (tag.equals("startRecording")) {
Intent intent = new Intent(this, RecorderService.class);
startForegroundService(intent);
}
if (tag.equals("stopRecording")) {
Intent intent = new Intent(this, RecorderService.class);
stopService(intent);
}
I know that I can start recording onStartCommand like this in RecorderService:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startRecording();
// and show sticky notification
return START_STICKY;
and stop recording
@Override
public void onDestroy() {
stopRecording();
saveFile();
But I have another problem: How can I send more messages to a running service? For example how can I handle PAUSE in such service? or how can I call stopRecording() and saveFile() method before destroying the service? In brief is there another option to remotley handle a service rather than startCommand and destroy?
In this special case I want to call stopRecording() by another notification and call saveFile() with 3rd notification and finally stop the service by 4th notification. I don't want you to write methods to do that. I am just looking for a way to communicatie with a running service. Thanks.
Edit: I know other options like bind and unbind events but I am looking for a general way to cummunicate with service not using its prepared events.
Comment: Did you try answer below?

Here is the accepted answer: Instead of posting actions as Intent you need to use bound Service. It will gives you instance of Service binder, which you can use as a callback to your service and send hundred of events/messages.
Check the documentation about it.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:11/11/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<android><background-service>', 'Title': 'How can I send multiple messages to a running service?', 'ViewCount': 55, 'id': 58804737, 'user_AboutMe': '<p>I am expert in HTML, CSS, JavaScript, Jquery, SQL, Classic ASP and VBscript also familar with PHP, JAVA, Android. I guess I am doing Extreme Programming! The popular ZING is my handmade android app! <a href="https://play.google.com/store/apps/details?id=ir.zingapp.zing2&amp;hl=en&amp;gl=US" rel="nofollow noreferrer">https://play.google.com/store/apps/details?id=ir.zingapp.zing2&amp;hl=en&amp;gl=US</a></p>\n', 'user_AccountId': 5733651.0, 'user_DisplayName': 'Ali Sheikhpour', 'user_Reputation': 9633.0, 'user_Views': 1712.0, 'user_id': 4700922.0}}