AudioManager in Android with Example (original) (raw)

Last Updated : 23 Jul, 2025

**AudioManager is a class provided by Android which can be used to control the ringer volume of your Android device. With the help of this Audio Manager class, you can easily control the ringer volume of your device. Audio Manager Class can be used by calling the getSystemService() method in Android. When you create Audio Manager Class then you can use **setRingerMode() method to change the ringer volume of your device. The **setRingerMode() method takes an integer parameter to set the ringer profile of your device. There are three different integer parameters that need to be passed in **setRingerMode() method are as follows:

**RINGER_MODE_NORMAL This mode will set your device mode to Normal/General mode.
**RINGER_MODE_SILENT This mode will set your device mode to Silent mode.
**RINGER_MODE_VIBRATE This mode will set your device mode to vibrate mode.

Example

This is a Simple Example where we are creating a Ringtone Manager App. This app will help you to change the current state of your device from General to Vibrate and then to Silent Mode. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the **Java language.

**Step by Step Implementation

**Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select **Java as the programming language.

**Step 2: Add permissions in the AndroidManifest.xml file

Add below line in AndroidManifest.xml file.

**Step 3: Add google repository in the build.gradle file of the application project if by default it is not there

buildscript {

repositories {

google()

mavenCentral()

}

All Jetpack components are available in the Google Maven repository, include them in the build.gradle file

allprojects {

repositories {

google()

mavenCentral()

}

}

**Step 4: Modify the strings.xml file

Below is the code for the **strings.xml file.

XML `

GFG Ringtone Manager vibrate_mode silent_mode ringtone_mode Welcome to Ringtone Manager App Current Mode

`

**Step 5: Working with the activity_main.xml file

Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.

XML `

<!-- Textview to display the heading of the app-->
<TextView
    android:id="@+id/idTVHeading"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="50dp"
    android:layout_marginRight="20dp"
    android:text="@string/welcome_to_ringtone_manager_app"
    android:textAlignment="center"
    android:textColor="@color/green"
    android:textSize="20sp" />

<!-- Textview to display the current mode 
     of the Ringer mode-->
<TextView
    android:id="@+id/idTVCurrentMode"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/idTVHeading"
    android:layout_marginTop="60dp"
    android:text="@string/current_mode"
    android:textAlignment="center"
    android:textAllCaps="true"
    android:textColor="@color/black"
    android:textSize="20sp"
    android:textStyle="bold" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/idTVCurrentMode"
    android:layout_marginTop="80dp"
    android:orientation="horizontal"
    android:weightSum="3">
    
    <!-- width of image button is 0dp because 
         we have mentioned weight=1-->
    <!-- tint represents the color of icons 
         of image button-->
    <!-- all icons of image button are placed 
         in drawable folder-->
    <ImageButton
        android:id="@+id/idIBVibrateMode"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:layout_weight="1"
        android:background="@color/green"
        android:contentDescription="@string/vibrate_mode"
        android:src="@drawable/ic_vibrate"
        android:tint="@color/white" />

    <ImageButton
        android:id="@+id/idIBSilentMode"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:layout_weight="1"
        android:background="@color/green"
        android:contentDescription="@string/silent_mode"
        android:src="@drawable/ic_silent_mode"
        android:tint="@color/white" />

    <ImageButton
        android:id="@+id/idIBRingtoneMode"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:layout_weight="1"
        android:background="@color/green"
        android:contentDescription="@string/ring_mode"
        android:src="@drawable/ic_ringtone_mode"
        android:tint="@color/white" />
</LinearLayout>

`

**Step 6: Working with the MainActivity.java file

Below is the code for the **MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Java `

import android.app.NotificationManager; import android.content.Context; import android.content.Intent; import android.media.AudioManager; import android.os.Build; import android.os.Bundle; import android.view.View; import android.widget.ImageButton; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

// TextView to display the current ringer mode
TextView currentStateTV;

// Image buttons to switch ringer mode.
ImageButton silentIB, vibrateIB, ringtoneIB;

// object class variable for audio manager class.
private AudioManager audioManager;

// current mode to store integer value of ringer mode.
int currentmode;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    currentStateTV = findViewById(R.id.idTVCurrentMode);
    silentIB = findViewById(R.id.idIBSilentMode);
    vibrateIB = findViewById(R.id.idIBVibrateMode);
    ringtoneIB = findViewById(R.id.idIBRingtoneMode);
    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    // current mode will store current mode
    // of ringer of users device..
    currentmode = audioManager.getRingerMode();

    switch (currentmode) {
        case AudioManager.RINGER_MODE_NORMAL:
            currentStateTV.setText("Ringer Mode");
            break;
        case AudioManager.RINGER_MODE_SILENT:
            currentStateTV.setText("Silent Mode");
            break;
        case AudioManager.RINGER_MODE_VIBRATE:
            currentStateTV.setText("Vibrate Mode");
        default:
            currentStateTV.setText("Fail to get mode");
    }

    silentIB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            // the below code is to check the permission that the access
            // notification policy settings from users device..
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !notificationManager.isNotificationPolicyAccessGranted()) {
                Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
                startActivity(intent);
            }

            // set ringer mode here will sets your ringer mode to silent mode
            audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
            Toast.makeText(MainActivity.this, "Silent Mode Activated..", Toast.LENGTH_SHORT).show();
            currentStateTV.setText("Silent Mode Activated..");
        }
    });

    vibrateIB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // set ringer mode here will sets your ringer mode to vibrate mode
            audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
            Toast.makeText(MainActivity.this, "Vibrate Mode Activated..", Toast.LENGTH_SHORT).show();
            currentStateTV.setText("Vibrate Mode Activated..");
        }
    });

    ringtoneIB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // set ringer mode here will sets your ringer mode to normal mode
            audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
            Toast.makeText(MainActivity.this, "Ringtone Mode Activated..", Toast.LENGTH_SHORT).show();
            currentStateTV.setText("Ringtone Mode Activated..");
        }
    });
}

}

`

**Output: Run the app on your device

**Project Link: Click Here