Android Application To Set Silent Mode, Ringer Mode and Vibrate Mode
Android Sample Codes

Android Application To Set Silent Mode, Ringer Mode and Vibrate Mode

Android Application To Set Silent Mode, Ringer Mode and Vibrate Mode | in this tutorial

I will show you how to create an android application using android studio which can be used to set ringer mode, vibrate mode and silent mode in your Android phone.

Android Application To Set Silent Mode, Ringer Mode and Vibrate Mode tutorial can also be used to check the current status of your ringing mode that is whether the phone is in Ringer node, Vibrate mode or in silent mode.

Android Application To Set Silent Mode, Ringer Mode and Vibrate Mode tutorial will toast the ringer status of your phone when you click on the button.

Android using AudioManager class that control access to your ringer volume and ringer profile ie : silent mode,vibrate mode, ringer mode etc in android

Create an object of AudioManager class by calling the getSystemService() method for establishing connection with the ringer profile. Its syntax is given below.

private AudioManager myAudioManager;

myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

Once you created or instantiate the object of android AudioManager class, setRingerMode method is used to set the audio or ringer profile of your phone. Its syntax is given below.

myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

The method setRingerMode takes an integer number as a parameter.

//For Normal 
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); 

//For Silent 
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); 

//For Vibrate 
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

For each mode , an integer number is assigned that will differentiate between different modes. The possible modes are return using the method  getRingerMode.

myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int mod=myAudioManager.getRingerMode();

if(mod==AudioManager.RINGER_MODE_VIBRATE){

// perform your action here

}

if(mod==AudioManager.RINGER_MODE_NORMAL){

// perform your action here

}

if(mod==AudioManager.RINGER_MODE_SILENT){

// perform your action here

}

Here  I have created an android application for ringtone management consisting of  four button, first  button is for knowing the current ringer mode status, second button is to set the phone in Ringer mode, third button is to set your phone in Silent mode and the fourth button is to set your phone in Vibrate mode.

Android Application To Set Silent Mode

AudioManager am;

am= (AudioManager)getBaseContext().getSystemService(Context.AUDIO_SERVICE);

//For Silent mode

am.setRingerMode(AudioManager.RINGER_MODE_SILENT);

Android Application To Set Ringer Mode

AudioManager am;

am= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE); 

//For Normal mode

am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

Android Application To Set Vibrate Mode

AudioManager am;

am= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE); 

//For Vibrate 

am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

Create a new project by selecting Empty Activity (File>New>New Project> Empty Activity).  Update the code of MainActivity.java in your file using the following code.

MainActivity.java

package com.example.phoneringmanagement;
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 {

    private AudioManager myAudioManager;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    }
    public void onMode(View v)
    {
        int mod=myAudioManager.getRingerMode();

        if(mod==AudioManager.RINGER_MODE_VIBRATE){
            Toast.makeText(this,"Now in Vibrate Mode",Toast.LENGTH_LONG).show();
        }

        else if(mod==AudioManager.RINGER_MODE_NORMAL){
            Toast.makeText(this,"Now in Ringing Mode",Toast.LENGTH_LONG).show();
        }

        else
        {
            Toast.makeText(this,"Now in Silent Mode",Toast.LENGTH_LONG).show();
        }
    }
    public void onRing(View v)
    {
        myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        Toast.makeText(this,"Now in Ringing Mode",Toast.LENGTH_LONG).show();
    }
    public void onSilent(View v)
    {
        myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
        Toast.makeText(this,"Now in silent Mode",Toast.LENGTH_LONG).show();
    }
    public void onVibrate(View v)
    {
        myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
        Toast.makeText(this,"Now in Vibrate Mode",Toast.LENGTH_LONG).show();
    }
}

The layout of the Android Application To Set Silent Mode, Ringer Mode and Vibrate Mode is shown below.

Four Buttons are created for toast the current ring mode status  when the user click on the button.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">
    <Button
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/bt1"
        android:text="CURRENT MODE CHECK"
        android:onClick="onMode"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/bt2"
        android:text="RINGER MODE"
        android:onClick="onRing"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/bt3"
        android:text="SILENT MODE"
        android:onClick="onSilent"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/bt4"
        android:text="VIBRATE MODE"
        android:onClick="onVibrate"/>

</LinearLayout>

The graphical design of the above code which looks like

Android Application To Set Silent Mode, Ringer Mode and Vibrate Mode

Output :

When you click on any of the button in the layout it will set the mode except the first button. First button is for knowing the current mode of your phone.

Android Application To Set Silent Mode, Ringer Mode and Vibrate Mode

 

Leave a Reply

Your email address will not be published. Required fields are marked *