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){
}
}