2013-08-01 106 views
0
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(); 

回答

0

嘗試和刪除此行

calnow.setTimeInMillis(System.currentTimeMillis()); 

我相信一定會導致問題,不能肯定。

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