1
我的應用中有兩個按鈕。一個用於將聲音文件設置爲鈴聲,另一個用於通知。我可以將聲音文件設置爲鈴聲,但當我嘗試將其設置爲通知時,它會刪除之前添加的鈴聲。聲音文件僅作爲通知顯示。我想通過單獨的按鈕將鈴聲和通知設置爲聲音文件。對於鈴聲在Android中以編程方式設置聲音文件作爲鈴聲和通知
代碼:
File k = new File(baseDir,fileName);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "Voicetransform ");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/wav");
values.put(MediaStore.MediaColumns.SIZE, k.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, false);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k
.getAbsolutePath());
context.getContentResolver().delete(
uri,
MediaStore.MediaColumns.DATA + "=\""
+ k.getAbsolutePath() + "\"", null);
Uri newUri = context.getContentResolver().insert(uri, values);
try {
RingtoneManager.setActualDefaultRingtoneUri(
context, RingtoneManager.TYPE_RINGTONE,
newUri);
} catch (Throwable t) {
}
代碼通知:
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "Voicetransform1");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/wav");
values.put(MediaStore.MediaColumns.SIZE, k.length());
values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
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(k
.getAbsolutePath());
context.getContentResolver().delete(
uri,
MediaStore.MediaColumns.DATA + "=\""
+ k.getAbsolutePath() + "\"", null);
Uri newUri = context.getContentResolver().insert(uri, values);
try {
RingtoneManager.setActualDefaultRingtoneUri(
context, RingtoneManager.TYPE_NOTIFICATION,newUri);
} catch (Throwable t) {
}
File k = new File(baseDir,fileName);