程式設計工藝大師http://tccnchsu.blogspot.com/
範例來源 http://cooking-java.blogspot.com/search/label/Android
1. 音量調整主要是透過AudioManager來對手機音量進行調整,可以調整的模式有:正常模式、靜音模式、震動模式、音量增大、音量減少。
2.AudioManagerExample .java
3. main.xml(Layout)
4. 執行畫面
範例來源 http://cooking-java.blogspot.com/search/label/Android
1. 音量調整主要是透過AudioManager來對手機音量進行調整,可以調整的模式有:正常模式、靜音模式、震動模式、音量增大、音量減少。
2.AudioManagerExample .java
01.
package
tw.nicky;
02.
import
android.app.Activity;
03.
import
android.content.Context;
04.
import
android.media.AudioManager;
05.
import
android.os.Bundle;
06.
import
android.view.View;
07.
import
android.widget.Button;
08.
09.
public
class
AudioManagerExample
extends
Activity {
10.
private
Button normalButn;
11.
private
Button vibrateButn;
12.
private
Button silentButn;
13.
private
Button upButn;
14.
private
Button downButn;
15.
private
AudioManager audioManager;
16.
17.
@Override
18.
public
void
onCreate(Bundle savedInstanceState) {
19.
super
.onCreate(savedInstanceState);
20.
setContentView(R.layout.main);
21.
normalButn = (Button)findViewById(R.id.normalButn);
22.
vibrateButn = (Button)findViewById(R.id.vibrateButn);
23.
silentButn = (Button)findViewById(R.id.silentButn);
24.
upButn = (Button)findViewById(R.id.upButn);
25.
downButn = (Button)findViewById(R.id.downButn);
26.
27.
//取得音量控制器
28.
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
29.
30.
//正常模式
31.
normalButn.setOnClickListener(
new
View.OnClickListener() {
32.
@Override
33.
public
void
onClick(View v) {
34.
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
35.
}
36.
});
37.
38.
//震動模式
39.
vibrateButn.setOnClickListener(
new
View.OnClickListener() {
40.
41.
@Override
42.
public
void
onClick(View v) {
43.
audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
44.
}
45.
});
46.
47.
//靜音模式
48.
silentButn.setOnClickListener(
new
View.OnClickListener() {
49.
@Override
50.
public
void
onClick(View v) {
51.
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
52.
}
53.
});
54.
55.
//增大音量
56.
upButn.setOnClickListener(
new
View.OnClickListener() {
57.
@Override
58.
public
void
onClick(View v) {
59.
audioManager.adjustVolume(AudioManager.ADJUST_RAISE,
0
);
60.
}
61.
});
62.
63.
//減少音量
64.
downButn.setOnClickListener(
new
View.OnClickListener() {
65.
@Override
66.
public
void
onClick(View v) {
67.
audioManager.adjustVolume(AudioManager.ADJUST_LOWER,
0
);
68.
}
69.
});
70.
}
71.
}
3. main.xml(Layout)
01.
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02.
<
AbsoluteLayout
03.
android:id
=
"@+id/widget0"
04.
android:layout_width
=
"fill_parent"
05.
android:layout_height
=
"fill_parent"
06.
xmlns:android
=
"http://schemas.android.com/apk/res/android"
07.
>
08.
<
Button
09.
android:id
=
"@+id/vibrateButn"
10.
android:layout_width
=
"wrap_content"
11.
android:layout_height
=
"wrap_content"
12.
android:text
=
"震動"
13.
android:layout_x
=
"106px"
14.
android:layout_y
=
"16px"
15.
>
16.
</
Button
>
17.
<
Button
18.
android:id
=
"@+id/silentButn"
19.
android:layout_width
=
"wrap_content"
20.
android:layout_height
=
"wrap_content"
21.
android:text
=
"靜音"
22.
android:layout_x
=
"189px"
23.
android:layout_y
=
"14px"
24.
>
25.
</
Button
>
26.
<
Button
27.
android:id
=
"@+id/normalButn"
28.
android:layout_width
=
"wrap_content"
29.
android:layout_height
=
"wrap_content"
30.
android:text
=
"正常"
31.
android:layout_x
=
"28px"
32.
android:layout_y
=
"17px"
33.
>
34.
</
Button
>
35.
<
Button
36.
android:id
=
"@+id/upButn"
37.
android:layout_width
=
"wrap_content"
38.
android:layout_height
=
"wrap_content"
39.
android:text
=
"大聲"
40.
android:layout_x
=
"66px"
41.
android:layout_y
=
"97px"
42.
>
43.
</
Button
>
44.
<
Button
45.
android:id
=
"@+id/downButn"
46.
android:layout_width
=
"wrap_content"
47.
android:layout_height
=
"wrap_content"
48.
android:text
=
"小聲"
49.
android:layout_x
=
"144px"
50.
android:layout_y
=
"97px"
51.
>
52.
</
Button
>
53.
</
AbsoluteLayout
>
4. 執行畫面
沒有留言:
張貼留言