(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. jsp、Servlet相关知识介绍(转)

    1.servlet生命周期 所谓生命周期,指的是servlet容器如何创建servlet实例.分配其资源.调用其方法.并销毁其实例的整个过程. 阶段一: 实例化(就是创建servlet对象,调用构造器 ...

  2. UVa 10397 Connect the Campus

    最小生成树 Kruskal #include<cmath> #include<iostream> #include<cstdio> #include<algo ...

  3. MEF初体验之一:在应用程序宿主MEF

    在MEF出现以前,其实微软已经发布了一个类似的框架,叫MAF(Managed Add-in Framework),它旨在使应用程序孤立和更好的管理扩展,而MEF更关心的是可发现性.扩展性和轻便性,后者 ...

  4. ASP.NET 5 Beta8 发布

    ASP.NET 5 Beta8 发布 ASP.NET 5 的路线图(详见 ASP.NET 5 Schedule and Roadmap : https://github.com/aspnet/home ...

  5. Behavioral模式之Observer模式

    1.意图 定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,全部依赖于它的对象都得到通知并被自己主动更新. 2.别名 依赖(dependents).公布-订阅(Publish-Subscr ...

  6. HDU 1711 Number Sequence(kmp)

    Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...

  7. POJ 3979 分数减法【数学问题的探讨】

    将a/b和c/d简化一下就可以了 分数加减法 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12588   Accepted ...

  8. C#分布式缓存Couchbase

    C#分布式缓存Couchbase使用 一.简介 目前C#业界使用得最多的 Cache 系统主要是 Memcached和 Redis. 这两个 Cache 系统可以说是比较成熟的解决方案,也是很多系统当 ...

  9. hdu 5072 Coprime(同色三角形+容斥)

    pid=5072">http://acm.hdu.edu.cn/showproblem.php?pid=5072 单色三角形模型 现场赛和队友想了3个小时,最后发现想跑偏了.感觉好可惜 ...

  10. Linux Kernel系列 - 黄牛X内核代码凝视

    Hanks.Wang - 专注于操作系统与移动安全研究.Linux-Kernel/SELinux/SEAndroid/TrustZone/Encription/MDM    Mail - byhank ...