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.
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);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);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.
Sr.No | Mode & Description |
---|---|
1 | RINGER_MODE_VIBRATE This Mode sets the device at vibrate mode. |
2 | RINGER_MODE_NORMAL This Mode sets the device at normal(loud) mode. |
3 | RINGER_MODE_SILENT This Mode sets the device at silent mode. |
int mod = myAudioManager.getRingerMode();Apart from the getRingerMode method, there are other methods availaible in the AudioManager class to control the volume and other modes. They are listed below.
Sr.No | Method & description |
---|---|
1 | adjustVolume(int direction, int flags) This method adjusts the volume of the most relevant stream |
2 | getMode() This method returns the current audio mode |
3 | getStreamMaxVolume(int streamType) This method returns the maximum volume index for a particular stream |
4 | getStreamVolume(int streamType) This method returns the current volume index for a particular stream |
5 | isMusicActive() This method checks whether any music is active. |
6 | startBluetoothSco() This method Start bluetooth SCO audio connection |
7 | stopBluetoothSco() This method stop bluetooth SCO audio connection. |
Example
The below example demonstrates the use of AudioManager class. It crates a basic application that allows you to set differnet ringer modes for your device.
To experiment with this example , you need to run this on an actual device.
Steps | Description |
---|---|
1 | You will use Eclipse IDE to create an Android application and name it as AudioManager under a package com.example.audiomanager. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs. |
2 | Modify src/MainActivity.java file to add AudioManager code |
3 | Modify layout XML file res/layout/activity_main.xml add any GUI component if required. |
4 | Modify res/values/string.xml file and add necessary string components. |
5 | Modify AndroidManifest.xml to add necessary permissions. |
6 | Run the application and choose a running android device and install the application on it and verify the results. |
Here is the content of src/com.example.audiomanager/MainActivity.java
package com.example.audiomanager; import android.media.AudioManager; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private Button Vibrate , Ring , Silent , Mode; private TextView Status; private AudioManager myAudioManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Vibrate = (Button)findViewById(R.id.button2); Ring = (Button)findViewById(R.id.button4); Silent = (Button)findViewById(R.id.button3); Mode = (Button)findViewById(R.id.button1); Status = (TextView)findViewById(R.id.textView2); myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); } public void vibrate(View view){ myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE); } public void ring(View view){ myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); } public void silent(View view){ myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); } public void mode(View view){ int mod = myAudioManager.getRingerMode(); if(mod == AudioManager.RINGER_MODE_NORMAL){ Status.setText("Current Status: Ring"); } else if(mod == AudioManager.RINGER_MODE_SILENT){ Status.setText("Current Status: Silent"); } else if(mod == AudioManager.RINGER_MODE_VIBRATE){ Status.setText("Current Status: Vibrate"); } else{ } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
Here is the content of activity_main.xml
<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=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="25dp" android:text="@string/audio" android:textAppearance="?android:attr/textAppearanceLarge" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button3" android:layout_alignBottom="@+id/button3" android:layout_alignRight="@+id/textView1" android:onClick="vibrate" android:text="@string/Vibrate" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_marginBottom="144dp" android:layout_marginLeft="40dp" android:layout_toLeftOf="@+id/button2" android:onClick="silent" android:text="@string/Silent" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_toRightOf="@+id/button1" android:onClick="ring" android:text="@string/Ring" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button2" android:layout_alignLeft="@+id/button3" android:layout_marginBottom="15dp" android:onClick="mode" android:text="@string/Mode" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="46dp" android:text="@string/Status" android:textAppearance="?android:attr/textAppearanceMedium" /> </RelativeLayout>
Here is the content of Strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">AudioManager</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="audio">Set Audio Profiles</string> <string name="Ring">Ring</string> <string name="Vibrate">Vibrate</string> <string name="Silent">Silent</string> <string name="Mode">Current Mode</string> <string name="Status">Current Status</string> </resources>
Here is the content of AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.audiomanager" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.audiomanager.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>Let's try to run your Androidmanager application. I assume you have connected your actual Android Mobile device with your computer. To run the app from Eclipse, open one of your project's activity files and click Run icon from the toolbar. Before starting your application, Eclipse will display following window to select an option where you want to run your Android application.
Select your mobile device as an option and then check your mobile device which will display following screen.
Now just select the ring button and then press the current mode button to see that if its status has been set.
Now press the silent button and then press the current mode button to see that if it is set or not.It will display the following screen.
Now press the vibrate button and then press the current mode button to see that if it is set or not.It will display the following screen.
Post A Comment:
0 comments: