title: Android / alarm manager / specific time and date
body: <p>Hello good people of stackoverflow,</p>
  
  <p>I want to write an app with a section to fire an alarm at a specific time and date. Idealy it would fire a notfication, but I would almost be happy if the alarm would work. I tried a few tutorials, and examples but none explained it thoroughly (if you know a site please send link). I think by now I got a mix of tries. 
  This is the activity that is supposed to gather Information on when the alarm should get off and fire it:</p>
  
  <pre><code>public class Activity_new_termin extends Activity {
  
   Button btnSelectDate,btnSelectTime, submit;
   EditText mTitle, mDescr;
   DatePicker pickerDate;
   TimePicker pickerTime;
  
   static final int DATE_DIALOG_ID = 0;
   static final int TIME_DIALOG_ID = 1;
   final static int RQS_1 = 1;
  
   public  int year,month,day,hour,minute;  
   private int mYear, mMonth, mDay,mHour,mMinute;
  
   private string date_name;
   private string descr;
  
   TextView info;
  
   public Activity_new_termin()
   {
               final Calendar c = Calendar.getInstance();
               mYear = c.get(Calendar.YEAR);
               mMonth = c.get(Calendar.MONTH);
               mDay = c.get(Calendar.DAY_OF_MONTH);
               mHour = c.get(Calendar.HOUR_OF_DAY);
               mMinute = c.get(Calendar.MINUTE);
   }
  
   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.activity_new_termin);
  
               btnSelectDate=(Button)findViewById(R.id.buttonSelectDate);
               btnSelectTime=(Button)findViewById(R.id.buttonSelectTime);
               submit=(Button)findViewById(R.id.butt_submit_newdate);
               mTitle=(EditText)findViewById(R.id.editText_datename_newdate);
               mDescr=(EditText)findViewById(R.id.editText_datedescr_newdate);
  
               btnSelectDate.setOnClickListener(new View.OnClickListener() {
  
                   public void onClick(View v) {
                        showDialog(DATE_DIALOG_ID);
                   }
               });
  
               btnSelectTime.setOnClickListener(new View.OnClickListener() {
  
                   public void onClick(View v) {
                        showDialog(TIME_DIALOG_ID);
                   }
               });
  
               submit.setOnClickListener(
                          new View.OnClickListener()
                          {
                              public void onClick(View view)
                              {
  
                                  Log.v("editText_datename_newdate", mTitle.getText().toString());
                                  Log.v("editText_datedescr_newdate", mDescr.getText().toString());
  
                                  Calendar current = Calendar.getInstance();
  
                                  Calendar cal = Calendar.getInstance();
  
                                  cal.set(Calendar.YEAR, year);
                                  cal.set(Calendar.MONTH, month);
                                  cal.set(Calendar.DAY_OF_MONTH, day);
                                  cal.set(Calendar.HOUR_OF_DAY, hour);
                                  cal.set(Calendar.MINUTE, minute);
                                  cal.set(Calendar.SECOND, 0);
                                  cal.set(Calendar.MILLISECOND, 0);
  
  
                                  if(cal.compareTo(current) &lt;= 0){
  
                                      Toast.makeText(getApplicationContext(), 
                                        "Invalid Date/Time", 
                                        Toast.LENGTH_LONG).show();
                                  }else{
                                   setAlarm(cal);
                                  }
  
                              }
                          });
   }
  
  
  
    private DatePickerDialog.OnDateSetListener mDateSetListener =
                           new DatePickerDialog.OnDateSetListener() {
  
                               public void onDateSet(DatePicker view, int yearSelected,
                                                     int monthOfYear, int dayOfMonth) {
                                  year = yearSelected;
                                  month = monthOfYear;
                                  day = dayOfMonth;
  
                                  btnSelectDate.setText("Date selected : "+day+"-"+month+"-"+year);
                               }
                           };
  
  
                           private TimePickerDialog.OnTimeSetListener mTimeSetListener =
                               new TimePickerDialog.OnTimeSetListener() {
  
                                   public void onTimeSet(TimePicker view, int hourOfDay, int min) {
                                       hour = hourOfDay;
                                       minute = min;
  
                                       btnSelectTime.setText("Time selected :"+hour+"-"+minute);
                                     }
                               };
  
                           @Override
                           protected Dialog onCreateDialog(int id) {
                               switch (id) {
                               case DATE_DIALOG_ID:
  
                                   return new DatePickerDialog(this,
                                               mDateSetListener,
                                               mYear, mMonth, mDay);
  
                               case TIME_DIALOG_ID:
                                   return new TimePickerDialog(this,
                                           mTimeSetListener, mHour, mMinute, false);  
  
                               }
                               return null;
                           }
  
                           private void setAlarm(Calendar targetCal){
  
                                info.setText("\n\n***\n"
                                  + "Alarm is set@ " + targetCal.getTime() + "\n"
                                  + "***\n");
  
                                Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
                                PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
                                AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
                                alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);   
                               }                         
  }
  </code></pre>
  
  <p>and its xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content" &gt;
  
  &lt;RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="470dp"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin" &gt;
  
  &lt;TextView
      android:id="@+id/textview_title_newdate"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="10dp"
      android:text="@string/title_new_termin" /&gt;
  
      &lt;EditText
      android:id="@+id/editText_datename_newdate"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/textview_title_newdate"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="36dp"
      android:ems="10"
      android:inputType="textMultiLine" /&gt;
  
   &lt;EditText
       android:id="@+id/editText_datedescr_newdate"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignLeft="@+id/buttonSelectDate"
       android:layout_below="@+id/buttonSelectTime"
       android:layout_marginTop="27dp"
       android:ems="10" &gt;
  
       &lt;requestFocus /&gt;
   &lt;/EditText&gt;
  
   &lt;Button
       android:id="@+id/butt_submit_newdate"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignLeft="@+id/textview_title_newdate"
       android:layout_below="@+id/editText_datedescr_newdate"
       android:layout_marginTop="38dp"
       android:text="@string/butt_newdate_submit" /&gt;
  
   &lt;Button
       android:id="@+id/buttonSelectDate"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/editText_datename_newdate"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="20dp"
       android:text="Select Date" /&gt;
  
   &lt;Button
       android:id="@+id/buttonSelectTime"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignLeft="@+id/editText_datedescr_newdate"
       android:layout_below="@+id/buttonSelectDate"
       android:layout_marginTop="14dp"
       android:text="Select Time" /&gt;
  
  &lt;/RelativeLayout&gt;
  &lt;/ScrollView&gt;
  </code></pre>
  
  <p>and finally the alarmreceiver:</p>
  
  <pre><code>import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.widget.Toast;
  
  public class AlarmReceiver extends BroadcastReceiver
  {
           @Override
              public void onReceive(Context context, Intent intent)
              {
  
                      // Show the toast  like in above screen shot
                      Toast.makeText(context, "Alarm ausgelöst", Toast.LENGTH_LONG).show();
  
  
               }
  
  }
  </code></pre>
  
  <p>Thank you so very much in advance!</p>