0
我想創建Reminder.I使用下面的代碼在用戶設置的特定時間和日期生成通知。如何從數據庫檢索日期和時間並在該特定日期創建通知
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字段,INT值)commands.I認爲我可以使用光標檢索日期和時間,但我不知道什麼值傳遞在哪裏和哪裏args.MY數據庫有三列名稱,日期,時間。