Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Passing variables to an already running service

I am having issues passing a value from an Activity to an already running service. I was wondering what the best approach to take would be? Adding extras wont work as I believe this has to be done before the intent is started? (correct me if i'm wrong).

Any help would be great! I can elaborate if needed.

Dan.

like image 299
Daniel Flannery Avatar asked Mar 11 '13 19:03

Daniel Flannery


People also ask

How do you pass data to a running service?

You can send data to a running Service by calling startService() with an Intent . In the Service , onStartCommand() will be called on your running service. This is certainly the easiest method, although there are plenty of others.

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. We can also store data in a common database and access it on services as well as in Activity.

How do you share data between activity and service?

Primitive Data Types To share primitive data between Activities/Services in an application, use Intent. putExtras(). For passing primitive data that needs to persist use the Preferences storage mechanism. The android.

Which of the following code will you use to pass data to the sub activities of Android?

putExtra() method is used for sending the data, data in key-value pair key is variable name and value can be Int, String, Float, etc.


1 Answers

If your service is not an IntentService, you can call startService(...) as many times you want. The service will run the first time but next calls will result in new onStartCommand() calls with the new extras you need.

Check this answer and the doc.

like image 73
fedepaol Avatar answered Sep 24 '22 07:09

fedepaol