Академический Документы
Профессиональный Документы
Культура Документы
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.
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.
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
RINGER_MODE_NORMAL
RINGER_MODE_SILENT
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.
@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();
}
}
});
}
}
<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>