Hải Phong
Hải Phong

Reputation: 5144

How to temporarily override system default ringtone?

I am writing an app that has a feature to temporarily override the system default ringtone with another custom ringtone when the user turn the app on, and restore to the default ringtone when the user turn it off. Meanwhile, the user can change the default ringtone in Settings, or in other app, but as long as my app is running, those changes has no effect until the user turn my app off.

I cannot do it like this

RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, toneUri);

Because it means changing the ringtone itself, not overriding it with another ringtone. Of course I can remember the system ringtone when the app is turned on, and change it back to that ringtone when it is turned off. However, using this approach, any changes made by the user (while my app is still running) in changing the system ringtone will be discarded when my app is turned off, because it will then automatically revert to the previously-remembered ringtone. will make my custom ringtone immediately not in effect anymore

So otherwise how can this be done?

Upvotes: 1

Views: 379

Answers (1)

Mark Ormesher
Mark Ormesher

Reputation: 2408

I think you're on the right track with remembering the "old" ringtone and restoring it when your app is finished. The problem, as stated, is what happens when the user changes their ringtone while your app is running.

There are a couple of approaches you could take to avoid this:

  • Listen for a system broadcast for the ringtone being changed and respond accordingly by re-applying your ringtone. I looked into this - unfortunately, no such broadcast exists, so this is a moot point.
  • Use an alarm to periodically check the ringtone and see if it's the one you set - if not, they have changed it, so you could change it back.
  • Irrelevant for updated question: Just before you restore the ringtone, check what the current setting is - if it's different to the one you set, then you know that it was changed while your app was open.

In the last case, you should be able to deal with the change, according to the functionality of your app.

Your update raises another question though: are you sure the user wants you to forcibly override their ringtone, even when they change it? If you are, then that's fine, but it's something you need to bear in mind.

Upvotes: 1

Related Questions