0
public void onRingtone(View view) {
        final Uri currentTone= RingtoneManager.getActualDefaultRingtoneUri(Setting.this, RingtoneManager.TYPE_ALARM);
        Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Ringtone");
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, currentTone);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
        startActivityForResult(intent, 999);
    }

I have this onClick method to select available Ringtone from it's own device. When startActivityForResult() method is called it redirects to custom popup dialog from where user can select a Ringtone and at same time it starts that Ringtone. That's Ok. But I want to save that Ringtone to play it on another activity. So what should I do? Can I store it in database(SQLite), or in SharedPreference or any other way?

Edit: I don't know why my question got closed even if I provided all information. But still giving more information, to understand my question better.

Xml file:

<RelativeLayout
                android:id="@+id/Ringtone"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_below="@id/sp2"
                android:layout_marginTop="1dp"
                android:background="@drawable/pressed"
                android:clickable="true"
                android:focusable="true"
                android:onClick="onRingtone"
                android:gravity="center"
                android:paddingTop="15dp"
                android:paddingBottom="15dp">

The above method is called when the user selects the Ringtone. I got one answer which told me to use SharedPreference to store uri. I want to keep the selected ringtone even after app gets closed. So SharedPreference is a better option but how to make it global so other activity can also use it?

If I create object of SharedPreference in this java file, how can I get access to other acivity?

matthias_h
  • 11,356
  • 9
  • 22
  • 40

1 Answers1

1

To save ring tone Uri , you can use:

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("currentTone", currentTone.toString()); 
editor.commit();

To retrieve ring tone Uri you need to parse the string to Uri :

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String currentToneString= settings.getString("currentTone", null);
Uri currentTone= Uri.parse(currentToneString);
return currentTone;
Jinn
  • 72
  • 7
  • what currentTone.toString() will contain? Actually I copied the Ringtone taker code from online so don't know what is that. – Aayush dual Mar 20 '20 at 08:04
  • And is this Preference available to all activities? – Aayush dual Mar 20 '20 at 08:11
  • Uri should be the path of the ringtone. onActivityResult, uri will be data.getData(); – Jinn Mar 20 '20 at 08:14
  • https://stackoverflow.com/questions/21765447/set-a-selected-audio-file-as-ringtone refer this – Jinn Mar 20 '20 at 08:15
  • Should I use commit() function after 3 line of your code? – Aayush dual Mar 20 '20 at 08:15
  • uri currentTone was already declared final on the first line and is not storing the selected ringtone. The ringtone is selected at startActivityForResult() method. So storing uri doesn't make any sense. – Aayush dual Mar 20 '20 at 08:34
  • Shared preference can be accessed from any activity because its saved as xml file in storage. Create a SharedPreferenceManager class and add a Constructor for it. eg: https://www.javatips.net/api/SEPTA-Android-master/app/src/main/java/org/septa/android/app/managers/SharedPreferencesManager.java – Jinn Mar 20 '20 at 08:48