(1)项目介绍

于android API的AudioManager于,它提供了一种方法来调整电话音量。

audioMa.adjustVolume(AudioManager.ADJUST_LOWER, 0);
audioMa.adjustVolume(AudioManager.ADJUST_RAISE, 0);

也能够调节手机声音的模式为震动或者静音

audioMa.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

audioMa.setRingerMode(AudioManager.RINGER_MODE_SILENT);

audioMa.setRingerMode(AudioManager.RINGER_MODE_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)代码:

package com.liuzuyi.soundmode;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar; public class MainActivity 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;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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));
}
downbutton.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
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));
} }
}); upbutton.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
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));
} }
});
normalbutton.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
audioMa.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
volume = audioMa.getStreamVolume(AudioManager.STREAM_RING);
myprogress.setProgress(volume);
myimage.setImageDrawable(getResources().getDrawable(R.drawable.normal));
}
});
mutebutton.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
audioMa.setRingerMode(AudioManager.RINGER_MODE_SILENT);
volume = audioMa.getStreamVolume(AudioManager.STREAM_RING);
myprogress.setProgress(volume);
myimage.setImageDrawable(getResources().getDrawable(R.drawable.mute));
}
});
vibratebutton.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
audioMa.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
volume = audioMa.getStreamVolume(AudioManager.STREAM_RING);
myprogress.setProgress(volume);
myimage.setImageDrawable(getResources().getDrawable(R.drawable.vibrate));
}
}); } }

版权声明:本文博客原创文章,博客,未经同意,不得转载。

android 控制手机的体积的大小 切换音效模式的更多相关文章

  1. 如何消除手机设置的字体大小对Cordova app(Android)界面font-size的影响

    ===================== 更新分割线 =================== 现在发现其实不需要用安卓编辑器打开,也能找到这个文件,路径是platforms\android\Cord ...

  2. 电脑控制手机的另一选择——android vnc server

    近来发现的Android上的原生VNC Server,就是说只要手机上安装并运行这个软件,即可实现电脑上查看并控制手机了. 首先是手机端. 1)下载androidvncserver: http://c ...

  3. Android开发之控制手机音频

    本实例通过MediaPlayer播放一首音乐并通过AudioManager控制手机音频.关于AudioManager的具体解释可參照:Android开发之AudioManager(音频管理器)具体解释 ...

  4. 腾讯技术分享:Android版手机QQ的缓存监控与优化实践

    本文内容整理自公众号腾讯Bugly,感谢原作者的分享. 1.问题背景 对于Android应用来说,内存向来是比较重要的性能指标.内存占用过高,会影响应用的流畅度,甚至引发OOM,非常影响用户体验.因此 ...

  5. Android之手机向导以及设置中心模块的开发

    当我们使用的新的软件的时候,我们首先需要教用户如何使用我们的软件,当用户学习完使用教程,下次再登录的时候,我们应该直接跳到我们的功能界面,下面我来展示一下我学习视频做的效果图:手机防盗这个功能模块就是 ...

  6. 使用Vibrator控制手机振动

    import android.os.Bundle;import android.os.Vibrator;import android.app.Activity;import android.app.S ...

  7. Vibrator控制手机震动

    Vibrator控制手机震动 效果图 源码 下载地址(Android Studio工程):http://download.csdn.net/detail/q4878802/9049755 添加权限 & ...

  8. 在Airtest中如何使用无线模式控制手机

    在使用Airtest超快速开发App爬虫文章的最后,我们留了一个尾巴:如何启动Airtest的无线模式,不用USB线就能控制手机? 本文将会讲到具体的做法.做法分为两种:第一种是在Airtest的ID ...

  9. Android获取手机分辨率DisplayMetircs类

    关于Android中手机分辨率的使用 Android 可设置为随着窗口大小调整缩放比例,但即便如此,手机程序设计人员还是必须知道手机屏幕的边界,以避免缩放造成的布局变形问题. 手机的分辨率信息是手机的 ...

随机推荐

  1. 容易centos配置docker维修

    首先.由于docker实施需要linux某些组件支持本身和内核特性.所以一定要确保centos版本号大于6,和内核版本号大于2.6.32-431.可轻松升级centos6到最新的版本号. sudo y ...

  2. SQL Server 2008 R2 性能计数器详细列表(五)

    原文:SQL Server 2008 R2 性能计数器详细列表(五) SQL Server:SQL Statistics 对象: 监视编译和发送到 SQL Server 实例的请求类型 SQL Ser ...

  3. Python网络02 Python服务器进化

    原文:Python网络02 Python服务器进化 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! **注意,在Python 3. ...

  4. 用Ghostscript API将PDF格式转换为图像格式(C#)

    原文:用Ghostscript API将PDF格式转换为图像格式(C#) 由于项目需要在.net下将pdf转换为普通图像格式,在网上搜了好久终于找到一个解决方案,于是采用拿来主义直接用.来源见代码中注 ...

  5. 实现键值对存储(三):Kyoto Cabinet 和LevelDB的架构比較分析

    译自  Emmanuel Goossaert (CodeCapsule.com) 在本文中,我将会逐组件地把Kyoto Cabinet 和 LevelDB的架构过一遍.目标和本系列第二部分讲的差点儿相 ...

  6. 为什么 Linux Mint 比 Ubuntu好?

    Linux Mint由Linux Mint Team团队于2006年开始发行,是一份基于Debian和Ubuntu的Linux发行版.其目标是提供一种更完整的即刻可用体验,这包括提供浏览器插件.多媒体 ...

  7. Ruby 一些经常使用的细节

    1.try 永远不会抛出异常 在 没有的时候 返回 nil province_id = Province.find_by_name(prov).try(:id) 2.find(:first, :con ...

  8. HDU 1505 Largest Rectangle in a Histogram &amp;&amp; HDU 1506 City Game(动态规划)

    1506意甲冠军:给你一个连续的直方图(拼贴底部长度1).求连续基质区. 对每一个直方图,分别向左向右进行扩展. #include<cstdio> #include<stdlib.h ...

  9. 常见的FPGA内串行数据采样的方式

    总结下常见的对串行数据采样的三种方式: 1. 全采样存储方式: 采用过采样,用过采样时钟,用移位寄存器移位,把每次采样值都存起来.采用高速的过采样时钟运行. 然后等待触发条件,(就是找到数据的起始点条 ...

  10. adb这点小事——远程adb调试

    欢迎转载.转载请注明:http://blog.csdn.net/zhgxhuaa 1.   前言 1.1.  写在前面的话 在之前的一篇文章<360电视助手实现研究>中介绍了在局域网内直接 ...