您的位置:首页 > 移动开发 > Android开发

Android设置手机音量的方法

2013-08-21 22:33 537 查看


这里功能点较多,但是好多都是雷同的,仔细看是可以看懂的

package dfzy.EX100;

/* import相关class */
import irdc.example175.R;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;

public class EX100 extends Activity
{
/* 变量声明 */
private ImageView myImage;
private ImageButton downButton;
private ImageButton upButton;
private ImageButton normalButton;
private ImageButton muteButton;
private ImageButton vibrateButton;
private ProgressBar myProgress;
private AudioManager audioMa;
private int volume=0;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

/* 对象初始化 */
audioMa = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
myImage = (ImageView)findViewById(R.id.myImage);
myProgress = (ProgressBar)findViewById(R.id.myProgress);
downButton = (ImageButton)findViewById(R.id.downButton);
upButton = (ImageButton)findViewById(R.id.upButton);
normalButton = (ImageButton)findViewById(R.id.normalButton);
muteButton = (ImageButton)findViewById(R.id.muteButton);
vibrateButton = (ImageButton)findViewById(R.id.vibrateButton);

/* 设置初始的手机音量 */
volume=audioMa.getStreamVolume(AudioManager.STREAM_RING);
myProgress.setProgress(volume);
/* 设置初始的声音模式 */
int mode=audioMa.getRingerMode();
if(mode==AudioManager.RINGER_MODE_NORMAL)
{
myImage.setImageDrawable(getResources()
.getDrawable(R.drawable.normal));
}
else if(mode==AudioManager.RINGER_MODE_SILENT)
{
myImage.setImageDrawable(getResources()
.getDrawable(R.drawable.mute));
}
else if(mode==AudioManager.RINGER_MODE_VIBRATE)
{
myImage.setImageDrawable(getResources()
.getDrawable(R.drawable.vibrate));
}

/* 音量调小声的Button */
downButton.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0)
{
/* 设置音量调小声一格 */
audioMa.adjustVolume(AudioManager.ADJUST_LOWER, 0);
volume=audioMa.getStreamVolume(AudioManager.STREAM_RING);
myProgress.setProgress(volume);
/* 设置调整后声音模式 */
int mode=audioMa.getRingerMode();
if(mode==AudioManager.RINGER_MODE_NORMAL)
{
myImage.setImageDrawable(getResources()
.getDrawable(R.drawable.normal));
}
else if(mode==AudioManager.RINGER_MODE_SILENT)
{
myImage.setImageDrawable(getResources()
.getDrawable(R.drawable.mute));
}
else if(mode==AudioManager.RINGER_MODE_VIBRATE)
{
myImage.setImageDrawable(getResources()
.getDrawable(R.drawable.vibrate));
}
}
});

/* 音量调大声的Button */
upButton.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0)
{
/* 设置音量调大声一格 */
audioMa.adjustVolume(AudioManager.ADJUST_RAISE, 0);
volume=audioMa.getStreamVolume(AudioManager.STREAM_RING);
myProgress.setProgress(volume);
/* 设置调整后的声音模式 */
int mode=audioMa.getRingerMode();
if(mode==AudioManager.RINGER_MODE_NORMAL)
{
myImage.setImageDrawable(getResources()
.getDrawable(R.drawable.normal));
}
else if(mode==AudioManager.RINGER_MODE_SILENT)
{
myImage.setImageDrawable(getResources()
.getDrawable(R.drawable.mute));
}
else if(mode==AudioManager.RINGER_MODE_VIBRATE)
{
myImage.setImageDrawable(getResources()
.getDrawable(R.drawable.vibrate));
}
}
});

/* 调整铃声模式为正常模式的Button */
normalButton.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0)
{
/* 设置铃声模式为NORMAL */
audioMa.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
/* 设置音量与声音模式 */
volume=audioMa.getStreamVolume(AudioManager.STREAM_RING);
myProgress.setProgress(volume);
myImage.setImageDrawable(getResources()
.getDrawable(R.drawable.normal));
}
});

/* 调整铃声模式为静音模式的Button */
muteButton.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0)
{
/* 设置铃声模式为SILENT */
audioMa.setRingerMode(AudioManager.RINGER_MODE_SILENT);
/* 设置音量与声音状态 */
volume=audioMa.getStreamVolume(AudioManager.STREAM_RING);
myProgress.setProgress(volume);
myImage.setImageDrawable(getResources()
.getDrawable(R.drawable.mute));
}
});

/* 调整铃声模式为震动模式的Button */
vibrateButton.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0)
{
/* 设置铃声模式为VIBRATE */
audioMa.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
/* 设置音量与声音状态 */
volume=audioMa.getStreamVolume(AudioManager.STREAM_RING);
myProgress.setProgress(volume);
myImage.setImageDrawable(getResources()
.getDrawable(R.drawable.vibrate));
}
});
}
}


2
<?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>

3

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: