Android - Switching Silent Mode to Normal Mode Example - ANDROID - Helper

Tuesday, November 08, 2011

Android - Switching Silent Mode to Normal Mode Example


SILENT MODE DEMO

SOURCE CODE [main.xml] is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <TextView android:id="@+id/txt1"
android:layout_width="fill_parent"
                                android:layout_height="wrap_content" />

                <Button android:id="@+id/silent"
android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
android:text="Switch to Silent Mode" />

                <Button android:id="@+id/normal"
android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
android:text="Switch to Normal Mode" />
</LinearLayout>

SOURCE CODE [SilentMode.java] is

package com.SilentModeDemo;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class SilentMode extends Activity
{
               
                public void onCreate(Bundle savedInstanceState)
{

                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.main);

                                final TextView txt = (TextView) findViewById(R.id.txt1);

                                Button silent = (Button) findViewById(R.id.silent);
                                Button normal = (Button) findViewById(R.id.normal);

                                final AudioManager mode = (AudioManager) this
                                                                .getSystemService(Context.AUDIO_SERVICE);

                                silent.setOnClickListener(new View.OnClickListener()
{
                                                public void onClick(View v)
{
                                                                txt.setText("The Mobile in Silent Mode");
                                                                mode.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                                                                Toast.makeText(getBaseContext(), "Silent Mode Activated",
 Toast.LENGTH_LONG).show();
                                                }
                                });

                                normal.setOnClickListener(new View.OnClickListener()
{
                                                public void onClick(View v)
{
                                                                txt.setText("The Mobile in Normal Mode");
                                                                mode.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                                                                Toast.makeText(getBaseContext(), "Normal Mode Activated",
                                                                                Toast.LENGTH_LONG).show();
                                                }
                                });

                }
}


//*******************************************************************//
// Source provided by Anish Kumar (Android Developer[Enrichware Systems]) //
//*******************************************************************//

The OUTPUT will be

 https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRvtTWNy2dU9xW0BMKlEOcVayNWqiCQSXZYTS1L4_uuUjc2OBbZ7SkzHqwHj69YkpfET_y1CgFIFvXEQtpxV4YwldT9uITu1YNHbkPry-Ia-jDSvBbXrVRVImyeZUyuxoW59YYq29zuVE/s480/silent_mode_demo1.png

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOIjybSIKJ0oBla2g9E52ZwYywEB_diuJSk5rBPrOWLMNi4ee7-oCDzSGNpb0WRZgX5QYISoZXhpB7pD9cVfjFU7m7RpBkocffX7Mx2HlsqhOyOnik_ysHOsNRAy9ESuJ5DcLMGTPizVs/s480/silent_mode_demo2.png

5 comments:

  1. This is very useful for me.......


    thanks boss



    ARUMUGAM.K
    TAMILNADU,INDIA.

    ReplyDelete
  2. sir i have a problem regarding closing my android application could you please help me ? i tried the following code but it does not close my app .i want to close the app based on the time given by user
    [code]
    package com.example.theading;

    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;

    public class MainActivity extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent myint=new Intent("android.intent.action.MUSIC_PLAYER");
    startActivity(myint);

    Thread mtr=new Thread(){

    public void run()
    {

    try{


    sleep(50000);


    }catch(InterruptedException e){
    e.printStackTrace();
    }finally{


    android.os.Process.killProcess(android.os.Process.myPid());

    }
    }











    };

    mtr.start();

    }
    }
    [/code]

    ReplyDelete
  3. thank you so much the code is great

    ReplyDelete
  4. please put manifest description like which permissions need to put etc..

    ReplyDelete