Question

is it possible to put a mp3 soundfile into my app folder and set this as the default ringtone / notification sound in the settings of my app?

For now I have a settings screen where you can select a ringtone that will be taken when a notification comes up from my app. This works but you can only select sounds existing on the device. What I want is to bring my own ringtone file to the device when installing the app. Is this possible? E.g. store the mp3 into the assets folder and call it there?

EDIT:

MyApp.class:

public class MyApp extends Application {

public static SharedPreferences sp;

@Override
public void onCreate() {
    super.onCreate();

    // initialize default settings values if not already done (does not overwrite existing settings)
    PreferenceManager.setDefaultValues(this, R.xml.settings, false);

    sp = PreferenceManager.getDefaultSharedPreferences(this);
}

public static String getRingtone() {
    return sp.getString("ringtone_pref", "DEFAULT_NOTIFICATION_URI");
}

public static boolean getVibrate() {
    return sp.getBoolean("vibrate_pref", true);
}
}

SettingsFragment.class:

public class SettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {

public void SettingFragment() {

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);
}

@Override
public void onResume() {
    super.onResume();
    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    getRingtoneTitle();
}

@Override
public void onPause() {
    super.onPause();
    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    getRingtoneTitle();
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    updatePreference(key);
}

private void updatePreference(String key) {
    Preference pref = findPreference(key);

    if (pref instanceof RingtonePreference) {
        Uri ringtoneUri = Uri.parse(KitchenHelper.getRingtone());
        Ringtone ringtone = RingtoneManager.getRingtone(this.getActivity(), ringtoneUri);
        if (ringtone != null) { pref.setSummary(ringtone.getTitle(this.getActivity())); }
    }
}

private void getRingtoneTitle() {
    Ringtone ringtone = RingtoneManager.getRingtone(this.getActivity(), Uri.parse(KitchenHelper.getRingtone()));
    if (ringtone != null) { findPreference("ringtone_pref").setSummary(ringtone.getTitle(this.getActivity())); }
}
}
Était-ce utile?

La solution

If you want to hard-code a particular sound to be used for your notification, then this thread should help.

If you still want to be able to select from a list of notification sounds, including your new one, then you'll need another method (which someone else should be able to help with).

Autres conseils

As long as you request the correct permissions you should be able to write to the SD card.

If you store the notification mp3 in /sdcard/media/notifications (create it if it doesn't exist) and rescan for new media then they should show up in the notifications list.

Rescan for new media:

scanFile(Context, filepaths, mimetypes (optional), callback (optional))

See http://developer.android.com/reference/android/media/MediaScannerConnection.html#scanFile(android.content.Context, java.lang.String[], java.lang.String[], android.media.MediaScannerConnection.OnScanCompletedListener)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top