2012-07-26 90 views
2

我從哪裏可以設置從日期選取器和時間選擇在一次時間和日期驗證碼:如何爲選定的日期和時間設置鬧鐘?

private void dialoguetime() { 

    final Dialog dialog = new Dialog(this); 
    dialog.setContentView(R.layout.custom_dialogue); 
    dialog.setCancelable(true); 
    dialog.setTitle("This is Dialog 1"); 
    dialog.show(); 

    TimePicker time_picker = (TimePicker) dialog 
      .findViewById(R.id.timePicker1); 
    hours = time_picker.getCurrentHour(); 
    minute = time_picker.getCurrentMinute(); 
    time_picker.setOnTimeChangedListener(new OnTimeChangedListener() { 
     public void onTimeChanged(TimePicker view, int hourOfDay, 
       int minutes) { 
      // TODO Auto-generated method stub 
      // Toast.makeText(CustomDialog.this, 
      // "hourOfDay = "+hourOfDay+"minute = "+minute , 1000).show(); 
      hours = hourOfDay; 
      minute = minutes; 
     } 
    }); 

    final DatePicker date_picker = (DatePicker) dialog 
      .findViewById(R.id.datePicker1); 
    Button btn = (Button) dialog.findViewById(R.id.button2); 
    btn.setOnClickListener(new OnClickListener() { 

     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 



      xDate = date_picker.getYear() + "-"+ (date_picker.getMonth() + 1) + "-"+ date_picker.getDayOfMonth() + " " + hours+ ":" + minute + ":00"; 
      Toast.makeText(
        getApplicationContext(), 
        xDate, Toast.LENGTH_LONG) 
        .show(); 
      dialog.cancel(); 
     } 

    } 

    ); 

} 

由此我可以得到一個字符串作爲這樣yyyy-mm-dd hh:mm:ss日期格式,現在我想向該選定時間的用戶發出警報(作爲警報)。我爲此使用了鬧鈴管理器,但它不允許我選擇該日期?

我該怎麼做?

+0

請發佈您的報警管理器代碼。 – Tushar 2012-07-26 21:26:59

+0

我是alerm經理的開工人員,我使用的報警管理器在另一個項目中,每隔15分鐘發送一次短信,但我知道不會在這裏工作 – Reyjohn 2012-07-26 21:31:27

回答

2

如果你看一下AlarmManager的API(http://developer.android.com/reference/android/app/AlarmManager.html),你可以看到,該方法

set() 

需要下列參數:

public void set (int type, long triggerAtMillis, PendingIntent operation) 

其中

int type = One of ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC or RTC_WAKEUP. 
long triggerAtMillis = time in milliseconds that the alarm should go off, using the appropriate clock (depending on the alarm type). 
PendingIntent operation = Action to perform when the alarm goes off 

所以basicly你應該做的,是讓日之間的時間e選定的日期(DateFormat.parse()來解析日期格式的字符串)和當前日期。類似這樣的:

Date now = new Date(); 
SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); 
Date parsedDate = format.parse(YOUR_PICKED_DATE_AS_STRING); 
long alertTime = parsedDate.getTime() - now.getTime(); 

Intent someIntent = new Intent(getApplicationContext(), YourClass.class); 
PendingIntent pending = PendingIntent.getBroadcast(getApplicationContext(), 0, someIntent, 0); 

AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
manager.set(AlarmManager. 
相關問題