title: How to silence android phone based on set time
body: <p>I'm trying to write a program that will silence a phone based on a time. So for instance, I press a button the I can set a start time and end time. At that start time the phone will silence. At that end time the phone will go back to regular ringer mode. Currently it will silence the phone but not go back to regular mode once it hits the end time. I have a lot of things commented out from previous attempts at trying to solve this issue. The uncommented parts is what I currently have. I really would like some help/guidance on this issue. </p>
  
  <pre><code>import java.util.Calendar;
  
  import android.app.Activity;
  import android.app.Dialog;
  import android.app.TimePickerDialog;
  import android.content.Context;
  import android.media.AudioManager;
  import android.os.Bundle;
  import android.text.format.Time;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.TimePicker;
  import android.widget.Toast;
  
  public class PresetEdit extends Activity
  {
      private AudioManager mAudioManager;
      private boolean mPhoneIsSilent;
      Button save;
      private Button startTime;
      private Button endTime;
  
      private int sHour;
      private int sMinute;
      private int eHour;
      private int eMinute;
  
      static final int TIME_DIALOG_ID = 0;
      static final int TIME_DIALOG_ID2 = 1;
  
      private TimePickerDialog.OnTimeSetListener sTimeSetListener =
              new TimePickerDialog.OnTimeSetListener() {
                  public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                      sHour = hourOfDay;
                      sMinute = minute;
                      updateDisplay();
                      displayToast();
                  }
              };
  
              private TimePickerDialog.OnTimeSetListener eTimeSetListener =
                      new TimePickerDialog.OnTimeSetListener() {
                          public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                              eHour = hourOfDay;
                              eMinute = minute;
                              updateDisplayE();
                              displayToastE();
                          }
                      };       
  
  
              private void updateDisplay() {
                  startTime.setText(
                      new StringBuilder()
                              .append(pad(sHour)).append(":")
                              .append(pad(sMinute)));
  
              }        
              private void updateDisplayE(){
                  endTime.setText(new StringBuilder()
                                          .append(pad(eHour)).append(":")
                                          .append(pad(eMinute)));
              }
              private void displayToast() {
                  Toast.makeText(this, new StringBuilder().append("Time choosen is ").append(startTime.getText()),   Toast.LENGTH_SHORT).show();
  
              }
              private void displayToastE() {
                  Toast.makeText(this, new StringBuilder().append("Time choosen is ").append(endTime.getText()),   Toast.LENGTH_SHORT).show();
  
              }
  
              private static String pad(int c) {
                  if (c &gt;= 10)
                      return String.valueOf(c);
                  else
                      return "0" + String.valueOf(c);
              }
  
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) 
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.preset_edit);
  
          TextView eventTxt = (TextView)findViewById(R.id.txtViewEvent);
          startTime = (Button)findViewById(R.id.btnStartTime);
          endTime = (Button)findViewById(R.id.btnEndTime);
  
           startTime.setOnClickListener(new View.OnClickListener()
              {
                  public void onClick(View v)
                  {
                      showDialog(TIME_DIALOG_ID);
                  }
              });
  
           endTime.setOnClickListener(new View.OnClickListener()
              {
                  public void onClick(View v)
                  {
                      showDialog(TIME_DIALOG_ID2);
                  }
              });
  
           final Calendar cal = Calendar.getInstance();
              sHour = cal.get(Calendar.HOUR);
              sMinute = cal.get(Calendar.MINUTE);
              updateDisplay();
  
              final Calendar calE = Calendar.getInstance();
              Calendar tmp = (Calendar)calE.clone();
              tmp.add(Calendar.HOUR, 0);
              tmp.add(Calendar.MINUTE, 2);
              //eHour = calE.get(Calendar.HOUR);
              Calendar stopTime = tmp;
              //eMinute = calE.get(Calendar.MINUTE);
              eHour = tmp.get(Calendar.HOUR);
              eMinute = tmp.get(Calendar.MINUTE);
              updateDisplayE();
  
  
          //save = (Button)findViewById(R.id.btnSave);
          //get event txt to change according to button click
  
          /*Bundle car = getIntent().getExtras();
          String receivingdata = car.getString("eventCar");
          TextView eventTxt = (TextView)findViewById(R.id.event);
          eventTxt.setText(receivingdata);*/
  
          /*Intent a = getIntent();
          a.getStringExtra("eventDrive");*/
  
          String drv = getIntent().getExtras().getString("event");
          if(drv!=null)
          {
              eventTxt.setText(drv); 
  
              Time now = new Time(Time.getCurrentTimezone());
              //Time end = new Time(Time.getCurrentTimezone());
              Time current = new Time(Time.getCurrentTimezone());
              //Time setToEnd = new Time(Time.getCurrentTimezone());
              current.setToNow();
  
              now.setToNow();
              //now.hour = 6;
              //now.minute = 35;
              //String sTime = ""+ now.hour+":"+now.minute;
              //startTime.setText(sTime);
              //end.hour = now.hour;
              //end.minute = now.minute + 2;
              //String eTime = ""+ end.hour+":"+end.minute;
              //endTime.setText(eTime);
              //setToEnd.hour = eHour;
              //setToEnd.minute = 13;
              if (sHour == now.hour &amp;&amp; sMinute == now.minute) 
              {
  
                  mAudioManager =(AudioManager)PresetEdit.this.getSystemService(Context.AUDIO_SERVICE);
                  mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                  //or adjust volume here instead setting silent profile for the ringer
  
  
  
  
              }
              if(current.hour == eHour &amp;&amp; current.minute == eMinute)
              {
                  mAudioManager =(AudioManager)PresetEdit.this.getSystemService(Context.AUDIO_SERVICE);
                  mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
  
              }
  
  
  
  
          }//end if
  
  
          String mov = getIntent().getExtras().getString("event");
          if(mov!=null)
              eventTxt.setText(mov);
  
          String study = getIntent().getExtras().getString("event");
          if(study!=null)
              eventTxt.setText(study);
  
          String act = getIntent().getExtras().getString("event");
          if(act!=null)
              eventTxt.setText(act);
  
  
  
  
          /*Intent intent = getIntent();
          String text = intent.getStringExtra("eventDriving");*/
          //eventTxt.setText(text);
      }
      /*private void setButtonClickListener() {
           save = (Button)findViewById(R.id.btnSave);
          save.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  if (mPhoneIsSilent) {
                      //change back to normal mode
                      mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                      mPhoneIsSilent = false;
                      save.setText("Activate");
                  }
                  else
                  {
                      //change to silent mode
                      mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                      //mAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
                      mPhoneIsSilent = true;
                      save.setText("Activated");
                  }
                  // Now toggle the UI again
  
              }
          });
      }
  
       private void checkIfPhoneIsSilent() {
              int ringermode = mAudioManager.getRingerMode();
              if (ringermode == AudioManager.RINGER_MODE_SILENT) {
                  mPhoneIsSilent = true;
              }
              else
              {
                  mPhoneIsSilent = false;
              }
          }
  
       @Override      
          protected void onResume() {
              super.onResume();
              checkIfPhoneIsSilent();
  
          }*/
  
      @Override
      protected Dialog onCreateDialog(int id)
      {
          switch (id)
          {
                  case TIME_DIALOG_ID:
                  return new TimePickerDialog(this,
                  sTimeSetListener, sHour, sMinute, false);
  
                  case TIME_DIALOG_ID2:
                      return new TimePickerDialog(this, 
                              eTimeSetListener, eHour, eMinute, true);
          }
      return null;
      }
  
  }
  </code></pre>