How to Set Ringer Mode, Vibrate Mode & Silent Mode Through AudioManager in Android

In this article you will learn how to set Ringer Mode, Vibrate Mode & Silent Mode through AudioManager in Android.

Procedure

  1. Start the Eclipse IDE.
  2. Make an activity MainActivity.java.
  3. Make a XML file activity_main.xml.
  4. In XML file,3 Buttons should be there.
  5. The code is given below.

MainActivity.java

package com.example.setringer;

import android.media.AudioManager;

import android.os.Bundle;

import android.provider.MediaStore.Audio;

import android.app.Activity;

import android.content.Context;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

 

public class MainActivity extends Activity {

      Button b1,b2,b3;

      AudioManager am;

 

      @Override

      protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);

            b1=(Button)findViewById(R.id.button1);

            b2=(Button)findViewById(R.id.button2);

            b3=(Button)findViewById(R.id.button3);

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

           

            b1.setOnClickListener(new OnClickListener() {

                 

                  @Override

                  public void onClick(View v) {

                        // TODO Auto-generated method stub

                        am.setRingerMode(0);

                  }

            });

           

            b2.setOnClickListener(new OnClickListener() {

                 

                  @Override

                  public void onClick(View v) {

                        // TODO Auto-generated method stub

                        am.setRingerMode(1);

                  }

            });

           

            b3.setOnClickListener(new OnClickListener() {

                 

                  @Override

                  public void onClick(View v) {

                        // TODO Auto-generated method stub
                        am.setRingerMode(3);
                  }
            });

           

      }

      
}


Activity_main.xml

 

<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=".MainActivity" >

 

    <TextView

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/hello_world" />

 

    <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@+id/textView1"

        android:layout_marginTop="78dp"

        android:layout_toRightOf="@+id/textView1"

        android:text="silent" />

 

    <Button

        android:id="@+id/button2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/button1"

        android:layout_centerVertical="true"

        android:text="vibrate" />

 

    <Button

        android:id="@+id/button3"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/button2"

        android:layout_below="@+id/button2"

        android:layout_marginTop="30dp"

        android:text="ringer" />

 

</RelativeLayout>

Output

Android.jpg

Android1.jpg