Working With Android Audio Manager To Change Ringing Mode

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Android tutorial of audio manager to change ringing mode thumbnail

Every Phone Has a Functionality to Change the Ringing type of phone. Phones comes with three type of Ringing mode

  • Normal Mode
  • Silent Mode
  • Vibrate Mode

In this tutorial, we will see How to Find and Change the Ringing mode of an Android Phone Using Android AudioManager Class.

So, guys Lets start,
  1. Create a new Project by Going to File ⇒ New Android Project. Fill all the Details and Name your Activity as MainActivity.
  2. Once your Project is created successfully open your activity_main.xml file and Add Below Code.
<LinearLayout 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="com.targetandroid.info.MainActivity"  
   android:orientation="vertical">  
   
   <Button  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:text="Find Mode"  
     android:id="@+id/btmode_audioman" />  
   
   <ImageButton  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_gravity="center"  
     android:src="@android:drawable/ic_lock_silent_mode_off"  
     android:id="@+id/ibring_audioman"/>  
   
   <ImageButton  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:src="@android:drawable/ic_lock_silent_mode"  
     android:layout_gravity="center"  
     android:id="@+id/ibsilent_audioman"/>  
   
   <Button  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:text="Vibrate"  
     android:id="@+id/btvibrate_audioman"/>  
   
 </LinearLayout>

3. Now Open your MainActivity.java and Add below into it

import android.content.Context;  
 import android.media.AudioManager;  
 import android.support.v7.app.AppCompatActivity;  
 import android.os.Bundle;  
 import android.view.Menu;  
 import android.view.MenuItem;  
 import android.view.View;  
 import android.widget.Button;  
 import android.widget.ImageButton;  
 import android.widget.Toast;  
   
 import com.targetandroid.info.R;  
   
 public class AudioManagerActivity extends AppCompatActivity {  
   
   //Creating Variables  
   Button btmode_audioman,btvibrate_man;  
   ImageButton ibring_audioman,ibsilent_audioman;  
   AudioManager audioManager;  
   
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_audio_manager);  
     btmode_audioman=(Button)findViewById(R.id.btmode_audioman);  
     btvibrate_man=(Button)findViewById(R.id.btvibrate_audioman);  
     ibring_audioman=(ImageButton)findViewById(R.id.ibring_audioman);  
     ibsilent_audioman=(ImageButton)findViewById(R.id.ibsilent_audioman);  
       
     //using Audio Manager for Ringing Modes  
     audioManager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);  
   
     btvibrate_man.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View v) {  
         audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);  
         Toast.makeText(AudioManagerActivity.this,"Vibrate mode Enabled",Toast.LENGTH_SHORT).show();  
       }  
     });  
   
     ibring_audioman.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View v) {  
         audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);  
         Toast.makeText(AudioManagerActivity.this,"Ringing mode Enabled",Toast.LENGTH_SHORT).show();  
       }  
     });  
   
     ibsilent_audioman.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View v) {  
         audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);  
         Toast.makeText(AudioManagerActivity.this,"Silent Mode Enabled",Toast.LENGTH_SHORT).show();  
       }  
     });  
   
     btmode_audioman.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View v) {  
   
           int mod=audioManager.getRingerMode();  
   
         switch (mod){  
   
           case 0:  
             Toast.makeText(AudioManagerActivity.this, "Silent Mode", Toast.LENGTH_SHORT).show();  
             break;  
   
           case 1:  
             Toast.makeText(AudioManagerActivity.this,"Vibrate Mode", Toast.LENGTH_SHORT).show();  
             break;  
   
           default:  
             Toast.makeText(AudioManagerActivity.this,"Ringing Mode", Toast.LENGTH_SHORT).show();  
             break;  
         }  
   
         //Toast.makeText(AudioManagerActivity.this, mod, Toast.LENGTH_SHORT).show();  
   
       }  
     });  
   
   }  
 }

4. Now run your Project and See the Output.

Final output of audio manager to chnage ringing mode
Final Output


For More Awesome Android Tutorial Subscribe to my blog and share this with all your Programmer Friends

ThankYou


  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

2 Comments

  1. ampme pc November 17, 2017 Reply
  2. Devajeet December 21, 2017 Reply

Add a Comment

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