title: Why android alarm triggers (plays sound) while the time is set to backward?
body: <p><strong>The problem with my alarm clock is it plays alarm ring even I set the time backward.</strong></p>
  
  <p>The alarm mainly starts when a user toggle the switch.</p>
  
  <p><strong>Another problem with that is toggling the switch <em>OFF</em> doesn't stop the alarm. And it only happens while alarm triggering at the idle of the device or app. That means when the device is idle and alarm triggers, I have to open the alarmClock app to switch off the alarm, at that time pressing the toggle switch doesn't stop the sound.</strong></p>
  
  <p>In another situation other than triggering alarm at idle of the device the alarm clock works fine and I can toggle the button to stop the alarm. </p>
  
  <p>Please help me with those issue.</p>
  
  <p>MainActivity:</p>
  
  <pre><code>package com.mycompany.alarmclock;
  
  import android.support.v7.app.ActionBarActivity;
  
  import java.util.Calendar;
  
  public class MainActivity extends Activity {
  
      AlarmManager alarmManager;
      private PendingIntent pendingIntent;//an action to be performed by other/foreign application
      private TimePicker alarmTimePicker;//In where the user set the time
      private static MainActivity inst;
      private TextView alarmTextView;//the area where alarm message/notification will be displayed
  
  
      public static MainActivity instance() {
          return inst;
      }
  
      public void onStart() {
          super.onStart();
          inst = this;
      }
  
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
          alarmTextView = (TextView) findViewById(R.id.alarmText);
          ToggleButton alarmToggle = (ToggleButton) findViewById(R.id.alarmToggle);
          alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
      }
  
  
      public void onToggleClicked(View view) {
          if (((ToggleButton) view).isChecked()) {//if toggle button is "ON" do the alarming function
              Log.d("MainActivity", "Alarm On");
              Calendar calendar = Calendar.getInstance();
              calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
              calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
              Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
              pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);
              alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
  
          } else {
              AlarmReceiver.stopRingtone();
              alarmManager.cancel(pendingIntent);
              setAlarmText("");
              Log.d("MyActivity", "Alarm Off");
          }
  
      }
  
      public void setAlarmText(String alarmText) {
          alarmTextView.setText(alarmText);
  
      }
  }
  </code></pre>
  
  <p>Alarmreceiver:</p>
  
  <pre><code>package com.mycompany.alarmclock;
  
  import android.support.v4.content.WakefulBroadcastReceiver;
  
  import java.net.URI;
  
  public class AlarmReceiver extends WakefulBroadcastReceiver {
  
      private static Ringtone mRingtone=null;
      @Override
      public void onReceive(final Context context, Intent intent) {
  
          MainActivity inst=MainActivity.instance();
          inst.setAlarmText("\n                  Alarm!\n           Get Up! Get up!");
          Uri alarmUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
  
          if (alarmUri == null) {
              alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
          }
          Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
          mRingtone=RingtoneManager.getRingtone(context,alarmUri);
          mRingtone.play();
          //ringtone.play();
  
          //this will send a notification message
          ComponentName comp = new ComponentName(context.getPackageName(),
                  AlarmService.class.getName());
          startWakefulService(context, (intent.setComponent(comp)));
          setResultCode(Activity.RESULT_OK);
      }
      public  static void stopRingtone(){
          mRingtone.stop();
      }
  }
  </code></pre>
  
  <p>AlarmService:</p>
  
  <pre><code>    package com.mycompany.alarmclock;
  
  import android.support.v4.app.NotificationCompat;
  
  public class AlarmService extends IntentService {
  
  private NotificationManager alarmnotificationManager;
  
      public AlarmService(){
          super("AlarmService");
      }
  
      @Override
      protected void onHandleIntent(Intent intent) {
          sendNotification("Get up! Get up!");
  
      }
      private void sendNotification(String msg){
  
          Log.d("AlarmService","Sending notification...:"+msg);
          alarmnotificationManager=(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
  
          PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                  new Intent(this, MainActivity.class), 0);
  
          NotificationCompat.Builder alamNotificationBuilder = new NotificationCompat.Builder(
                  this).setContentTitle("Alarm").setSmallIcon(R.drawable.ic_launcher)
                  .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                  .setContentText(msg);
  
  
          alamNotificationBuilder.setContentIntent(contentIntent);
          alarmnotificationManager.notify(1, alamNotificationBuilder.build());
          Log.d("AlarmService", "Notification sent.");
  
      }
  
  }
  </code></pre>