
- Android 基础
- Android - 主页
- Android - 概述
- Android - 环境设置
- Android - 架构
- Android - 应用组件
- Android - Hello World 示例
- Android - 资源
- Android - 活动
- Android - 服务
- Android - 广播接收器
- Android - 内容提供器
- Android - 碎片
- Android - 意图/过滤器
- Android - 用户界面
- Android - UI 布局
- Android - UI 控件
- Android - 事件处理
- Android - 样式和主题
- Android - 自定义组件
- Android 高级概念
- Android - 拖放
- Android - 通知
- 基于位置的服务
- Android - 发送电子邮件
- Android - 发送短信
- Android - 电话呼叫
- 发布 Android 应用
- Android 实用示例
- Android - 警报对话框
- Android - 动画
- Android - 音频捕获
- Android - 音频管理器
- Android - 自动完成
- Android - 最佳实践
- Android - 蓝牙
- Android - 摄像头
- Android - 剪贴板
- Android - 自定义字体
- Android - 数据备份
- Android - 开发者工具
- Android - 模拟器
- Android - Facebook 集成
- Android - 手势
- Android - 谷歌地图
- Android - 图像效果
- Android - ImageSwitcher
- Android - 内部存储
- Android - JetPlayer
- Android - JSON 解析器
- Android - Linkedin 集成
- Android - 加载微调器
- Android - 本地化
- Android - 登录屏幕
- Android - MediaPlayer
- Android - 多点触控
- Android - 导航
- Android - 网络连接
- Android - NFC 指南
- Android - PHP/MySQL
- Android - 进度圆圈
- Android - 进度条
- Android - 推送通知
- Android - RenderScript
- Android - RSS 阅读器
- Android - 屏幕录制
- Android - SDK 管理器
- Android - 传感器
- Android - 会话管理
- Android - 共享首选项
- Android - SIP 协议
- Android - 拼写检查器
- Android - SQLite 数据库
- Android - 支持库
- Android - 测试
- Android - 文字转语音
- Android - TextureView
- Android - Twitter 集成
- Android - UI 设计
- Android - UI 模式
- Android - UI 测试
- Android - WebView 布局
- Android - Wi-Fi
- Android - 小部件
- Android - XML 解析器
- Android 实用资源
- Android - 问题与解答
- Android - 实用资源
- Android - 讨论
Android - 音频管理器
您可以轻松控制 Android 中的铃声音量和铃声配置文件,例如:静音、振动、响铃等。Android 提供了 AudioManager 类,可访问这些控件。
为了使用 AndroidManager 类,您必须首先通过调用 getSystemService() 方法创建 AudioManager 类的对象。语法如下所示。
private AudioManager myAudioManager; myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
一旦您实例化了 AudioManager 类的对象,就可以使用 setRingerMode 方法设置设备的音频或铃声配置文件。语法如下所示。
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
setRingerMode 方法将整数作为参数。对于每种模式,都会分配一个整数,用于区分不同的模式。可能的模式是。
序号 | 模式和描述 |
---|---|
1 |
RINGER_MODE_VIBRATE 此模式将设备设置为振动模式。 |
2 |
RINGER_MODE_NORMAL 此模式将设备设置为正常(响亮)模式。 |
3 |
RINGER_MODE_SILENT 此模式将设备设置为静音模式。 |
设置模式后,您可以调用 getRingerMode() 方法获取系统的设置状态。语法如下所示。
int mod = myAudioManager.getRingerMode();
除了 getRingerMode 方法外,AudioManager 类中还有其他方法可用于控制音量和其他模式。它们列在下面。
序号 | 方法和描述 |
---|---|
1 |
adjustVolume(int direction, int flags) 此方法调整最相关的流的音量 |
2 | getMode() 此方法返回当前音频模式 |
3 | getStreamMaxVolume(int streamType) 此方法返回特定流的最大音量索引 |
4 | getStreamVolume(int streamType) 此方法返回特定流的当前音量索引 |
5 | isMusicActive() 此方法检查是否有音乐正在播放。 |
6 | startBluetoothSco() 此方法启动蓝牙 SCO 音频连接 |
7 | stopBluetoothSco() 此方法停止蓝牙 SCO 音频连接。 |
示例
以下示例演示了 AudioManager 类的用法。它创建了一个应用程序,允许您为设备设置不同的铃声模式。
要试验此示例,您需要在实际设备上运行它。
步骤 | 描述 |
---|---|
1 | 您将使用 Android Studio IDE 在包 com.example.sairamkrishna.myapplication 下创建一个 Android 应用程序。 |
2 | 修改 src/MainActivity.java 文件以添加 AudioManager 代码 |
3 | 修改布局 XML 文件 res/layout/activity_main.xml,如有必要添加任何 GUI 组件。 |
4 | 修改 res/values/string.xml 文件并添加必要的字符串组件。 |
5 | 修改 AndroidManifest.xml 以添加必要的权限。 |
6 | 运行应用程序并选择正在运行的 Android 设备,将应用程序安装到设备上并验证结果。 |
以下是 src/MainActivity.java 的内容
package com.example.sairamkrishna.myapplication; 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.Toast; public class MainActivity extends Activity { Button mode,ring,vibrate,silent; private AudioManager myAudioManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vibrate=(Button)findViewById(R.id.button3); ring=(Button)findViewById(R.id.button2); mode=(Button)findViewById(R.id.button); silent=(Button)findViewById(R.id.button4); myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); vibrate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE); Toast.makeText(MainActivity.this,"Now in Vibrate Mode", Toast.LENGTH_LONG).show(); } }); ring.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); Toast.makeText(MainActivity.this,"Now in Ringing Mode", Toast.LENGTH_LONG).show(); } }); silent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); Toast.makeText(MainActivity.this,"Now in silent Mode", Toast.LENGTH_LONG).show(); } }); mode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int mod=myAudioManager.getRingerMode(); if(mod==AudioManager.RINGER_MODE_VIBRATE){ Toast.makeText(MainActivity.this,"Now in Vibrate Mode", Toast.LENGTH_LONG).show(); } else if(mod==AudioManager.RINGER_MODE_NORMAL){ Toast.makeText(MainActivity.this,"Now in Ringing Mode", Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this,"Now in Vibrate Mode", Toast.LENGTH_LONG).show(); } } }); } }
以下是 activity_main.xml 的内容
此处 abc 表示 tutorialspoint 的徽标
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Android Audio Recording" android:id="@+id/textView" android:textSize="30dp" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tutorialspoint" android:id="@+id/textView2" android:textColor="#ff3eff0f" android:textSize="35dp" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/abc" android:layout_below="@+id/textView2" android:layout_alignLeft="@+id/textView2" android:layout_alignStart="@+id/textView2" android:layout_alignRight="@+id/textView2" android:layout_alignEnd="@+id/textView2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Mode" android:id="@+id/button" android:layout_below="@+id/imageView" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="59dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ring" android:id="@+id/button2" android:layout_alignTop="@+id/button" android:layout_centerHorizontal="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="vibrate" android:id="@+id/button3" android:layout_alignTop="@+id/button2" android:layout_alignRight="@+id/textView" android:layout_alignEnd="@+id/textView" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Silent" android:id="@+id/button4" android:layout_below="@+id/button2" android:layout_alignLeft="@+id/button2" android:layout_alignStart="@+id/button2" /> </RelativeLayout>
以下是 Strings.xml 的内容
<resources> <string name="app_name">My Application</string> </resources>
以下是 AndroidManifest.xml 的内容
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sairamkrishna.myapplication" > <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.sairamkrishna.myapplication" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
让我们尝试运行您的应用程序。我假设您已将您的实际 Android 移动设备连接到您的计算机。要在 Android Studio 中运行应用程序,请打开项目的某个活动文件,然后单击工具栏中的运行 图标。Android Studio 将显示图像
现在选择静音按钮,您将在通知栏中获得静音图标

现在只需选择响铃按钮,然后按当前模式按钮查看其状态是否已设置。

现在按振动按钮,然后按当前模式按钮查看其是否已设置。它将显示以下屏幕。
