0

What I want to do: To set my ringtone to incoming SMS even though there is another one in the SMS app.

What I tried: In SMS reciever, I set volume of STREAM_NOTIFICATION to 0 and than tried to play my ringtone as media via MediaPlayer.

So basically something like this:

am.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 0, 0);
MediaPlayer mp = MediaPlayer.create(context, notifUri);
mp.start();
int duration = mp.getDuration() + 3000;  //Margin
try {
   Thread.sleep(duration);
 } catch (InterruptedException e) {
 }
am.setStreamVolume(AudioManager.STREAM_NOTIFICATION, beforOverrideNotifVol, 0);

What happened: My custom ringtone played but immediately after this, the sms-app sound rang too. I tried to add some more sleep to the thread (to let the sms-app play on background) but it just waits for the volume to restore and then plays its ringtone.

At first I tried this:

 RingtoneManager.setActualDefaultRingtoneUri(...)

But it doesn't override the app-setting, it only overrides notification sounds when the ringtone is chosen in the sms-app.

PetrD
  • 45
  • 1
  • 5

1 Answers1

0

For me Ringtonemanger.setDefaultRingtoneUri worked

Try using this to choose ringtone and then set it

 private void chooseTone(int Id) {
    Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
            RingtoneManager.TYPE_RINGTONE);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
    MainActivity.this.startActivityForResult(intent, NotificationId);
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent intent) {
    if (resultCode == Activity.RESULT_OK) {
        Uri uri = intent
                .getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
        Log.d(TAG, "Notification Tone: " + uri);

            if (uri != null)
                this.chosenRingtone = uri;
    }}

And then finally to set the ringtone RingtoneManager.setActualDefaultRingtoneUri(MainActivity.this, RingtoneManager.TYPE_RINGTONE, chosenRingtone);

Neha Nathani
  • 544
  • 4
  • 10
  • I've already tried that. The problem is that this doesnt work when you have the ringtone chosen in the sms-app. It overrides only the default setting for notifications (and sms if its not chosen in the sms-app) – PetrD Feb 28 '13 at 06:43