Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Changing Incoming Call Notification to Silent, Vibrate, or Normal

Author: Rachee Singh
Published? true -- FormatLanguage: W

Problem:

You need to put the Android device to silent, vibrate or normal mode

Solution:

Use Android's AudioManager system service to turn the phone to Normal, Silent and Vibrate modes

Discussion:

Here is a sample application that has 3 buttons to change the phone mode to Silent, Vibrate and Normal:

We instantiate the AudioManager class to be able to use the setRingerMode method. For each of these buttons (silentButton, normalButton and vibrateButton) we have OnclickListeners defined in which we used the AudioManager object to set the ringer mode. We also display a Toast notifying the mode change.


        am= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
        silentButton = (Button)findViewById(R.id.silent);
        normalButton = (Button)findViewById(R.id.normal);
        vibrateButton = (Button)findViewById(R.id.vibrate);
        
        //For Silent mode
        silentButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
				Toast.makeText(getApplicationContext(), "Silent Mode Activated.", Toast.LENGTH_LONG).show();
			}
		});
        

        //For Normal mode
        normalButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
				Toast.makeText(getApplicationContext(), "Normal Mode Activated", Toast.LENGTH_LONG).show();
			}
		});
        
        
        //For Vibrate mode
        vibrateButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
				Toast.makeText(getApplicationContext(), "Vibrate Mode Activated", Toast.LENGTH_LONG).show();
			}
		});
        

This is how the application looks when the Silent button is clicked. (Notice the silent icon in the status bar of the phone.)

No records found.