Sets the ringtone based on the position of the selected ListView item
I’m trying to save the selected sound as a ringtone/notification sound based on the position of the selected ListView item, but I’m having serious problems finding relevant tutorials (if any) for this. So far, below is my code, but I wanted to achieve this in the simplest way/as few lines of code as possible, so I used 1 context menu for simplicity. Ideally
public void function1(int id){
}
is the code that sets the ringtone
public void function2(int id){
}
is where the code sets the notification sound.
For example, click and hold the Ringtone list item (trying to do this when setting a ringtone) > context menu appears> select the Set as Ringtone context menu item> the Ringtone window appears (Ringtone is one of the available options) > User clicks OK or Cancel > If the user clicks OK, return to My Apps with a toast notification (“Ringtone saved”) or if the user clicks Cancel, Return to My Apps and display a toast notification (“Ringtone not saved”).
We would appreciate all help.
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){
}
}
Solution
To set a ringtone or notification sound, you can use RingToneManager
Specific you use
RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE, newUri);
Used to set the default ringtone. And you use
RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_NOTIFICATION, newUri);
Lets you set the default notification sound.
This requires a URI (Uniform Resource Identifier), which is not an integer or ItemId that your function is currently using.
From your sample code, there are some possible options you can take.
The first is to pass the title of the ringtone
as a string instead of the ID to the function, then call RingtoneManager.getCursor() to get all possible list ringtones
and check each ringtone to see if the title matches, and if so, set the URI for the matching title.
The second is to make your picklist based on the cursor and incoming id of all available ringtones, and get the URI using RingtoneManager.getRingtoneUri(id
). One way to do this is detailed in Using SimpleCursorAdapter to Display Ringtones from RingtoneManager in Android Using ListView Templates
The third is to use ACTION_RINGTONE_PICKER, which has a related StackOverflow Question .