我們在拿到新手機後通常會為其設置來年鈴聲,那麼怎樣通過代碼來設置Android來電鈴聲,本文就為大家實例講解下。
1、如果讀到的是音頻文件路徑,需要先將音樂文件插入到多媒體庫。
Java代碼
- //設置--鈴聲的具體方法
- 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------鬧鈴音");
- }
2、如果讀取多媒體庫的音頻文件,設為鈴聲,使用以下方式:
首先寫一個常量類(定義想要設置為那種鈴聲的標示):
AppConstant.java
Java代碼
- public interface AppConstant {
- public static final int RINGTONE = 0; //鈴聲
- public static final int NOTIFICATION = 1; //通知音
- public static final int ALARM = 2; //鬧鐘
- public static final int ALL = 3; //所有聲音
- }
此方法需要傳入想要設置為鈴聲的全路徑(如:/mnt/sdcard/mp3/a.mp3),和想要設置為哪種鈴聲的標示:
Java代碼
- private void setVoice(String path2,int id)
- {
- ContentValues cv = new ContentValues();
- Uri newUri = null;
- Uri uri = MediaStore.Audio.Media.getContentUriForPath(path2);
-
- // 查詢音樂文件在媒體庫是否存在
- Cursor cursor = this.getContentResolver().query(uri, null, MediaStore.MediaColumns.DATA + "=?", new String[] { path2 },null);
- if (cursor.moveToFirst() && cursor.getCount() > 0)
- {
- String _id = cursor.getString(0);
- switch (id) {
- case AppConstant.RINGTONE:
- cv.put(MediaStore.Audio.Media.IS_RINGTONE, true);
- cv.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
- cv.put(MediaStore.Audio.Media.IS_ALARM, false);
- cv.put(MediaStore.Audio.Media.IS_MUSIC, false);
- break;
- case AppConstant.NOTIFICATION:
- cv.put(MediaStore.Audio.Media.IS_RINGTONE, false);
- cv.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
- cv.put(MediaStore.Audio.Media.IS_ALARM, false);
- cv.put(MediaStore.Audio.Media.IS_MUSIC, false);
- break;
- case AppConstant.ALARM:
- cv.put(MediaStore.Audio.Media.IS_RINGTONE, false);
- cv.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
- cv.put(MediaStore.Audio.Media.IS_ALARM, true);
- cv.put(MediaStore.Audio.Media.IS_MUSIC, false);
- break;
- case AppConstant.ALL:
- cv.put(MediaStore.Audio.Media.IS_RINGTONE, true);
- cv.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
- cv.put(MediaStore.Audio.Media.IS_ALARM, true);
- cv.put(MediaStore.Audio.Media.IS_MUSIC, false);
- break;
- default:
- break;
- }
-
- // 把需要設為鈴聲的歌曲更新鈴聲庫
- getContentResolver().update(uri, cv, MediaStore.MediaColumns.DATA + "=?",new String[] { path2 });
- newUri = ContentUris.withAppendedId(uri, Long.valueOf(_id));
- // 一下為關鍵代碼:
- switch (id) {
- case AppConstant.RINGTONE:
- RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri);
- break;
- case AppConstant.NOTIFICATION:
- RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION, newUri);
- break;
- case AppConstant.ALARM:
- RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM, newUri);
- break;
- case AppConstant.ALL:
- RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALL, newUri);
- break;
- default:
- break;
- }
-
- //播放鈴聲
- //Ringtone rt = RingtoneManager.getRingtone(this, newUri);
- //rt.play();
- }
- }