rick
rick

Reputation: 4715

changing the notification ringtone in android

I have implemented preference screen on own i.e., I have prepared custom listview and managed all the things what my app needs on own. But I am stuck how to give user a facility to change the notification ringtone. Usually we can achieve this by RingtonePreferece.

But how to implement it without using preference screen, so that by clicking on that list item it should redirect the user to list of ringtones and when he selects that particular ringtone, the title of the ringtone should be shown on that particular list row and has to be used as ringtone for notification. Can someone please suggest on how to achieve this? Below is my notification code.

NotificationCompat.Builder builder =  
                new NotificationCompat.Builder(context)  
                .setSmallIcon(R.drawable.ic_launcher)
                .setTicker(msg)
                .setContentTitle(title)  
                .setContentText(msg)
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true);
NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);  
         manager.notify(id, builder.build());

Upvotes: 1

Views: 1412

Answers (2)

Sherif elKhatib
Sherif elKhatib

Reputation: 45942

Ok the first step would be to allow the user to select the ringtone he wants. I will help you implement an activity that lists the available ringtones and allows the user to select one. Naturally, this activity is a ListActivity.

First of all create a container for the ringtone. I called it RingtoneObject and it contains the name of the ringtone along with the uri of the ringtone:

private static final class RingtoneObject {
    private String name;
    private Uri uri;
    public RingtoneObject(String name, Uri uri) {
        this.name = name;
        this.uri = uri;
    }
    @Override
    public String toString() {
        return name==null?"":name.toString();
    }

}

In the onCreate, you should query the RingtoneManager for the available ringtones and then iterate over them and add them to the list of type RingtoneObject. The code for this is simply:

ArrayAdapter<RingtoneObject> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    RingtoneManager ringtoneMgr = new RingtoneManager(this);
    ringtoneMgr.setType(RingtoneManager.TYPE_ALARM);
    Cursor alarmsCursor = ringtoneMgr.getCursor();
    int alarmsCount = alarmsCursor.getCount();
    RingtoneObject[] alarms = new RingtoneObject[alarmsCount];
    int index = 0;
    while(alarmsCursor.moveToNext()) {
        alarms[index] = new RingtoneObject(ringtoneMgr.getRingtone(index).getTitle(this), ringtoneMgr.getRingtoneUri(index));
        index++;
    }
    alarmsCursor.close();
    adapter = new ArrayAdapter<MainActivity.RingtoneObject>(this, android.R.layout.simple_list_item_1, alarms);
    setListAdapter(adapter);
    getListView().setOnItemClickListener(this);
}

You can notice that the adapter's scope is out of the onCreate function. I need reference to it in the OnItemClickListener's callback. You can, as well, notice that I set the OnItemClickListener of the listview to this which is, in this context, the activity itself. Therefore the activity should implement OnItemClickListener and when an item is clicked: I simply played the ringtone.

MediaPlayer md;
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
    if(md != null) {
        if(md.isPlaying())
            md.stop();
        md = null;
    }
    md = MediaPlayer.create(this, adapter.getItem(position).uri);
    md.start();
}

Again, the MediaPlayer is out of the function because I need the reference whenever I want to stop playing.

Anyway, in your own onItemClick function or some other sort of mechanism you present for the user to select a ringtone, you will have to save the Uri of the ringtone in some persistent storage (e.g. SharedPreferences) and then when you want to show the notification, use the saved uri.

Note: You can save the uri as a string using theUri.toString() and load this string and convert it back to a Uri using Uri.parse(theString).

Here is the complete sample (for uri selection).

Upvotes: 3

Stefano Munarini
Stefano Munarini

Reputation: 2717

This is the code that let you set tone:

Notification notification = new Notification(icon, text, time);

//custom sound
notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.siren);

Upvotes: 2

Related Questions