Вы находитесь на странице: 1из 3

Android Ringer Mode

You can easily control your ringer volume and ringer profile i-e:(silent,vibrate,loud e.t.c)
in android. Android provides AudioManager class that provides access to these controls.

getSystemService() method

In order to use AndroidManager class, you have to first create an object of AudioManager
class by calling the getSystemService() method. Its syntax is given below.

private AudioManager myAudioManager;


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

setRingerMode() method
Once you instantiate the object of AudioManager class, you can use setRingerMode
method to set the audio or ringer profile of your device. Its syntax is given below.

myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

getRingerMode() method
Once you have set the mode , you can call the getRingerMode() method to get the set state
of the system. Its syntax is given below.

int mod = myAudioManager.getRingerMode();

The method setRingerMode() takes an integer number as a parameter. For each mode, an
integer number is assigned that will differentiate between different modes. The possible
modes are.

RINGER_MODE_VIBRATE

This Mode sets the device at vibrate mode.

RINGER_MODE_NORMAL

This Mode sets the device at normal(loud) mode.

RINGER_MODE_SILENT

This Mode sets the device at silent mode.

Example:
The below example demonstrates the use of AudioManager class. It creates an application
that allows you to set different ringer modes for your device.

Here is the content of MainActivity.java


public class MainActivity extends AppCompatActivity {
Button mode,ring,vibrate,silent;
AudioManager myAudioManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mode = (Button)findViewById(R.id.button);
ring = (Button)findViewById(R.id.button2);
vibrate = (Button)findViewById(R.id.button3);
silent = (Button)findViewById(R.id.button4);
myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
vibrate.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
Toast.makeText(MainActivity.this,"Now in Vibrate Mode",Toast.LENGTH_LONG).show();
}
});
silent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Toast.makeText(MainActivity.this,"Now in Silent Mode",Toast.LENGTH_LONG).show();
}
});
ring.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
Toast.makeText(MainActivity.this,"Now in Ringing Mode",Toast.LENGTH_LONG).show();
}
});
mode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int mod = myAudioManager.getRingerMode();
if(mod == AudioManager.RINGER_MODE_VIBRATE){
Toast.makeText(MainActivity.this,"Now in Vibrate Mode",Toast.LENGTH_LONG).show();
}else if(mod == AudioManager.RINGER_MODE_NORMAL){
Toast.makeText(MainActivity.this,"Now in Ringing Mode",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"Now in Silent Mode",Toast.LENGTH_LONG).show();
}
}
});
}
}

Here is the content of res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Audio Mangaer"
android:textSize="30dp"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mode"
android:id="@+id/button"
android:layout_below="@+id/textView"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView"
android:layout_marginTop="98dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ring"
android:id="@+id/button2"
android:layout_alignTop="@+id/button"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vibrate"
android:id="@+id/button3"
android:layout_below="@+id/button"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button"
android:layout_marginTop="81dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Silent"
android:id="@+id/button4"
android:layout_alignTop="@+id/button3"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView" />
</RelativeLayout>

Вам также может понравиться