(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. 设置状态栏样式Demo

    达到的效果: 色有黑色变为了白色 //设置状态条的样式 - (UIStatusBarStyle)preferredStatusBarStyle { returnUIStatusBarStyleLigh ...

  2. SqlServer 添加列并赋值

    有个需求,需要给某张表添加一列并且赋值,分解需求,一共分两部走: 添加列 赋值 两个功能都不难,很快实现. --add column alter table Med_Summary_Template ...

  3. POJ1458 Common Subsequence 【最长公共子序列】

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37614   Accepted: 15 ...

  4. 【Android小应用】强迫症头像生成器

    近期一段时间在微信朋友圈,在头像的右上角添加一个红底白字的数字,让非常多有强迫症的同学点个不停,深深佩服发明这样的头像的姑娘,太机智了.但它不能自己定义,这是硬伤.... 这是朋友圈里的效果图: 这个 ...

  5. js 执行一个字符串类型的函数

    两种方法,一种是加括号一种是加叹号,new Function()的方法没有成功. var a = "function(args){console.log(args)}" undef ...

  6. 解决LINUX vncserver 启动 could not open default font &#39;fixed&#39;错.

    安装vncserver例如,会发生以下错误:  vncext:      VNC extension running!  vncext:      Listening for VNC connecti ...

  7. 在ABP项目的应用Castle Windsor

    Castle Windsor常用介绍以及其在ABP项目的应用介绍 最近在研究ABP项目,有关ABP的介绍请看阳光铭睿 博客,ABP的DI和AOP框架用的是Castle Windsor下面就对Castl ...

  8. android(9)_数据存储和访问3_scard基本介绍

    使用Activity的openFileOutput()保存文件的方法,文件存储在手机空间,通常情况下,手机的存储空间不是很大,存储小文件确定.假设你要存储大文件,如视频,是不可行. 对于这样大的文件, ...

  9. 采用max_dump_file_size 参数限制trc文件大小

    max_dump_file_size 参数:这个参数可以限制相应的过程trc文件大小(进程是否是oracle后台和前台应用程序对应的server process) 使用案例: 若是一个trc文件已经4 ...

  10. 金蝶K3无法创建数据库,请查看该文件夹的错误的解决方法。

    无法创建数据库! 检查你的文件夹C:\XXX\DATA是否存在.并且该系统是不低,或SQL Server服务的启动用户不具备<K3ERP\DBFILE>文件夹的写权限.请改动Windows ...