3

I'm trying to save the selected sound as a ringtone/notification sound based on the position of the selected list view item but I'm having serious issues with finding relative tutorials (if any) for this. Below is my code so far but I want to achieve this in the simplest way/as less lines of code as possible hence for the sake of simplicity, I have used 1 context menu. Ideally

public void function1(int id){

    }

is where the code would go for setting the ringtone and

public void function2(int id){

    }

is where the code would go for setting the notification sound.

E.g. (trying to achieve this when setting a ringtone) Click & hold "chimes" list item > Context menu appears > Select "Set as Ringtone" context menu item > 'Phone ringtone' window appears (with "chimes" as one of the available options) > User clicks OK or Cancel > If the user clicks OK, return back to my app and show a toast notification ("Ringtone saved") OR If the user clicks Cancel, return back to my app and show a toast notification ("Ringtone not saved").

All help will be highly appreciated.

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class MainActivity extends ActionBarActivity {

    private ListView mainList;
    private MediaPlayer mp;
    private final String[] listContent = {
            "chimes", "chord", "ding", "notify", 
            "recycle", "ringin", "ring out","tada"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mp = new MediaPlayer();
        mainList = (ListView) findViewById(R.id.main_listView);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, listContent);
        mainList.setAdapter(adapter);
        registerForContextMenu(this.mainList);
    }


    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, v.getId(), 0, "Action 1");
        menu.add(0, v.getId(), 0, "Action 2");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        if (item.getTitle().equals("Action 1")){
            function1(item.getItemId());
        } else if (item.getTitle().equals("Action 2")){
            function2(item.getItemId());
        } else {
            return false;
        }
        return true;
    }

    public void function1(int id){

    }
    public void function2(int id){

    }
}
wbk727
  • 8,017
  • 12
  • 61
  • 125

2 Answers2

4

In order to set the ringtone or notification sound you use the RingToneManager.

Specifically you use

RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE, newUri);

for setting default ringtone. And you use

RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_NOTIFICATION, newUri);

for setting default notification sound.

This needs a URI (Uniform Resource Identifier) which is a not an integer or ItemId like the one your functions currently take.

From your sample code, there are a few possible options that could be taken.

The first is to pass in the title of the Ringtone into the function, as a string instead of the ID, and then call RingtoneManager.getCursor() in order to get a list of all possible ringtones and check each one to see if the titles match and if they do then you set the URI for the matching title.

The second is to make your list of choices based on the cursor of all available ringtones and the pass in the id, and get the URI using RingtoneManager.getRingtoneUri(id). One way of doing this is detailed at Using SimpleCursorAdapter to Display Ringtones from RingtoneManager in Android Using ListView Templates

The third is to use ACTION_RINGTONE_PICKER which has a relevant StackOverflow Question.

Community
  • 1
  • 1
Appleman1234
  • 15,946
  • 45
  • 67
0

Sorry, this could have been a comment. I dont have enough permission to comment right now. And do let me know how this works out for u...

To get the ringtone picker,

Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,RingtoneManager.TYPE_NOTIFICATION);
startActivityForResult(intent,999);

Now override the onActivityResult() in the activity/fragment.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK
            && requestCode == 999)
        if (data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI) != null) {
            Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
            if (uri != null) {
                RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_NOTIFICATION, uri);
            }
        }
    }
}

Hope this works.... (And Upvote if useful)...

Praveen Kishore
  • 436
  • 1
  • 3
  • 14
  • Put the first part where u intended to launch the ringtone picker dialog. Eg: If u want to show the ringtone dialog after the user clicks on a button, then add this code on the button.onClick(). ;). – Praveen Kishore Feb 16 '15 at 15:45
  • OK but what about the uri of the ringtone? I have multiple ringtones within my project. – wbk727 Feb 16 '15 at 21:57
  • Then I guess u r trying to show the ringtones which are not present on the phone.. but its withing ur project and ur trying to show those ringtones to the user and the user has to pick from that set of ringtones... Right? – Praveen Kishore Feb 17 '15 at 08:17
  • Correct, the ringtones are within my app + the list I have in my project represents the ringtones. When the user taps the list view item the desired ringtone will play as necessary. When the user taps & holds the list view item the context menu will appear but I also want the name of the selected list view item to appear as the title of my context menu (same context menu items, different context menu titles), when the user taps 'set as ringtone' I want the ringtone menu to appear and the name of the selected menu item to appear within that menu, based on the list view item that was selected. – wbk727 Feb 17 '15 at 15:35
  • send me an email "praveenkishorepraveen@gmail.com" so that we can communicate instantly about the issue on any other instant medium. I have an idea that can be implemented. Its not good to reply with such a time gap between the comments. It may take lots of time... Alright with you @MacaronLover? – Praveen Kishore Feb 18 '15 at 13:24
  • Sure. Just sent you an email. Mine is "macaronlover22@gmail.com" – wbk727 Feb 18 '15 at 22:54