I want to send data to the server periodically, I'm using background Service
for that, but I want to send when the data got updated, and updated data I'm getting in Activity
and the Service
is running in background.. so how can i pass data to running Service
from Activity
. Using Intent
I can send data only one time while starting the Service
.
Intent serviceIntent= new Intent(DriverActivity.this,demoService.class);
serviceIntent.putExtra("token", token);
startService(serviceIntent);
1.Sending data to service Upto Lolipop edition
Retrieving data in Service class :
The best option would be to persist the data on the hard drive (e.g. SharedPreferences, database, ...).
Activity got updated => persist to storage => invoke Service
The Service has to read the data from the chosen storage before sending the data.
Use multithreading instead it becomes much easier and you will get the same functionality.
Read this article https://developer.android.com/guide/components/bound-services.html
For example you can use
Messanger
And in your
Activity
orFragment
you can send data in this way:If you don't know how to pass data with
Message
look at this answer https://stackoverflow.com/a/17929775