How to set your device on Ring/Normal mode in Android?

In this article, we are going to learn the method to use AudioManager and change the current audio mode of device.
Submitted by Manu Jemini, on February 02, 2018

In the example below, we are going to use the Media.AudioManager class to have all the functionality required for AudioManager. This getSystemService(Context.AUDIO_SERVICE) will create an object which can be used to process the current audio mode.

To implement this in your program you will need to import: android.media.AudioManager in your java file. Now on your XML File we need a button for switching the mode. For this just add button like this:

<Button
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="Click here to set the device on RING mode"
	android:id="@+id/button"
	android:layout_alignParentLeft="true"
	android:layout_alignParentStart="true"
	android:layout_marginLeft="10dp"
	android:layout_marginTop="175dp"
	android:onClick="button"
	android:layout_marginRight="10dp" />

Now this button have a onClick attribute which is referring to a function called button, this function is implemented in JAVA file with corresponding logics to change the current mode and use the Toast class to make a Text.

Every mode has a value which we provide to the function setRingerMode and change the current mode. When we change our mode successfully, we Toast it.

Toast.makeText(MainActivity.this,"Your device is now set on RING mode",Toast.LENGTH_LONG).show();

1) Java file:

package com.example.hp.myapplication;

import android.content.Context;
import android.media.AudioManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
AudioManager AManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    }

	public void button(View v) {
		AManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
		Toast.makeText(MainActivity.this,"Your device is now set on RING mode",Toast.LENGTH_LONG).show();
	}
}

2) XML file:

<?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.hp.myapplication.MainActivity">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click here to set the device on RING mode"
        android:id="@+id/button"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="175dp"
        android:onClick="button"
        android:layout_marginRight="10dp" />

</RelativeLayout>

3) Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output

Android - Set device ring normal 1
Android - Set device ring normal 2


Related Tutorials




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.