content: package anna.richarddawkinsalarmclock;
  
  import android.annotation.TargetApi;
  import android.app.AlarmManager;
  import android.app.PendingIntent;
  import android.content.Context;
  import android.content.Intent;
  import android.os.Build;
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.util.Log;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.TimePicker;
  import android.widget.Toast;
  
  import java.util.Calendar;
  import java.util.Random;
  
  
  
  public class MainActivity extends AppCompatActivity {
      AlarmManager alarmManager;
      private PendingIntent pending_intent;
  
      private TimePicker alarmTimePicker;
      private static MainActivity inst;
      private TextView alarmTextView;
  
      private AlarmReceiver alarm;
      private Context context;
  
      @TargetApi(Build.VERSION_CODES.M)
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_alarm);
  
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
  
          this.context = this;
  
          //alarm = new AlarmReceiver();
          alarmTextView = (TextView) findViewById(R.id.alarmText);
  
          final Intent myIntent = new Intent(this.context, AlarmReceiver.class);
  
          // Get the alarm manager service
          alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
  
          // set the alarm to the time that you picked
          final Calendar calendar = Calendar.getInstance();
          //calendar.add(Calendar.SECOND, 3);
          alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
  
  
  
  
  
          Button start_alarm= (Button) findViewById(R.id.start_alarm);
          start_alarm.setOnClickListener(new View.OnClickListener() {
              @TargetApi(Build.VERSION_CODES.M)
  
              @Override
              public void onClick(View v) {
  
  
                  calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getHour());
                  calendar.set(Calendar.MINUTE, alarmTimePicker.getMinute());
  
                  final int hour = alarmTimePicker.getHour();
                  final int minute = alarmTimePicker.getMinute();;
  
  
                  myIntent.putExtra("extra", "yes");
                  pending_intent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  
                  alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pending_intent);
  
                  setAlarmText("Alarm set to " + hour + ":" + minute);
                  //Toast.makeText(getApplicationContext(), "You set the alarm", Toast.LENGTH_SHORT).show();
              }
  
          });
  
          Button stop_alarm= (Button) findViewById(R.id.stop_alarm);
          stop_alarm.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
  
                  int min = 1;
                  int max = 9;
  
                  Random r = new Random();
                  int random_number = r.nextInt(max - min + 1) + min;
                  Log.e("random number is ", String.valueOf(random_number));
  
  
                  myIntent.putExtra("extra", "no");
                  sendBroadcast(myIntent);
  
                  alarmManager.cancel(pending_intent);
                  setAlarmText("Alarm canceled");
              }
          });
  
      }
  
      public void setAlarmText(String alarmText) {
          alarmTextView.setText(alarmText);
      }
  
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_alarm, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  
  
      @Override
      public void onStart() {
          super.onStart();
          inst = this;
      }
  
  
      @Override
      public void onDestroy() {
          super.onDestroy();
  
          Log.e("MyActivity", "on Destroy");
      }
  
  }