Android Application To Set Silent Mode, Ringer Mode, And Vibrate Mode In A Phone

Introduction

 
Android is one of the most popular operating systems for mobile. In this article, I will show you how to create an application that can be used to set silent mode, ringer mode, and vibrate mode in your Android phone. 
 
Requirements
Steps should be followed
 
Follow these steps to build the app. I have included the source code below.
 
Step 1
 
Open Android Studio and start a new Android Studio Project.
 
Android
 
Step 2
 
You can choose your application name and choose where your project is stored on the location. If you wish to use C++ for coding the project, mark the "Include C++ support", and click the "Next" button.
 
Android
 
Now, select the version of Android and select the target Android devices.
 
Android
 
Step 3
 
Now, add the activity and click the "Next" button.
 
Android
 
Add Activity name and click "Finish".
 
Android
 
Step 4
 
Go to activity_main.xml. This XML file contains the designing code for the Android app.
 
Android
 
The XML code is given below.
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2. xmlns:tools="http://schemas.android.com/tools"  
  3. android:layout_width="match_parent"  
  4. android:layout_height="match_parent"  
  5. tools:context="abu.myapplication.MainActivity" >  
  6.    <TextView  
  7.       android:id="@+id/textView1"  
  8.       android:layout_width="wrap_content"  
  9.       android:layout_height="wrap_content"  
  10.       android:text="ringer app" />  
  11.   
  12.    <Button  
  13.       android:id="@+id/button1"  
  14.       android:layout_width="wrap_content"  
  15.       android:layout_height="wrap_content"  
  16.       android:layout_below="@+id/textView1"  
  17.       android:layout_marginTop="15dp"  
  18.       android:layout_toRightOf="@+id/textView1"  
  19.       android:text="silent" />  
  20.    <Button  
  21.       android:id="@+id/button2"  
  22.       android:layout_width="wrap_content"  
  23.       android:layout_height="wrap_content"  
  24.       android:layout_alignLeft="@+id/button1"  
  25.       android:layout_marginTop="98dp"  
  26.       android:text="vibrate" />  
  27.   
  28.    <Button  
  29.       android:id="@+id/button3"  
  30.       android:layout_width="wrap_content"  
  31.       android:layout_height="wrap_content"  
  32.       android:layout_alignLeft="@+id/button2"  
  33.       android:layout_below="@+id/button2"  
  34.       android:layout_marginTop="30dp"  
  35.       android:text="ringer" />  
  36. </RelativeLayout>  
Step 5
 
Go to Main Activity.java. This Java program is the backend language for Android.
 
Android
 
The java code is given below.
  1. package abu.myapplication;  
  2. import android.media.AudioManager;  
  3. import android.os.Bundle;  
  4. import android.provider.MediaStore.Audio;  
  5. import android.app.Activity;  
  6. import android.content.Context;  
  7. import android.view.Menu;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. public class MainActivity extends Activity {  
  12.     Button b1, b2, b3;  
  13.     AudioManager am;  
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.         b1 = (Button) findViewById(R.id.button1);  
  19.         b2 = (Button) findViewById(R.id.button2);  
  20.         b3 = (Button) findViewById(R.id.button3);  
  21.         am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);  
  22.         b1.setOnClickListener(new OnClickListener() {  
  23.             @Override  
  24.             public void onClick(View v) {  
  25.                 // TODO Auto-generated method stub  
  26.                 am.setRingerMode(0);  
  27.             }  
  28.         });  
  29.         b2.setOnClickListener(new OnClickListener() {  
  30.             @Override  
  31.             public void onClick(View v) {  
  32.                 // TODO Auto-generated method stub  
  33.                 am.setRingerMode(1);  
  34.             }  
  35.         });  
  36.         b3.setOnClickListener(new OnClickListener() {  
  37.             @Override  
  38.             public void onClick(View v) {  
  39.                 // TODO Auto-generated method stub  
  40.                 am.setRingerMode(2);  
  41.             }  
  42.         });  
  43.     }  
  44. }  
Step 6
 
Now, go to the menu bar and click "Make Project" or press ctrl+f9 to debug the error.
 
Android
 
Step 7
 
Then, click the "Run" button or press shift+f10 to run the project. Choose the virtual machine and click OK.
 
Android
 

Conclusion

 
We have successfully created the application to set silent mode, ringer mode, vibrate mode in Android.
 
Android
 
Android


Similar Articles