Android - Custom Notification Sound


If you want user to get notified with custom notification sound then you need to find the Uri of the sound on device. Let us implement an activity where user can select which notification sound they want to get played.

Screenshot

public class SettingsActivity extends Activity implements OnClickListener{
  @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		notificationView = (TextView) findViewById(R.id.notificationView);
	    notificationView.setOnClickListener(this);
		
	}
@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub		
	}	
	}

RingtoneManager provides access to system sounds like ringtones, notification tones.

Steps :

  1. The below code will show the ringtone picker as a subactivity.
    Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
  2. In Intent extra information add the following code to specify that you would like to see the available notification sounds only.
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
    
  3. Set the title of the subactivity.


     
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "CHOOSE NOTIFICATION SOUND");
    
  4. Give the Uri of the current ringtone. It would be selected as default. Pass NULL if user has never selected any notification tone for your application.
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri)null);
  5. Start the sub Activity and wait for result.
    this.startActivityForResult(intent, 3);

Receive the result from RingtoneManager's subactivity

  • Code to get the Uri of picked notification tone.
    Uri toneUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
    
  • getParcelableExtra() helps to fetch the ringtone selected by the user.
  • Convert the retrieved Uri to String.
    tonePath = toneUri.toString();
package com.example.notificationsoundkb4dev;

import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.TextView;

public class SettingsActivity extends Activity implements OnClickListener{
TextView notificationView;
String tonePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        notificationView = (TextView) findViewById(R.id.notificationView);
        notificationView.setOnClickListener(this);
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.settings, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
    if(v == findViewById(R.id.notificationView)){
        Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "CHOOSE NOTIFICATION SOUND");
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri)null);
        this.startActivityForResult(intent, 3);
        
    }    
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == Activity.RESULT_OK && requestCode == 3){
            Uri toneUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
            //RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION, toneUri);
            if(toneUri!=null){
                tonePath = toneUri.toString();
                Log.i("tonepath", tonePath);    
            }
        }
    }

}

In the next tutorial we are going to learn about SharedPreferences in android. We will save our selected notification sound uri as a key-value pair in shared preferences.