Saturday 17 May 2014

Change ringer Mode

This post will describe how to set the change the properties of ringer mode using Audio manager class which help to access the ringer mode properties and allows to set the ringer mode properties i.e: (silent,vibrate,normal e.t.c). 

Ringer Mode:

Ringer mode is the property of android used to control ringer volume and ringer profile i-e: (silent,vibrate,Normal e.t.c) in android.

Android Manager:

AudioManager class provides access to these volume and ringer mode controls.

Output:

ringer mode ringingringer mode setting
Create new Android Project
Project Name: RingingMode
//tested from 2.3.3 to current android sdk 
Build Target: Android 2.3.3   //or greater than that
Application Name: RingingMode
Package Name: com.shaikhhamadali.blogspot.ringingmode
Create Layout file: activity_main


1. code of Layout:

<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:orientation="vertical"
    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="Set Ringer Mode" />

    <ImageButton
        android:id="@+id/imBtnVibrate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/vibrate_black"
        android:text="vibrate" />

    <ImageButton
        android:id="@+id/imBtnSilent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/mute_black"
        android:text="silent" />

    <ImageButton
        android:id="@+id/imBtnNormal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/sound_black"
        android:text="normal" />

</LinearLayout>

2. code of activity:

package com.changeringingmode;

import android.app.Activity;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends Activity {
 //declare views/controls 
 ImageButton imBtnVibrate,imBtnSilent,imBtnNormal;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  initializeControls();
  //initialize AudioManager
  final AudioManager audioManager = 
    (AudioManager) getSystemService(getApplicationContext().AUDIO_SERVICE);

  imBtnVibrate.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    //set Ringer mode as AudioManager.RINGER_MODE_VIBRATE for vibration
    audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
    //reset buttons images
    resetUI(1);
    //show toast of vibration mode
    Toast.makeText(getBaseContext(), "Mode: Vibration ", Toast.LENGTH_SHORT).show();
   }
  });

  imBtnSilent.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    //set Ringer mode as AudioManager.RINGER_MODE_SILENT for silent
    audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
    //reset buttons images
    resetUI(2);
    //show toast of silent mode
    Toast.makeText(getBaseContext(), "Mode: Silent ", Toast.LENGTH_SHORT).show();
   }
  });

  imBtnNormal.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    //set Ringer mode as AudioManager.RINGER_MODE_SILENT for silent
    audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
    //reset buttons images
    resetUI(3);
    //show toast of ringing mode
    Toast.makeText(getBaseContext(), "Mode: Ringing ", Toast.LENGTH_SHORT).show();
   }
  });


 }

 private void initializeControls() {
  imBtnVibrate = (ImageButton) findViewById(R.id.imBtnVibrate);
  imBtnSilent = (ImageButton) findViewById(R.id.imBtnSilent);
  imBtnNormal = (ImageButton) findViewById(R.id.imBtnNormal);
 }

 private void resetUI(int i) {
  if(i==1)imBtnVibrate.setImageResource(R.drawable.vibrate);
  else imBtnVibrate.setImageResource(R.drawable.vibrate_black);
  if(i==2)imBtnSilent.setImageResource(R.drawable.mute);
  else imBtnSilent.setImageResource(R.drawable.mute_black);
  if(i==3)imBtnNormal.setImageResource(R.drawable.sound_color);
  else imBtnNormal.setImageResource(R.drawable.sound_black);
 }
}

3. note that:

  • Above I have used ImageButton, to change the image on button click, image button is nothing but the combination of ImageView and Button.
  • AudioManager is not only used for ringer mode, it also handy to use for many purposes see here on develope.android.com.
  • you may be interested in these post on playing with settings, ENABLE/DISABLE MOBILE DATA,BLUETOOTH,AIRPLANE MODE etc.

4. conclusion:

  • Some information about Ringer Mode.
  • Some information about AudioManager class and its uses.
  • Some information about ImageButton.
  • Know how to change ringer mode profile using AudioManager.

    5. About the post:

    • The code seems to explain itself due to comments, but if you have any questions you can freely ask too!
    •  Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
    •  Hope you enjoy it!
        6. Source Code:
                you can download the source code from: GoogleDriveGithub

        Cheers,
        Hamad Ali Shaikh