0

I'm about to be finished with my app, and noticed a "bug". Because if the user press the button that starts a service, and then press another button that also starts the service but with other information, the service will just be restarted and the first start is simply gone. They can't mean that I'm supposed to write like 1 million service classes with same code but other name? So each button start each service, I hope not at least.

So, is there someway to re-use the same service and have multiple services, in the service? I think it was a bad explanation but I hope at least somebody gets what I mean and want to help me :)

Thanks!

GuiceU
  • 1,030
  • 6
  • 19
  • 35
  • Are you subclassing Service directly or IntentService? It seems the later is more appropriate to your case. – Alex Oliveira Jan 28 '13 at 18:27
  • If you mean subclass of my main class then no, the service is in an own file. @AlexOliveira – GuiceU Jan 28 '13 at 18:34
  • This service that is restarted, was it you who wrote? If "yes", does it extends Service or IntentService? If "Service", do you really need to handle multiple request at a time? if "no", try extending IntentService, wich is simpler, and don't has this problem of being restarted. It's just a suggestion, not the full solution. – Alex Oliveira Jan 28 '13 at 18:40
  • Okey, yes I wrote it. It extends just Service, and I need to handle multiple requests at a time. – GuiceU Jan 28 '13 at 18:50
  • Are you calling your service via `bindService` or `startService`? If you call `startService` multiple times, it can be a problem. PS: it look like you'r getting down-voted because your question isn't clear. Try to explain better (+1 for the try). – Alex Oliveira Jan 28 '13 at 18:59
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23522/discussion-between-alex-oliveira-and-guiceu) – Alex Oliveira Jan 28 '13 at 19:05

2 Answers2

1

The solution, as discussed in the chat:

https://chat.stackoverflow.com/rooms/23522/discussion-between-alex-oliveira-and-guiceu

The problem is that you are calling startService everytime. The solution is to change the call to bindService, so the Service is not restarted.

The Service will run until there are no more Context's bound to it. If you bind just an Activity, the Service can be stopped when the Activity is gone. If you need it to run continuously while your application is up, try calling from the application context.

Community
  • 1
  • 1
Alex Oliveira
  • 893
  • 1
  • 9
  • 27
0

Not sure what you mean by different services, but if you are just passing different information based on a button click you could pass information through extras. On the class you have buttons you would put something similar to this on each button click.

    Intent i = new Intent(FirstActivity.this, SecondActivity.class);   
    String values = "something";
    i.putExtra("EXTRASNAME", values);

Then to receive the values in the following class

    Bundle extras = getIntent().getExtras();
    String values = extras.getString("EXTRASNAME");

If the different buttons are values such as (YES OR NO) you put the value "YES" or the value "NO" in the same extras string and when receiving the value in the next activity you can do a simply if{} statement to have the activity do different things depending on extras value. With extras you can also do putBoolean/getBoolean and putInt/getInt. Hope this helps.

Bilbonic
  • 225
  • 1
  • 2
  • 7
  • 1
    Yeah, I think I can do like this. But the problem is that it's not only two buttons, the user can choose how as many buttons as he wants. – GuiceU Jan 28 '13 at 18:55