警报管理器在选定时间之前立即启动警报?

时间:2013-08-01 10:19:42

标签: android alarmmanager

  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 个答案:

答案 0 :(得分:0)

尝试删除此行

calnow.setTimeInMillis(System.currentTimeMillis());

我相信会造成这个问题,不能肯定。

答案 1 :(得分:0)

试试这个:

   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();