Friday, November 27, 2015

android - Ringer mode normal, silent and vibrate in code

activity_main.xml

<?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:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    android:background="#9cbea0"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:textColor="#000"
        android:padding="15dp"
        />
    <Button
        android:id="@+id/btn_normal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ringer Mode Normal"
        android:layout_below="@id/tv"
        />
    <Button
        android:id="@+id/btn_silent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ringer Mode Silent"
        android:layout_toRightOf="@id/btn_normal"
        android:layout_below="@id/tv"
        />
    <Button
        android:id="@+id/btn_vibrate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ringer Mode Vibrate"
        android:layout_below="@id/tv"
        android:layout_toRightOf="@id/btn_silent"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcode;

import android.content.Context;
import android.content.res.Resources;
import android.media.AudioManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Resources mResources;
    private RelativeLayout mRelativeLayout;
    private TextView mTextView;
    private Button mButtonNormal;
    private Button mButtonSilent;
    private Button mButtonVibrate;
    private AudioManager mAudioManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();

        // Get the Resources
        mResources = getResources();

        // Get the widgets reference from XML layout
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mTextView = (TextView) findViewById(R.id.tv);
        mButtonNormal = (Button) findViewById(R.id.btn_normal);
        mButtonSilent = (Button) findViewById(R.id.btn_silent);
        mButtonVibrate = (Button) findViewById(R.id.btn_vibrate);

        /*
            public abstract Object getSystemService (String name)
                Return the handle to a system-level service by name. The class of the returned
                object varies by the requested name.
        */
        /*
            AudioManager
                AudioManager provides access to volume and ringer mode control.
                Use Context.getSystemService(Context.AUDIO_SERVICE) to get an instance of this class.
        */

        // Get an instance of AudioManager system service
        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        // Get the device initial ringer mode
        mTextView.setText("Ringer Mode : " + getRingerModeString());

        // Set a click listener for ringer mode normal button
        mButtonNormal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
                    public void setRingerMode (int ringerMode)
                        Sets the ringer mode.

                        Silent mode will mute the volume and will not vibrate. Vibrate mode will
                        mute the volume and vibrate. Normal mode will be audible and may vibrate
                        according to user settings.

                        This method has no effect if the device implements a fixed volume policy
                        as indicated by isVolumeFixed().

                    Parameters
                        ringerMode : The ringer mode, one of RINGER_MODE_NORMAL, RINGER_MODE_SILENT,
                            or RINGER_MODE_VIBRATE.
                */

                /*
                    public boolean isVolumeFixed ()
                        Indicates if the device implements a fixed volume policy.

                        Some devices may not have volume control and may operate at a fixed volume,
                        and may not enable muting or changing the volume of audio streams. This
                        method will return true on such devices.

                        The following APIs have no effect when volume is fixed:
                            adjustVolume(int, int)
                            adjustSuggestedStreamVolume(int, int, int)
                            adjustStreamVolume(int, int, int)
                            setStreamVolume(int, int, int)
                            setRingerMode(int)
                            setStreamSolo(int, boolean)
                            setStreamMute(int, boolean)
                */

                // Set the ringer mode normal
                mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

                // Get the device ringer mode
                mTextView.setText("Ringer Mode : " + getRingerModeString());
            }
        });

        // Set a click listener for ringer mode silent button
        mButtonSilent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Set the ringer mode silent
                mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);

                // Get the device ringer mode
                mTextView.setText("Ringer Mode : " + getRingerModeString());
            }
        });

        // Set a click listener for ringer mode vibrate button
        mButtonVibrate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Set the ringer mode vibrate
                mAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

                // Get the device ringer mode
                mTextView.setText("Ringer Mode : " + getRingerModeString());
            }
        });
   }

    // Custom method to get device ringer mode as string
    protected String getRingerModeString(){
        /*
            public int getRingerMode ()
                Returns the current ringtone mode.

            Returns
                The current ringtone mode, one of RINGER_MODE_NORMAL, RINGER_MODE_SILENT,
                or RINGER_MODE_VIBRATE.
        */

        int modeConstantValue = mAudioManager.getRingerMode();
        String ringerModeString = "";
        if(modeConstantValue == 0){
        /*
            public static final int RINGER_MODE_SILENT
                Ringer mode that will be silent and will not vibrate.
                (This overrides the vibrate setting.)

                Constant Value: 0 (0x00000000)
        */
        ringerModeString = "Silent";
        } else if(modeConstantValue == 1){
            /*
                public static final int RINGER_MODE_VIBRATE
                    Ringer mode that will be silent and will vibrate. (This will cause the phone
                    ringer to always vibrate, but the notification vibrate to only vibrate if set.)

                    Constant Value: 1 (0x00000001)
            */
            ringerModeString = "Vibrate";
        }else if(modeConstantValue == 2){
            /*
                public static final int RINGER_MODE_NORMAL
                    Ringer mode that may be audible and may vibrate. It will be audible if the
                    volume before changing out of this mode was audible. It will vibrate if the
                    vibrate setting is on.

                    Constant Value: 2 (0x00000002)
            */
            ringerModeString = "Normal";
        }

        // Return the ringer mode as string
        return ringerModeString;
    }
}
More android examples