0

I am thinking of making an app that interacts with a Web API.

Requirement:

  1. Start the bet and Display notification
  2. Send the bet to the site via POST.
  3. When the result arrives, update UI with the result.
  4. If the user hasn't pressed the stop button, back to #2 else stop the bet and remove notification.
  5. If the app is closed or not the active app, the betting would still continue
  6. If the notification is clicked, show/start the app

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..

  1. How can I tell the service to start betting with the initial bet data from the activity?
  2. How can the service know when the app closes or is not the active app on screen?
  3. How can the service update the UI of the app?

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.

Karyuu Ouji
  • 314
  • 3
  • 13

2 Answers2

1

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:

  1. Services Tutorials
  2. Official Developers Guide To Services
  3. BroadCastReceivers Tutorial
Community
  • 1
  • 1
arsena
  • 1,935
  • 19
  • 36
  • Thanks... I will be trying this out and re-read those articles you linked.. Most of the articles in Android Developer site is a bit hard for me to understand thoroughly... – Karyuu Ouji Jul 14 '16 at 10:31
  • Take a look and ask me if you need anything. I'd give you my own examples but can't write in Java well, I'm Xamarin developer. – arsena Jul 14 '16 at 10:50
  • does listeners work for service? Im thinking of giving my service a listener that my activity will implement and pass data through that event.. – Karyuu Ouji Jul 14 '16 at 11:14
  • By listener You mean `BroadcastReceiver` or an interface? – arsena Jul 14 '16 at 13:39
  • an interface ... I think the Binder is what I am looking for, I missed that part while reading #2 link... I am currently studying it now... – Karyuu Ouji Jul 14 '16 at 14:15
  • Any progress on the issue? – arsena Jul 18 '16 at 08:05
  • I haven't implemented this yet, nor started coding, coz my whole project got deleted accedentally by my mother... Im redoing my project now and nearly on this part... – Karyuu Ouji Jul 18 '16 at 08:43
  • that's sad, comment if you need anything. – arsena Jul 18 '16 at 09:55
0

Use volley api. Add it as a dependent in build.gradle

dependencies {
    compile 'com.android.support:design:+'
    compile 'com.android.support:appcompat-v7:+'
    compile 'com.android.volley:volley:+'
    compile fileTree(dir: 'libs', include: ['*.jar'])
}
Miriam Farber
  • 18,986
  • 14
  • 61
  • 76
Francis Nduba Numbi
  • 2,499
  • 1
  • 11
  • 22