public class Main extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != RESULT_OK) { return; } Uri uri = data .getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); if (uri != null) { switch (requestCode) { case 1: RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, uri); break; case 2: RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM, uri); break; case 3: RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION, uri); break; default: break; } } } public void onClick_Call_Ringtone(View view) { Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "來電鈴聲"); startActivityForResult(intent, 1); } public void onClick_Alarm_Ringtone(View view) { Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_ALARM); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "鬧鐘鈴聲"); startActivityForResult(intent, 2); } public void onClick_Notification_Ringtone(View view) { Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "通知鈴聲"); startActivityForResult(intent, 3); } }
鈴聲存儲位置html
/system/media/audio/android
當這裏有一個問題,若是想設置用戶自定義的鈴聲,須要將鈴聲放到指定目錄,但通常用戶 沒有root權限,system不可見甚至不可讀,所以,解決此類問題,ide
android提供了sdcard擴展this
/sdcard/music目錄下的鈴聲也可被設置爲鈴聲spa
參考http://www.cnblogs.com/maxinliang/archive/2013/07/02/3167212.htmlcode
//音量控制,初始化定義 AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); //最大音量 int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); //當前音量 int currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
設置靜音和標準音量切換htm
if(isSilent){ mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0); }else{ mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, tempVolume, 0); //tempVolume:音量絕對值 }
彈出系統控制條blog
//下降音量,調出系統音量控制 if(flag == 0){ mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER, AudioManager.FX_FOCUS_NAVIGATION_UP); } //增長音量,調出系統音量控制 else if(flag == 1){ mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE, AudioManager.FX_FOCUS_NAVIGATION_UP); }