如何从数据库中检索日期和时间,并在该特定日期创建通知

时间:2013-08-24 06:14:08

标签: android

我想创建Reminder。我使用下面的代码在用户设置的特定时间和日期生成通知。

public class MainActivity extends Activity {
TimePicker timePicker;
DatePicker datePicker;
Button button1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     button1 = (Button) findViewById(R.id.button1);

     button1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
             timePicker = (TimePicker) findViewById(R.id.timePicker1);
                datePicker = (DatePicker) findViewById(R.id.datePicker1);    
                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);     

                Calendar calendar = Calendar.getInstance();     


                calendar.set(Calendar.YEAR, datePicker.getYear());
                calendar.set(Calendar.MONTH, datePicker.getMonth());
                calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());                 
                calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
                calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
                calendar.set(Calendar.SECOND, 0);

                Intent inn = new Intent(MainActivity.this,AlaramDetail.class);
                PendingIntent displayIntent = PendingIntent.getActivity(
                        getBaseContext(), 0,inn, 0);       
                alarmManager.set(AlarmManager.RTC_WAKEUP, 
                        calendar.getTimeInMillis(), displayIntent);



        }
    });

}
}

下面是AlaramDetail类,它包含用于生成通知的代码。

public class AlaramDetail extends Activity {

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detail);
    NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(android.R.drawable.stat_notify_more, "This is important notification",System.currentTimeMillis());
    String title = "pay attention";
    String detail = "this is implortant";
    Intent intent = new Intent(AlaramDetail.this,AlaramDetail.class);
    finish();
    PendingIntent pintent = PendingIntent.getActivity(AlaramDetail.this, 0,intent ,0 );

    notification.setLatestEventInfo(AlaramDetail.this, title, detail, pintent);

    notification.flags=Notification.FLAG_AUTO_CANCEL;
    notification.defaults|=Notification.DEFAULT_SOUND;
    notification.defaults|=Notification.DEFAULT_LIGHTS;
    notification.defaults|=Notification.DEFAULT_VIBRATE;
    nm.notify(1, notification);
}   

} 

上面的代码工作正常。 执行此操作后,我创建了一个数据库,存储user.database存储的日期和时间的多个日期和时间作为string.now我想生成存储在数据库中的特定日期和时间的通知。我不知道如何传递日期和从数据库到calendar.set(int field,int value)命令的时间。我想我可以使用游标来检索日期和时间,但我不知道在args.ay数据库有三列名称,日期,时间。

1 个答案:

答案 0 :(得分:1)

在数据库中存储时间(以毫秒为单位)也更好...它会为您提供更好的性能......

在检索数据后,您只需使用此代码设置时间

   Calendar calender = Calendar.getInstance();
   calender.setTimeInMillis(TimeInMilli);