如果读到的是音频文件路径,需要先将音乐文件插入到多媒体库。如:path传入:/mnt/sdcard/mp3/a.mp3
-
-
public void setMyRingtone(String path)
- {
-
File sdfile = new File(path);
-
ContentValues values = new ContentValues();
- values.put(MediaStore.MediaColumns.DATA, sdfile.getAbsolutePath());
- values.put(MediaStore.MediaColumns.TITLE, sdfile.getName());
-
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
-
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
-
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
-
values.put(MediaStore.Audio.Media.IS_ALARM, false);
-
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
-
- Uri uri = MediaStore.Audio.Media.getContentUriForPath(sdfile.getAbsolutePath());
-
Uri newUri = this.getContentResolver().insert(uri, values);
-
RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri);
-
Toast.makeText( getApplicationContext (),"设置来电铃声成功!", Toast.LENGTH_SHORT ).show();
-
System.out.println("setMyRingtone()-----铃声");
- }
-
-
-
public void setMyNotification(String path)
- {
-
-
File sdfile = new File(path);
-
ContentValues values = new ContentValues();
- values.put(MediaStore.MediaColumns.DATA, sdfile.getAbsolutePath());
- values.put(MediaStore.MediaColumns.TITLE, sdfile.getName());
-
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
-
values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
-
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
-
values.put(MediaStore.Audio.Media.IS_ALARM, false);
-
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
-
- Uri uri = MediaStore.Audio.Media.getContentUriForPath(sdfile.getAbsolutePath());
-
Uri newUri = this.getContentResolver().insert(uri, values);
-
-
RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION, newUri);
-
Toast.makeText( getApplicationContext (),"设置通知铃声成功!", Toast.LENGTH_SHORT ).show();
-
System.out.println("setMyNOTIFICATION-----提示音");
- }
-
-
public void setMyAlarm(String path)
- {
-
File sdfile = new File(path);
-
ContentValues values = new ContentValues();
- values.put(MediaStore.MediaColumns.DATA, sdfile.getAbsolutePath());
- values.put(MediaStore.MediaColumns.TITLE, sdfile.getName());
-
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
-
values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
-
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(sdfile.getAbsolutePath());
-
Uri newUri = this.getContentResolver().insert(uri, values);
-
RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM, newUri);
-
Toast.makeText( getApplicationContext (),"设置闹钟铃声成功!", Toast.LENGTH_SHORT ).show();
-
System.out.println("setMyNOTIFICATION------闹铃音");
- }
Android开发之手机铃声代码实现,布布扣,bubuko.com
Android开发之手机铃声代码实现