安卓专题
安卓声音管理器案例与源码
Android系统提供了对铃声的控制,我们能够通过 AudioManager 类控制铃声的音量和类型,在这个案例中展示如何改变铃声的模式(大,振动,沉默)以及音量。
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current Ring Mode: -"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RadioGroup
android:id="@+id/radioRinger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" >
<RadioButton
android:id="@+id/radioNormal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:checked="true"
android:text="Normal" />
<RadioButton
android:id="@+id/radioSilent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Silent" />
<RadioButton
android:id="@+id/vibrate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Vibrate" />
</RadioGroup>
<Button
android:id="@+id/mode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Choose Mode" />
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Adjust the volume:" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<Button
android:id="@+id/increase"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
<Button
android:id="@+id/decrease"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
</LinearLayout>
</LinearLayout>
MainActivity.java:
public class MainActivity extends Activity {
private Button modeBtn;
private Button increaseBtn;
private Button decreaseBtn;
private RadioButton normal;
private RadioButton silent;
private RadioGroup ringGroup;
private TextView status;
private AudioManager myAudioManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
normal = (RadioButton) findViewById(R.id.radioNormal);
silent = (RadioButton) findViewById(R.id.radioSilent);
status = (TextView) findViewById(R.id.text);
ringGroup = (RadioGroup) findViewById(R.id.radioRinger);
modeBtn = (Button)findViewById(R.id.mode);
modeBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int selectedId = ringGroup.getCheckedRadioButtonId();
// find which radioButton is checked by id
if(selectedId == silent.getId()) {
silentEnable(v);
} else if(selectedId == normal.getId()) {
normalEnable(v);
} else {
vibrateEnable(v);
}
}
});
increaseBtn = (Button) findViewById(R.id.increase);
increaseBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// increase the volume and show the ui
myAudioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
Toast.makeText(getApplicationContext(), "increase volume",
Toast.LENGTH_SHORT).show();
}
});
decreaseBtn = (Button) findViewById(R.id.decrease);
decreaseBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// decrease the volume and show the ui
myAudioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
Toast.makeText(getApplicationContext(), "decrease volume",
Toast.LENGTH_SHORT).show();
}
});
// get the instance of AudioManager class
myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
}
public void vibrateEnable(View view){
// set the ring mode to vibrate
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
status.setText("Current Status: Vibrate Mode");
}
public void normalEnable(View view){
// set the ring mode to loud设置吵闹
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
status.setText("Current Status: Ring Mode");
}
public void silentEnable(View view){
// set the ring mode to silent设置安静
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
status.setText("Current Status: Silent Mode");
}
}
运行效果: