I am thinking of making an app that interacts with a Web API.
Requirement:
After a lot of research and reading, I thought that the bound foreground service would accomplish this, but I can't find (or maybe I just don't understand) how doing it...
Here are my questions:
If I make a service and put the logic of betting in it, my activity/app would start the service and bind with it..
I will still search for a possible way to do this. I hope someone can guide me to the right way..
Update
(3) I ended up using LocalBroadcast
to signal the App components from the service of when to update the UI.
(2) By using LocalBroadcast
, I thought that my service should not mind the my App's State.
(1) I used Bound Service
and just call method on the service to pass data and start betting.
You send data to service via Intent: https://stackoverflow.com/a/15899750/3423468
Intent serviceIntent = new Intent(YourService.class.getName());
serviceIntent.putExtra("data", "123456");
context.startService(serviceIntent);
Then in Service override OnStartCommand
method:
public int onStartCommand (Intent intent, int flags, int startId)
{
String userID = intent.getStringExtra("data");
return START_STICKY;//or non-sticky
}
For handling app close event you can check this answer(never used myself): https://stackoverflow.com/a/26882533/3423468
And regarding your last question, you can use BroadcastReciever
to send data from service to activity and update UI
For further reading check this links: