I created a MediaPlayer class to play mp3 files. Everything was fine, then my player stopped playing while it was in background and I found out that my problem was I did not create it in a service, so I started to read about services to learn how to create one and use it for my player.
My question is, what is the best way for me to communicate between the service and my application? Should I use the message or send intents or bind it?
I should also create a UI in notification area, also to show the progress buffering. Do I create a listener from the service side to the activity or there is better way?
I used these examples to learn but I did not learn how to use the onBind method yet:
Example: Communication between Activity and Service using Messaging
http://www.sapandiwakar.in/technical/tutorial-how-to-manually-create-android-media-player-controls/
Thanks in advance.
EDIT : I need to sometimes ask the player to send me the track details which is playing, and the player to tell me the buffer updates so i can update seek bar. Do I keep sending intents to players or should I use IBinder ?
Bind your Service to Activity (Say your application), When ever you open it to control MediaPlayer. See Binding Service Tutorial Series. Use foreground service so that its priority always be high and it get killed as a late as possible in memory low sitiuaton
To communicate between service and Activity, I prefer Broadcast Receiver. Send Broadcast from Service and receive it in Activity.
Related
OK, so I am trying to use the same notification for 2 different fore-ground Services.
I have a music player service, which plays music....duh.
I have a downloader service which downloads the music file.
I have tried the following so far :
I kept the music Player service non-foreground :
I observed that when the download service finished and its foreground status got finished, my static final mediaPlayer which was in the other service got GC'ed. Basically my whole music player service stopped. This is probably the expected behavior and this happens quite randomly, not right after downloader finishes.
I called startForeground() in both services :
In this case as as soon as either of the 2 services finished, the foreground notification vanished. Probably foreground status in both services was not achieved. In any-case, my music-player can still get GC'ed.
What I plan on doing :
Somehow, have the knowledge that the notification is showing and we should not call start-foreground rather just update it (if any of the 2 services is in foreground, the associated process can not get GC'ed, so in my opinion my music player is safe, even if the actual service is not in foreground, am I correct in assuming this ?)
Merge the 2 services (really wanna avoid this)
Would appreciate some advice :)
I think you should have a single service that runs in the foreground (when it needs to) and have that service hold instances of your player and down loader. They don't need to be services, they can just be plain objects This might be a variation of your option 2, but it seems like the right approach to me at least.
I have got a list of Music Titles in a ListView.
I can click on each item to play the Music through a MediaPlayer in a Service.
Now I want to implement 2 Features:
Music ProgressBar which is showing the current position in the song
The service shall continue playing when Song is over with the next song in the list
So how can I update the UI from a background service?
(I know there are some solutions on Stackoverflow but they seem to me a little bit of an overkill to send a Broadcast each Second from the Service i.e.)
Do I have to use Binding? What is the benefit of Binding?
Right now I just start the service with an Intent (startService(intent)) which contains the Song path.
What about the 2nd question? How can I do that?
I guess you built the service by yourself. Thus you know how it is built and how to get access to a mediaPlayer reference. What you need to do is to transform your service into a bound service. Maybe you will want your service to be started via startService (otherwise the bound service won't survive your activity), but afterwards, you will have to bind to it from inside your activity.
Once you are bound, you will get a IBinder (that you will define) and will be able to export the mediaPlayer reference to the bound activity through this IBinder. Afterwards, everything is quite straightforward, plug a listener on the media player and update your UI (in the UI thread !).
Also, you will need your service to be put forward.
I was solving very similar issues, however, I did the mixing/playing part myself.
The code for the android player-service part is at -github-
For communication between application and the service (it should be a foreground service with a notification in status bar, otherwise it can be silently killed / paused quite frequently) I experimented with different approaches and ended up with
Activity -> Service - using Messenger
Service -> Activity / anything - using BroadcastReceiver
The code on github is not too big (less than 500 lines total including imports) to give you an inspiration...
Btw. it also shows a service binding that does not kill service on activity exit.
I need some help or suggestions regarding Background services.
Well I want to achieve this. I have an application with Some Views that application also has a Background Service that always keeps on running.
In my views there is a Button whenever I press that button, that button passes some files to the Background Service and my Background service upload that file onto some server.
I am done with the uploading process. Now I want to know that how can I make a Background Service that always keeps on running and on my tapping of the button it sends a file to the Service.
I am new in Background service implementation.
Please guide Friends with some tutorials, suggestions or guidelines.
Thanks a bunch
You've probably already read some of the Android Service documentation, but I suggest studying it further and looking at the Local Service Sample if you have not done so already:
http://developer.android.com/reference/android/app/Service.html
It sounds like you have already got your Service up and running, and I think the actual problem you are trying to solve now is how to communicate data from your Activity to your Service. When your Activity is bound with a Service that's part of the same application, that service is in the same process and runs on the same main UI thread, so once you get the IBinder object from the Service after binding with it, you can simply directly call the functions in that Service from your Activity. Similarly, you can pass your Service a handler object from your Activity so that the service can send messages or post Runnables to your Activity. Communication with a local Service is therefore quite simple.
So if you take a look at the Local Service Sample in the link above, you will see a section in the code where we get a reference to the Service once binding has completed:
mBoundService = ((LocalService.LocalBinder)service).getService();
After that point, it's possible to directly call methods on that Service that's in the same application. For example, you could have a method called sendFile in your Service. In your Activity, you might do something like:
mBoundService.sendFile( myStuffObject );
There are quite a number of questions on Stack Overflow regarding communicating between an Activity and a Service, and I think you'd find it beneficial to search and read these.
A standard Android service will do just fine in this case.
It will continue running in the background untill its work is finished or until you ask it to stop.
There is a topic on the android dev site explaining services in detail.
you should go for android Service that is used for Background operation . Inside the service your have use TimerTask which will be checking the Queue for every x sec and when any items present in the Queue it will pull the item and upload it to the server.
here is the link for Android Service..
http://developer.android.com/reference/android/app/Service.html
Link fro Queue.http://developer.android.com/reference/java/util/Queue.html
I need a way to bind, or connect, both an activity and a widget to a simple service that streams audio from a single online source. I can't for the life of me figure out how.
I also need the method I use to be able to return some value at least once a second to move a progress bar in the activity, but not the widget. Also, the service needs to keep playing in the background as it literally is a music/audio player.
I'm fairly new to Java, so please explain in more detail than to someone who knows what he's doing. Thanks.
I would recommend using a Broadcast Receiver in both your Activity and your Widget. Then broadcast from your service. This should be picked up by each and then you can handle them.
Same the otherway (to service) implement a Receiver and then broadcast from widget.
I think this should work and I am testing it now.
I want to create an application where i need to implement the concept of service. The service concept should be worked out from my activity.
Can anyone help out?
Thanks,
Niki
If your service shall only be manipulated and accessed in your own application, implement simply a local service (See 5. in the Service class documentation).
You can then directly call the service functions from the activity.
If your service is meant to be available publicly, it is a bit more complicated. You'll need an AIDL interface. You can download the Music application source code that includes a service to play audio files in the background and can be controlled by any application. The control is done using Broadcast messages (see BroadcastReceiver).