I'm writing an app that sometime fires reminders that are supposed to play ringtones. I can start the ringtone OK, but can't stop it. I have a dismiss method in my fragment that should stop the currently playing ringtone and close the activity, but it doesn't work right. Here's what it does:
RingtoneManager ringMan = new RingtoneManager(getActivity());
ringMan.stopPreviousRingtone();
getActivity().finish();
The method does get executed and the activity closes, but the ringtone just keeps on playing. What am I doing wrong?
Create RingtoneManager object using Application Context like this
and RingTone object also create like this only.
It is working for me.
You have to create a class extending from Service class, the demo code is given below
Please Note this is the code form my College Project, it is for starting up default Alarm using Ringtone from BroadcastReciever extending Activity and Stopping the Ringtone from different normal Android Activity.
Service Class code:
Code for Activity calling the Service:
Activity ending or closing the Service:
NOTE: please note for context reference.
Reason why stopPreviousRingtone() is not stopping the ringtone
I found that it is only possible to stop a ringtone using
RingtoneManager.stopPreviousRingtone()
only if theRingtone
was created by the sameRingtoneManager
instance. An easy way to confirm this is to create an activity with aRingtoneManager
and three buttons:RingtoneManager
to start a ringtone.stopPreviousRingtone()
on theRingtoneManager
.stopPreviousRingtone()
on the completely differentRingtoneManager
instance.After doing this, you should find that the ringtone will only stop if you click the second button.
Stopping a Ringtone if you have access to the original Ringtone instance
If you have access to the original
Ringtone
instance, you can just call thestop()
method:Stopping Ringtones when keeping a reference to the original Ringtone is not possible
What you could try is to create a service which will load and play the ringtone when it was started and will stop the ringtone when it was destroyed. Here is an example:
Then, you can start the ringtone in the background just by starting the service:
And, to stop the ringtone, just stop the service: