Android设置手机音量的方法

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

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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静山晚风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值