I’m sure this is an easy answer, but it’s driving me crazy and I’m hoping someone can just put me out of my misery.
I have an android activity, widget and service. The service runs sound in the background, either for a set amount of time or continuously. Because I want the sound to run until stop is hit, I don’t have a stopSelf() method in the service. This was all fine and dandy with the activity because I had it startService on play, and stopService on stop. Now I want to throw a widget in this latest update. So here’s where I’m stuck. Because it’s a broadcast receiver, I’m very limited in what it can do. I can call context.stopService, but I only want to stop the service when the button is pressed. I can only find a way to set an onClickListener with a pendingIntent.
In my service’s onStartCommand() method, I read the “loops” extra from the incoming intent. So I tried just passing in a value of zero, but for some reason it doesn’t seem to read it. I know the onStartCommand() is being called in the service because the music is playing, but regardless of what extra I throw on the intent, it doesn’t seem to read it and I have no idea why. So my question is, what is the best strategy to use to stop a service from a widget’s button? I’m hoping it’s an easy answer, otherwise I guess I could create a new activity or broadcast receiver to handle the button click, but that seems so unnecessary. Any advice would be very much appreciated! Thanks!
The
PendingIntentyou pass to the button in the Widget should be able to send Intent extras to the service like any other Intent: you should be able to detect that Intent inonStartCommandand then tell the Service to stop the sound and stop itself. Hard to tell what else you might be doing wrong without some code.