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.
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.
Now, select the version of Android and select the target Android devices.
Step 3
Now, add the activity and click the "Next" button.
Add Activity name and click "Finish".
Step 4
Go to activity_main.xml. This XML file contains the designing code for the Android app.
The XML code is given below.
- <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"
- tools:context="abu.myapplication.MainActivity" >
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="ringer app" />
-
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@+id/textView1"
- android:layout_marginTop="15dp"
- android:layout_toRightOf="@+id/textView1"
- android:text="silent" />
- <Button
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/button1"
- android:layout_marginTop="98dp"
- android:text="vibrate" />
-
- <Button
- android:id="@+id/button3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/button2"
- android:layout_below="@+id/button2"
- android:layout_marginTop="30dp"
- android:text="ringer" />
- </RelativeLayout>
Step 5
Go to Main Activity.java. This Java program is the backend language for Android.
The java code is given below.
- package abu.myapplication;
- import android.media.AudioManager;
- import android.os.Bundle;
- import android.provider.MediaStore.Audio;
- import android.app.Activity;
- import android.content.Context;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MainActivity extends Activity {
- Button b1, b2, b3;
- AudioManager am;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- b1 = (Button) findViewById(R.id.button1);
- b2 = (Button) findViewById(R.id.button2);
- b3 = (Button) findViewById(R.id.button3);
- am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
- b1.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
-
- am.setRingerMode(0);
- }
- });
- b2.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
-
- am.setRingerMode(1);
- }
- });
- b3.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
-
- am.setRingerMode(2);
- }
- });
- }
- }
Step 6
Now, go to the menu bar and click "Make Project" or press ctrl+f9 to debug the error.
Step 7
Then, click the "Run" button or press shift+f10 to run the project. Choose the virtual machine and click OK.
Conclusion
We have successfully created the application to set silent mode, ringer mode, vibrate mode in Android.