Monday, April 30, 2012

Android silent mode example

SlientMode.java

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);
Button vibra = (Button) findViewById(R.id.vibration);

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_SHORT).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_SHORT).show();
}
});

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

main.xml

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

    <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" />
   
    <Button
        android:id="@+id/vibration"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Switch to Vibration" />

</LinearLayout>

No comments:

Post a Comment