我正在用戶選擇日期和時間的鬧鐘應用程序。數據存儲在SQL數據庫中。我想要做的就是在達到時間後通知通知。我試圖簡單地使用用戶選擇的時間,但即使刪除鬧鐘或編輯時間,鬧鐘仍然存在。這就是爲什麼我需要使用數據庫中的數據,所以如果警報被刪除,通知將不會通過。我查看了互聯網上的所有相關內容,似乎沒有任何工作。這是我在數據庫中插入數據的位置,以及在文本視圖中顯示日期和時間的位置。如何將AlarmManager與存儲在SQLite數據庫中的數據一起使用?
databaseHelperAlarm = new DatabaseHelperAlarm(this);
final Calendar c = Calendar.getInstance();
final int mYear = c.get(Calendar.YEAR);
final int mMonth = c.get(Calendar.MONTH);
final int mDay = c.get(Calendar.DAY_OF_MONTH);
final int mHour = c.get(Calendar.HOUR_OF_DAY);
final int mMinute = c.get(Calendar.MINUTE);
xml_arrow_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(AddAlarmActivity.this, AlarmActivity.class);
startActivity(intent);
}
});
xml_fab_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = xml_title.getText().toString();
String date = xml_date.getText().toString();
String time = xml_time.getText().toString();
String repeat = xml_repeat.getText().toString();
if ((xml_title.getText().toString().equals("")) || (xml_date.getText().toString().equals("")) || (xml_time.getText().toString().equals(""))) {
Toast.makeText(getApplicationContext(), "Fields Must Not Be Empty", Toast.LENGTH_SHORT).show();
} else {
databaseHelperAlarm.insertData(title, date, time, repeat);
Toast.makeText(getApplicationContext(), "Reminder Made", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(AddAlarmActivity.this, AlarmActivity.class);
startActivity(intent);
}
}
});
xml_set_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
InputMethodManager input = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
input.hideSoftInputFromWindow(xml_set_date.getWindowToken(), 0);
xml_title.clearFocus();
datePickerDialog = new DatePickerDialog(AddAlarmActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, dayOfMonth);
SimpleDateFormat format = new SimpleDateFormat("EEE, MMM dd, yyyy");
String dateString = format.format(calendar.getTime());
xml_date.setText(dateString);
}
}, mYear, mMonth, mDay);
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
if(!datePickerDialog.isShowing()){
datePickerDialog.show();
}
}
});
xml_set_time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
InputMethodManager input = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
input.hideSoftInputFromWindow(xml_set_time.getWindowToken(), 0);
xml_title.clearFocus();
timePickerDialog = new TimePickerDialog(AddAlarmActivity.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
String chosenHour = "";
String chosenMinute = "";
String chosenTimeZone = "";
if(selectedHour > 12){
chosenTimeZone = "PM";
selectedHour = selectedHour - 12;
if(selectedHour < 10){
chosenHour = "0"+selectedHour;
}else{
chosenHour = ""+selectedHour;
}
}else{
chosenTimeZone = "AM";
if(selectedHour < 10){
chosenHour = "0"+selectedHour;
}else{
chosenHour = ""+selectedHour;
}
}
if(selectedMinute < 10){
chosenMinute = "0"+selectedMinute;
}else{
chosenMinute= ""+selectedMinute;
}
xml_time.setText(chosenHour + ":" + chosenMinute +" "+chosenTimeZone);
}
}, mHour, mMinute, false);
timePickerDialog.show();
}
});}
}
我的猜測是,我需要另一個光標或東西,但我不知道從哪裏開始。這裏是我的數據庫的代碼的一部分,但。
public void insertData (String a_title, String a_date, String a_time, String a_repeat){
ContentValues contentValues = new ContentValues();
contentValues.put(A_TITLE, a_title);
contentValues.put(A_DATE, a_date);
contentValues.put(A_TIME, a_time);
contentValues.put(A_REPEAT, a_repeat);
SQLiteDatabase sqLiteDB = this.getWritableDatabase();
sqLiteDB.insert(TABLE_NAME, null, contentValues);
sqLiteDB.close();
}
public ArrayList<AlarmInfo> getMainInfo() {
ArrayList<AlarmInfo> list = new ArrayList<>();
String SQL = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(SQL, null);
if (cursor.moveToFirst()) {
do {
AlarmInfo ai = new AlarmInfo();
ai.setiD(cursor.getInt(0) + "");
ai.setTitle(cursor.getString(1));
ai.setDate(cursor.getString(2));
ai.setTime(cursor.getString(3));
list.add(ai);
} while (cursor.moveToNext());
}
return list;
}
public Cursor getAllData(int getId) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " +
A_ID + "=?", new String[] {Integer.toString(getId)});
return res;
}
我感謝任何幫助。謝謝。