3
votes
  SA=(Button)findViewById(R.id.button1);
  SA.setOnClickListener(new OnClickListener() {

    @SuppressWarnings("deprecation")
    @Override
        public void onClick(View v) {
            showDialog(id);


        }
    });

}
    @Override
    protected Dialog onCreateDialog(int id1) {
    switch (id1) {
    case id:
        // set time picker as current time

        return new TimePickerDialog(this, 
                                     timePickerListener, hour, min,false);
    }
    return null;
}

    private TimePickerDialog.OnTimeSetListener timePickerListener = 
        new TimePickerDialog.OnTimeSetListener() {
    public void onTimeSet(TimePicker view, int selectedHour,
            int selectedMinute) {
        Calendar calnow=Calendar.getInstance();
        calnow.setTimeInMillis(System.currentTimeMillis());
        calnow.set(Calendar.HOUR_OF_DAY,selectedHour);
        calnow.set(Calendar.MINUTE,selectedMinute);
        calnow.set(Calendar.SECOND, 0);
         Intent intent=new Intent(getBaseContext(),alarm.class); 
        PendingIntent pendingintent= PendingIntent.getBroadcast(getBaseContext(),0,       intent, 0);
         AlarmManager alarmManager =     (AlarmManager)getSystemService(Context.ALARM_SERVICE);

          alarmManager.set(AlarmManager.RTC_WAKEUP,  calnow.getTimeInMillis(),pendingintent);
          Toast.makeText(getBaseContext(), "alarm set", Toast.LENGTH_SHORT).show();

    }
};


     public void onReceive(Context arg0, Intent arg1) {
                 AlarmManager mgr = (AlarmManager)arg0.getSystemService(Context.ALARM_SERVICE);

                    Toast.makeText(arg0,"Alarm Started.....", Toast.LENGTH_LONG).show();

2

2 Answers

0
votes

Try and remove this line

calnow.setTimeInMillis(System.currentTimeMillis());

I believe that will be causing the problem, can't be sure.

0
votes

Try this:

   Calendar cal = Calendar.getInstance();
   cal.set(Calendar.HOUR_OF_DAY, selectedHour);
   cal.set(Calendar.MINUTE, selectedMinute);
   cal.set(Calendar.SECOND, 0);
   ca.set(Calendar.MILLISECOND, 0);
   Intent intent=new Intent(getBaseContext(),alarm.class); 
   PendingIntent pendingintent= PendingIntent.getBroadcast(getBaseContext(),0,        intent, 0);
   AlarmManager alarmManager =     (AlarmManager)getSystemService(Context.ALARM_SERVICE);
   alarmManager.set(AlarmManager.RTC_WAKEUP,  cal.getTimeInMillis(),pendingintent);
   Toast.makeText(getBaseContext(), "alarm set", Toast.LENGTH_SHORT).show();