Home »
Android
How to show audio mode of device in Android?
In this article, we are going to learn the method to use AudioManager and toast the current audio mode of device.
Submitted by Manu Jemini, on February 02, 2018
In the example below, we are going to use the Media.AudioManager class to have all the functionality required to use AudioManager. This getSystemService(Context.AUDIO_SERVICE) will create an object which can be used to process the current audio mode.
To implement this in your program you will need to import: android.media.AudioManager in your java file. Now on your XML File we need a button for switching the mode. For this just add button like this:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here to know the audio mode"
android:id="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="175dp"
android:onClick="button"/>
Now this button have a onClick attribute which is referring to a function called button, this function is implemented in JAVA file with corresponding logics to get the current mode and use the Toast class to make a Text.
Every mode has a value which we take from the object AManager.getRingerMode(). This value we check with every other mode which we want to check. It can be done like this: mod == AudioManager.RINGER_MODE_NORMAL
When we get our mode matched we Toast it.
Toast.makeText(MainActivity.this, "Your phone is on Silent Mode", Toast.LENGTH_LONG).show();
1) Java file:
package com.example.hp.myapplication;
import android.content.Context;
import android.media.AudioManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
AudioManager AManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
}
public void button(View v) {
int mod = AManager.getRingerMode();
if (mod == AudioManager.RINGER_MODE_VIBRATE) {
Toast.makeText(MainActivity.this, "Your phone is on Vibrate Mode", Toast.LENGTH_LONG).show();
} else if (mod == AudioManager.RINGER_MODE_NORMAL) {
Toast.makeText(MainActivity.this, "Your phone is on Ringing Mode", Toast.LENGTH_LONG).show();
} else if (mod == AudioManager.RINGER_MODE_SILENT) {
Toast.makeText(MainActivity.this, "Your phone is on Silent Mode", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Information not available", Toast.LENGTH_LONG).show();
}
}
}
2) XML file:
<?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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.hp.myapplication.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here to know the audio mode"
android:id="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="175dp"
android:onClick="button"/>
</RelativeLayout>
3) Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hp.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output