我正在尝试让一个 Android 程序设置手机的铃声。当我运行以下代码一次时,铃声设置正确并且工作正常。但是,如果我多次运行代码,铃声就会变为静音。任何帮助解决这个问题将不胜感激。
void setRingtone() {
//File path = Environment.getExternalStorageDirectory();
//File file = new File(path, "ringtone.mp3");
File file = new File("/sdcard/", "ringtone.mp3");
Uri mUri = Uri.parse("android.resource://com.*****.*****/" + R.raw.*****);
ContentResolver mCr = getContentResolver();
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "my ringtone");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/oog");
values.put(MediaStore.MediaColumns.SIZE, file.length());
values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());
Uri newUri = mCr.insert(uri, values);
try {
RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE, newUri);
} catch (Throwable t) {}
}