Alarm not working with calendar time?
<p>I am making an alarm application. It's working with some hardcoded values like</p>

<pre><code> calendar.add(Calendar.SECOND,30);
</code></pre>

<p>after 30 second alarm is firing but it's not working with </p>

<pre><code>            cal.set(Calendar.AM_PM,Calendar.PM);
            cal.set(Calendar.MONTH,8);
            cal.set(Calendar.YEAR,2011);
            cal.set(Calendar.DAY_OF_MONTH,4);
            cal.set(Calendar.HOUR_OF_DAY,5);
            cal.set(Calendar.MINUTE,30);
</code></pre>

<p>Here is my whole code:</p>

<p>........Activity class..................</p>

<pre><code>public class SampleAlarm1Activity extends Activity {
    public Context context;
    private boolean alarmstarted=true;
    Button setalarm;
    Button start;
    Button cancel;
    Edit Text year;
    EditText month;
    EditText date;
    EditText hour;
    EditText minute;
    Intent i;
    PendingIntent pi;
    AlarmManager am;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        context=getApplicationContext();
        setalarm=(Button)findViewById(R.id.setalarm);
        start=(Button) findViewById(R.id.start);
        cancel=(Button)findViewById(R.id.cancelalarm);
        month=(EditText)findViewById(R.id.month);
        date=(EditText)findViewById(R.id.date);
        hour=(EditText)findViewById(R.id.hour);
        minute=(EditText)findViewById(R.id.minute);
        setalarm.setOnClickListener(setalarmListener);
        start.setOnClickListener(startalarmListener);
        cancel.setOnClickListener(cancelalarm);
        start.setVisibility(android.view.View.INVISIBLE);
        month.setVisibility(android.view.View.INVISIBLE);
        date.setVisibility(android.view.View.INVISIBLE);
        hour.setVisibility(android.view.View.INVISIBLE);
        minute.setVisibility(android.view.View.INVISIBLE);
    }
    private OnClickListener setalarmListener=new OnClickListener() {
        @Override
        public void onClick(View v) {   
            month.setVisibility(android.view.View.VISIBLE);
            date.setVisibility(android.view.View.VISIBLE);
            hour.setVisibility(android.view.View.VISIBLE);
            minute.setVisibility(android.view.View.VISIBLE);
            start.setVisibility(android.view.View.VISIBLE);
        }
    };
    private OnClickListener startalarmListener=new OnClickListener() {
        @Override
        public void onClick(View v) {
            int  mon = 0,dat = 0,hr = 0,min = 0;
            try{
                String  mon2=month.getText().toString();
                System.out.println("month string entered::::::::::"+mon2);
                mon=Integer.parseInt(mon2);
                System.out.println("month int valuereturn entered::::::::::"+mon);
                String  dat2=date.getText().toString();
                dat=Integer.parseInt(dat2);
                System.out.println("date entered::::::::::"+dat);
                String  hr2=hour.getText().toString();
                hr=Integer.parseInt(hr2);  
                System.out.println("hour  entered::::::::::"+hr);
                String  min2=minute.getText().toString();
                min=Integer.parseInt(min2); 
                System.out.println("minute  entered::::::::::"+min);
            }
            catch(NumberFormatException exc){
                System.out.println("exceptin is :"+exc);
            }
            Calendar cal=Calendar.getInstance();
            cal.setTimeInMillis(System.currentTimeMillis());
            cal.set(Calendar.AM_PM,Calendar.PM);
            cal.set(Calendar.MONTH,mon);
            System.out.println("month string entered::::::::::"+mon);
            cal.set(Calendar.YEAR,2011);
            cal.set(Calendar.DAY_OF_MONTH,dat);
            System.out.println("date  string entered::::::::::"+dat);
            cal.set(Calendar.HOUR_OF_DAY,hr);
            System.out.println("hour   entered::::::::::"+hr);
            cal.set(Calendar.MINUTE,min);
            System.out.println("minute  entered::::::::::"+min);
            i = new Intent(SampleAlarm1Activity.this, AlarmReceiver.class);
            pi=PendingIntent.getBroadcast(SampleAlarm1Activity.this, 0, i, 0);
            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),pi);
            System.out.println("alarm on in activity ");
            Toast.makeText(context, "alarm on  activity", Toast.LENGTH_SHORT).show();
        }
    };
    private OnClickListener cancelalarm=new OnClickListener() {
        @Override
        public void onClick(View v) {
            if(alarmstarted)
                try{
                    am.cancel(pi);
                }catch(Exception e){
                    System.out.println("alarm not started"+e);
                }
                System.out.println("alarm cancel ");
                Toast.makeText(context, "alarm cancel", Toast.LENGTH_SHORT).show();
        }
    };
}
</code></pre>

<p>broadcast receiver code....in same package ...........................................</p>

<pre><code>public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent){
        System.out.println("alarm start");
        Toast.makeText(context, "alarm is working",Toast.LENGTH_SHORT).show();
    }
}
</code></pre>

<p>Manifest file..................................</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.android.SampleAlarm"
      android:versionCode="1"
      android:versionName="1.0"&gt;
    &lt;uses-sdk android:minSdkVersion="8" /&gt;
        &lt;uses-permission android:name="android.permission.SET_ALARM"&gt;&lt;/uses-permission&gt;
    &lt;uses-permission android:name="android.permission.WRITE_CALENDAR"&gt;&lt;/uses-permission&gt;
    &lt;uses-permission android:name="com.android.alarm.permission.SET_ALARM"&gt;&lt;/uses-permission&gt;
    &lt;uses-permission android:name="android.permission.SET_TIME"&gt;&lt;/uses-permission&gt;
    &lt;uses-permission android:name="android.permission.SET_TIME_ZONE"&gt;&lt;/uses-permission&gt;
    &lt;application android:icon="@drawable/icon" android:label="@string/app_name"&gt;
        &lt;activity android:name=".SampleAlarm1Activity"
                  android:label="@string/app_name"&gt;
            &lt;intent-filter&gt;
                &lt;action android:name="android.intent.action.MAIN" /&gt;
                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
         &lt;receiver android:name=".AlarmReceiver"&gt;&lt;/receiver&gt;
    &lt;/application&gt;
&lt;/manifest&gt;
</code></pre>
