0

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())); }
}
}
4

2 回答 2

0

如果您想硬编码用于通知的特定声音,那么这个线程应该会有所帮助。

如果您仍然希望能够从通知声音列表中进行选择,包括您的新声音,那么您将需要另一种方法(其他人应该能够提供帮助)。

于 2013-10-26T22:07:26.090 回答
0

只要您请求正确的权限,您就应该能够写入 SD 卡。

如果您将通知 mp3 存储在/sdcard/media/notifications(如果它不存在则创建它)并重新扫描新媒体,那么它们应该会显示在通知列表中。

重新扫描新媒体:

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

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

于 2013-10-26T22:26:27.730 回答