package hyz.com;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
public class RingTestActivity extends Activity implements OnClickListener
{
private ImageView myImage;
private ImageButton down,up,normal,mute,vibrate;
private ProgressBar myProgress;
private AudioManager audioManager;
private int volume = 0;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
myImage = (ImageView)findViewById(R.id.myImage);
myProgress = (ProgressBar)findViewById(R.id.myProgress);
down = (ImageButton)findViewById(R.id.downButton);
up = (ImageButton)findViewById(R.id.upButton);
normal = (ImageButton)findViewById(R.id.muteButton);
mute = (ImageButton)findViewById(R.id.downButton);
vibrate = (ImageButton)findViewById(R.id.vibrateButton);
down.setOnClickListener(this);
up.setOnClickListener(this);
normal.setOnClickListener(this);
mute.setOnClickListener(this);
vibrate.setOnClickListener(this);
//设置初始手机音量,设置初始声音模式
setRingerAndImage();
}
@Override
public void onClick(View view)
{
switch(view.getId())
{
case R.id.downButton://音量调小一格
audioManager.adjustVolume(AudioManager.ADJUST_LOWER, 0);
setRingerAndImage();
break;
case R.id.upButton://音量调大一格
audioManager.adjustVolume(AudioManager.ADJUST_RAISE, 0);
setRingerAndImage();
break;
case R.id.normalButton://铃声正常格式
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
setRingerAndImage();
break;
case R.id.muteButton://铃声静音格式
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
setRingerAndImage();
break;
case R.id.vibrateButton://铃声振动格式
audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
setRingerAndImage();
break;
default:
break;
}
}
public void setRingerAndImage()
{
volume = audioManager.getStreamVolume(AudioManager.STREAM_RING);
myProgress.setProgress(volume);
switch(audioManager.getRingerMode())
{
case AudioManager.RINGER_MODE_NORMAL:
myImage.setImageDrawable(getResources().getDrawable(R.drawable.normal));
break;
case AudioManager.RINGER_MODE_SILENT:
myImage.setImageDrawable(getResources().getDrawable(R.drawable.mute));
break;
case AudioManager.RINGER_MODE_VIBRATE:
myImage.setImageDrawable(getResources().getDrawable(R.drawable.vibrate));
break;
default:
break;
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/myText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_text1"
android:textSize="16sp"
android:textColor="@drawable/black"
android:layout_x="20px"
android:layout_y="42px"
>
</TextView>
<ImageView
android:id="@+id/myImage"
android:layout_width="48px"
android:layout_height="48px"
android:layout_x="110px"
android:layout_y="32px"
>
</ImageView>
<TextView
android:id="@+id/myText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_text2"
android:textSize="16sp"
android:textColor="@drawable/black"
android:layout_x="20px"
android:layout_y="102px"
>
</TextView>
<ProgressBar
android:id="@+id/myProgress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="160dip"
android:layout_height="wrap_content"
android:max="7"
android:progress="5"
android:layout_x="110px"
android:layout_y="102px"
>
</ProgressBar>
<ImageButton
android:id="@+id/downButton"
android:layout_width="100px"
android:layout_height="100px"
android:layout_x="50px"
android:layout_y="162px"
android:src="@drawable/down"
>
</ImageButton>
<ImageButton
android:id="@+id/upButton"
android:layout_width="100px"
android:layout_height="100px"
android:layout_x="150px"
android:layout_y="162px"
android:src="@drawable/up"
>
</ImageButton>
<ImageButton
android:id="@+id/normalButton"
android:layout_width="60px"
android:layout_height="60px"
android:layout_x="50px"
android:layout_y="272px"
android:src="@drawable/normal"
>
</ImageButton>
<ImageButton
android:id="@+id/muteButton"
android:layout_width="60px"
android:layout_height="60px"
android:layout_x="120px"
android:layout_y="272px"
android:src="@drawable/mute"
>
</ImageButton>
<ImageButton
android:id="@+id/vibrateButton"
android:layout_width="60px"
android:layout_height="60px"
android:layout_x="190px"
android:layout_y="272px"
android:src="@drawable/vibrate"
>
</ImageButton>
</AbsoluteLayout>
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="black">#000000</drawable>
<drawable name="white">#FFFFFF</drawable>
<drawable name="blue">#0000FF</drawable>
</resources>
strings.xml
<resources>
<string name="hello">Hello World, RingTestActivity!</string>
<string name="app_name">RingTest</string>
<string name="str_text1">聲音模式:</string>
<string name="str_text2">手機音量:</string>
</resources>